# Special Series


```python
%matplotlib inline
from sympy import *
init_printing()
```


```python
x, t = symbols('x, t')
```

SymPy can compute special series like formal power series and fourier series. This is a new feature released in SymPy 1.0

Let's try computing formal power series of some basic functions.


```python
exp_series = fps(exp(x), x)
exp_series
```

This looks very similar to what ``series`` has to offer, but unlike series a formal power series object returns an infinite expansion.


```python
exp_series.infinite # Infinite representation
```

We can easily find out any term of the expansion (no need to recompute the expansion).


```python
exp_series.term(51) # equivalent to exp_series[51]
```


```python
exp_series.truncate(10) # return a truncated series expansion
```

# Exercise

Try computing the formal power series of $\log(1 + x)$. Try to look at the infinite representation. What is the 51st term in this case? Compute the expansion about 1.


```python
log_series = fps(?)
log_series
```


```python
# infinite representation

```


```python
# 51st term
```


```python
# expansion about 1
```

# Fourier Series

Fourier series for functions can be computed using ``fourier_series`` function.

A sawtooth wave is defined as:
   1. $$ s(x) = x/\pi \in (-\pi, \pi) $$
   2. $$ s(x + 2k\pi) = s(x) \in (-\infty, \infty) $$
    
Let's compute the fourier series of the above defined wave.


```python
sawtooth_series = fourier_series(x / pi, (x, -pi, pi))
sawtooth_series
```


```python
plot(sawtooth_series.truncate(50)) 
```

See https://en.wikipedia.org/wiki/Gibbs_phenomenon for why the fourier series has peculiar behavior near jump discontinuties.

Just like formal power series we can index fourier series as well.


```python
sawtooth_series[51]
```

It is easy to shift and scale the series using ``shift`` and ``scale`` methods.


```python
sawtooth_series.shift(10).truncate(5)
```


```python
sawtooth_series.scale(10).truncate(5)
```

# Exercise

Consider a square wave defined over the range of (0, 1) as:
   1. $$ f(t) = 1 \in (0, 1/2] $$
   2. $$ f(t) = -1 \in (1/2, 1) $$
   3. $$ f(t + 1) = f(t) \in (-\infty, \infty) $$
    
Try computing the fourier series of the above defined function. Also, plot the computed fourier series.


```python
square_wave = Piecewise(?)
```


```python
square_series = fourier_series(?)
square_series
```


```python
plot(?)
```

# What next?

Try some basic operations like addition, subtraction, etc on formal power series, fourier series and see what happens.

5228f76c95f20e8dcabbade4093f3f64c4013f72
ipynb
Jupyter Notebook
tutorial_exercises/Advanced - Special Series.ipynb
gvvynplaine/scipy-2016-tutorial
aa417427a1de2dcab2a9640b631b809d525d7929
BSD-3-Clause
2016-06-21T21:11:02.000Z
2021-02-04T07:51:03.000Z
tutorial_exercises/Advanced - Special Series.ipynb
gvvynplaine/scipy-2016-tutorial
aa417427a1de2dcab2a9640b631b809d525d7929
BSD-3-Clause
2016-07-02T20:24:06.000Z
2016-07-11T11:31:44.000Z
tutorial_exercises/Advanced - Special Series.ipynb
gvvynplaine/scipy-2016-tutorial
aa417427a1de2dcab2a9640b631b809d525d7929
BSD-3-Clause
2016-06-25T09:04:24.000Z
2021-08-09T06:46:01.000Z
Qwen/Qwen-72B
1. YES
2. YES