## Symbolic Mathematics with [Sympy](http://docs.sympy.org/latest/tutorial/intro.html)



```python
import sympy as sym
import numpy as np
import matplotlib.pyplot as plt
```

#### declare symbolic variables 


```python
x = sym.Symbol('x')
y = sym.Symbol('y')
```

or


```python
x, y = sym.symbols('x y')
```

#### declare the matematical expression $xy\exp[-(x^2+y^2)]$


```python
expr = x*y*sym.exp(-(x**2+y**2))
```

#### print the `LaTeX` expression


```python
sym.latex(expr)
```

#### expand an expression


```python
expr.expand()
```


```python
sym.expand( (x-y)**3 )
```


```python
sym.expand_trig(sym.cos(2*x))
```

#### simplify an expression


```python
sym.simplify( sym.sin(x) * ( sym.sin(x)**2 + sym.cos(x)**2 ) / sym.tan(x) )
```

#### Task: declare and simplify the expression $\left(2 \cos^{2}{\left (u \right )} - 1\right) \sin{\left (v \right )} + 2 \sin{\left (u \right )} \cos{\left (u \right )} \cos{\left (v \right )}$


```python
x,y = sym.symbols('u v')
expr = (2*sym.cos(x)**2 - 1)*sym.sin(y) + 2*sym.sin(x)*sym.cos(x)*sym.cos(y)
simplified = sym.simplify(expr)
print( sym.latex(simplified) )
```

* Limit, Integral, and Derivative represent the unevaluated calculation
* limit, integrate, and diff perform the calculation

#### limits: calculate $\lim_{x \to \frac{\pi}{2}} \tan{\left (x \right )}$


```python
x = sym.Symbol('x')
```


```python
print( sym.latex( sym.Limit( sym.tan(x), x, sym.pi/2, dir='+' ) ), '=' )
sym.limit( sym.tan(x), x, sym.pi/2, dir='+' )
```


```python
sym.limit( sym.tan(x), x, sym.pi/2, dir='-' )
```

#### Task: using `sym.Integral`,  `sym.integrate`, and  `sym.latex` calculate and print the integral $\int x y e^{- x^{2} - y^{2}}\, dx$ and its solution


```python
x,y = sym.symbols('x y')
expr = x*y*sym.exp(-(x**2+y**2))
print( sym.latex( sym.Integral(expr, x) ), '=', sym.latex( sym.integrate(expr, x) ))
```

#### Task: calculate and print the integral $\int_{-2}^{2}\int_{-2}^{2} x y e^{- x^{2} - y^{2}}\, dx\, dy$ and its solution


```python
print( sym.latex( sym.Integral(expr, [x,-2,2], [y,-2,2] ) ), '=', sym.latex( sym.integrate(expr, [x,-2,2], [y,-2,2] ) ) )
```

753f0b01db32d24f17c7a6d66538aca645bb8eee
ipynb
Jupyter Notebook
2017/lectures/03-sympy.ipynb
urania277/jupyter-course
20060173e7355fc4726148f00b61404d2613b74b
MIT
2017/lectures/03-sympy.ipynb
urania277/jupyter-course
20060173e7355fc4726148f00b61404d2613b74b
MIT
2017/lectures/03-sympy.ipynb
urania277/jupyter-course
20060173e7355fc4726148f00b61404d2613b74b
MIT
2018-11-20T14:45:43.000Z
2018-11-20T14:45:43.000Z
Qwen/Qwen-72B
1. YES
2. YES