# Special Series Solutions


```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
```




$$\sum_{k=1}^{\infty} \begin{cases} \frac{x^{k}}{k!} & \text{for}\: \operatorname{Mod}{\left (k,1 \right )} = 0 \\0 & \text{otherwise} \end{cases} + 1$$



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(1 + x), x)
log_series
```


```python
# infinite representation
log_series.infinite
```


```python
# 51st term
log_series.term(51)
```


```python
# expansion about 1
fps(log(1 + x), x, x0=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((1, t <= Rational(1, 2)), (-1, t > Rational(1, 2)))
```


```python
square_series = fourier_series(square_wave, (t, 0, 1))
square_series
```


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

# What next?

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

a96f09a95847608df9ebd5d8f64a126d3b9847ed
ipynb
Jupyter Notebook
tutorial_exercises/Advanced - Special Series Solutions.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 Solutions.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 Solutions.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