# SymPy Tutorial

*Arthur Ryman* <br/>
*Last Updated: 2020-04-14*


This notebook contains the examples from the 
[SymPy Tutorial](https://docs.sympy.org/latest/tutorial/index.html).

## Simplification


```python
from sympy import *
x, y, z = symbols('x y z')
init_printing(use_unicode=True)
```

### simplify


```python
simplify(sin(x)**2 + cos(x)**2)
```


```python
simplify((x**3 + x**2 - x - 1)/(x**2 + 2*x + 1))
```


```python
simplify(gamma(x)/gamma(x - 2))
```


```python
simplify(x**2 + 2*x + 1)
```

### Polynomial/Rational Function Simplification

#### expand


```python
expand((x + 1)**2)
```


```python
expand((x + 2)*(x - 3))
```


```python
expand((x + 1)*(x - 2) - (x -1)*x)
```

#### factor


```python
factor(x**3 - x**2 + x - 1)
```


```python
factor(x**2*z + 4*x*y*z + 4*y**2*z)
```


```python
factor_list(x**2*z + 4*x*y*z + 4*y**2*z)
```


```python
expand((cos(x) + sin(x))**2)
```


```python
factor(sin(x)**2 + 2*cos(x)*sin(x) + cos(x)**2)
```

#### collect


```python
expr = x*y + x - 3 + 2*x**2 - z*x**2 + x**3
expr
```


```python
collected_expr = collect(expr, x)
collected_expr
```


```python
collected_expr.coeff(x, 2)
```

#### cancel


```python
cancel((x**2 + 2*x + 1)/(x**2 + x))
```


```python
expr = 1/x + (3*x/2 - 2)/(x - 4)
expr
```


```python
cancel(expr)
```


```python
expr = (x*y**2 - 2*x*y*z + x*z**2 + y**2 - 2*y*z + z**2)/(x**2 - 1)
expr
```


```python
cancel(expr)
```


```python
factor(expr)
```

#### apart


```python
expr = (4*x**3 + 21*x**2 + 10*x + 12)/(x**4 + 5*x**3 + 5*x**2 + 4*x)
expr
```


```python
apart(expr)
```

### Trigonometric Simplification


```python
acos(x)
```


```python
cos(acos(x))
```


```python
asin(1)
```

#### trigsimp


```python
trigsimp(sin(x)**2 + cos(x)**2)
```


```python
trigsimp(sin(x)**4 - 2*cos(x)**2*sin(x)**2 + cos(x)**4)
```


```python
trigsimp(sin(x)*tan(x)/sec(x))
```


```python
trigsimp(cosh(x)**2 + sinh(x)**2)
```


```python
trigsimp(sinh(x)/tanh(x))
```

#### expand_trig


```python
expand_trig(sin(x + y))
```


```python
expand_trig(tan(2*x))
```


```python
trigsimp(sin(x)*cos(x) + sin(y)*cos(x))
```

### Powers


```python
x, y = symbols('x y', positive=True)
a, b = symbols('a b', real=True)
z, t, c = symbols('z t c')
```


```python
sqrt(x) == x**Rational(1, 2)
```




    True



#### powsimp


```python
powsimp(x**a*x**b)
```


```python
powsimp(x**a*y**a)
```


```python
powsimp(t**c*z**c)
```


```python
powsimp(t**c*z**c, force=True)
```


```python
(z*t)**2
```


```python
sqrt(x*y)
```


```python
powsimp(z**2*t**2)
```


```python
powsimp(sqrt(x)*sqrt(y))
```

#### expand_power_exp / expand_power_base


```python
expand_power_exp(x**(a + b))
```


```python
expand_power_base((x*y)**a)
```


```python
expand_power_base((z*t)**c)
```


```python
expand_power_base((z*t)**c, force=True)
```


```python
x**2*x**3
```


```python
expand_power_exp(x**5)
```

#### powdenest


```python
powdenest((x**a)**b)
```


```python
powdenest((z**a)**b)
```


```python
powdenest((z**a)**b, force=True)
```

### Exponentials and logarithms


```python
ln(x)
```


```python
x, y = symbols('x y', positive=True)
n = symbols('n', real=True)
```

#### expand_log


```python
expand_log(log(x*y))
```


```python
expand_log(log(x/y))
```


```python
expand_log(log(x**2))
```


```python
expand_log(log(x**n))
```


```python
expand_log(log(z*t))
```


```python
expand_log(log(z**2))
```


```python
expand_log(log(z**2), force=True)
```

#### logcombine


```python
logcombine(log(x) + log(y))
```


```python
logcombine(n*log(x))
```


```python
logcombine(n*log(z))
```


```python
logcombine(n*log(z), force=True)
```

### Special Functions


```python
x, y, z = symbols('x y z')
k, m, n = symbols('k m n')
```


```python
factorial(n)
```


```python
binomial(n, k)
```


```python
gamma(z)
```


```python
hyper([1, 2], [3], z)
```


```python
tan(x).rewrite(sin)
```

#### rewrite


```python
factorial(x).rewrite(gamma)
```

#### expand_func


```python
expand_func(gamma(x + 3))
```

### hyperexpand


```python
hyperexpand(hyper([1, 1], [2], z))
```


```python
expr = meijerg([[1], [1]], [[1], []], -z)
expr
```


```python
hyperexpand(expr)
```

##### combsimp


```python
n, k = symbols('n k', integer = True)
combsimp(factorial(n)/factorial(n -3))
```


```python
combsimp(binomial(n+1, k+1)/binomial(n, k))
```

gammasimp


```python
gammasimp(gamma(x)*gamma(1 - x))
```

### Example: Continued Fractions


```python
def list_to_frac(l):
    expr = Integer(0)
    for i in reversed(l[1:]):
        expr += i
        expr = 1/expr
    return l[0] + expr
list_to_frac([x, y, z])
```


```python
list_to_frac([1, 2, 3, 4])
```


```python
syms = symbols('a0:5')
syms
```


```python
a0, a1, a2, a3, a4 = syms
frac = list_to_frac(syms)
frac
```


```python
frac = cancel(frac)
frac
```


```python
l = []
frac = apart(frac, a0)
frac
```


```python
l.append(a0)
frac = 1/(frac - a0)
frac
```


```python
frac = apart(frac, a1)
frac
```


```python
l.append(a1)
frac = 1/(frac - a1)
frac = apart(frac, a2)
frac
```


```python
l.append(a2)
frac = 1/(frac - a2)
frac = apart(frac, a3)
frac
```


```python
l.append(a3)
frac = 1/(frac - a3)
frac = apart(frac, a4)
frac
```


```python
l.append(a4)
list_to_frac(l)
```


```python
import random
l = list(symbols('a0:5'))
random.shuffle(l)
orig_frac = frac = cancel(list_to_frac(l))
del l
```


```python
frac
```


```python
l = [a1]
frac = apart(frac, a1)
frac = 1/(frac - a1)
frac
```


```python
l.append(a3)
frac = apart(frac, a3)
frac = 1/(frac - a3)
frac
```


```python
l.append(a4)
frac = apart(frac, a4)
frac = 1/(frac - a4)
frac
```


```python
l.append(a0)
frac = apart(frac, a0)
frac = 1/(frac - a0)
frac
```


```python
l.append(a2)
new_frac = cancel(list_to_frac(l))
new_frac
```


```python
new_frac == orig_frac
```




    True



a78feacab31d9dddc8d550f1719dbfbfafd43201
ipynb
Jupyter Notebook
notes/docs.sympy.org/tutorial/simplification.ipynb
agryman/acmpy
9566a0abce51cb610ec6c79eaea2e2b26b707419
MIT
notes/docs.sympy.org/tutorial/simplification.ipynb
agryman/acmpy
9566a0abce51cb610ec6c79eaea2e2b26b707419
MIT
2022-01-21T22:17:28.000Z
2022-01-21T22:17:28.000Z
notes/docs.sympy.org/tutorial/simplification.ipynb
agryman/acmpy
9566a0abce51cb610ec6c79eaea2e2b26b707419
MIT
Qwen/Qwen-72B
1. YES
2. YES