```python
# Required to load webpages
from IPython.display import IFrame
```

[Table of contents](../toc.ipynb)




# SymPy

* SymPy is a symbolic mathematics library for Python.
* It is a very powerful computer algebra system, which is easy to include in your Python scripts.
* Please find the documentation and a tutorial here [https://www.sympy.org/en/index.html](https://www.sympy.org/en/index.html)

## SymPy live

There is a very nice SymPy live shell in [https://live.sympy.org/](https://live.sympy.org/), where you can try SymPy without installation.


```python
IFrame(src='https://live.sympy.org/', width=1000, height=600)
```










## SymPy import and basics


```python
import sympy as sp

%matplotlib inline
```

Symbols can be defined with `sympy.symbols` like:


```python
x, y, t = sp.symbols('x, y, t')
```

These symbols and later equations are rendered with LaTeX, which makes pretty prints.


```python
display(x)
```


$\displaystyle x$


Expressions can be easily defined, and equations with left and right hand side are defined with `sympy.Eq` function. 


```python
expr = x**2
expr
```




$\displaystyle x^{2}$




```python
eq = sp.Eq(3*x, -10)
eq
```




$\displaystyle 3 x = -10$



Plots are done with `sympy.plot` and the value range can be adjusted.


```python
sp.plot(expr, (x, -5, 5))
```

## Why should you consider symbolic math at all?

The power of symbolic computation is their precision. Just compare these two results.


```python
import math
math.sqrt(8)
```




    2.8284271247461903




```python
sp.sqrt(8)
```




$\displaystyle 2 \sqrt{2}$



You can simplify expressions and equations and also expand them.


```python
eq = sp.sin(x)**2 + sp.cos(x)**2
eq
```




$\displaystyle \sin^{2}{\left(x \right)} + \cos^{2}{\left(x \right)}$




```python
sp.simplify(eq)
```




$\displaystyle 1$




```python
eq = x*(x + y)
eq
```




$\displaystyle x \left(x + y\right)$




```python
sp.expand(eq)
```




$\displaystyle x^{2} + x y$




```python
sp.factor(eq)
```




$\displaystyle x \left(x + y\right)$



Differentiation and integration are built in of course.


```python
eq = sp.sin(x) * sp.exp(x)
eq
```




$\displaystyle e^{x} \sin{\left(x \right)}$




```python
sp.diff(eq, x)
```




$\displaystyle e^{x} \sin{\left(x \right)} + e^{x} \cos{\left(x \right)}$




```python
sp.integrate(eq, x)
```




$\displaystyle \frac{e^{x} \sin{\left(x \right)}}{2} - \frac{e^{x} \cos{\left(x \right)}}{2}$



Or define an explicit interval for the integration.


```python
sp.integrate(eq, (x, -10, 10))
```




$\displaystyle \frac{e^{10} \sin{\left(10 \right)}}{2} + \frac{\cos{\left(10 \right)}}{2 e^{10}} + \frac{\sin{\left(10 \right)}}{2 e^{10}} - \frac{e^{10} \cos{\left(10 \right)}}{2}$



We can also easily substitute one variable of an expression.


```python
eq.subs(x, 2)
```




$\displaystyle e^{2} \sin{\left(2 \right)}$



Solve one equation. $x^2 + \sin(x) = -10$.


```python
sp.solve(x**2 + 3*x - 10, x)
```




    [-5, 2]



## More advanced topics

Here, we will solve a linear system of equations.


```python
e1 = sp.Eq(3*x + 4*y, -20)
e2 = sp.Eq(4*y, -3)

system_of_eq = [e1, e2]

from sympy.solvers.solveset import linsolve

linsolve(system_of_eq, (x, y))
```




$\displaystyle \left\{\left( - \frac{17}{3}, \  - \frac{3}{4}\right)\right\}$



Also differential equations can be used. Let us solve $y'' - y = e^t$ for instance.


```python
y = sp.Function('y')
sp.dsolve(sp.Eq(y(t).diff(t, t) - y(t), sp.exp(t)), y(t))
```




$\displaystyle y{\left(t \right)} = C_{2} e^{- t} + \left(C_{1} + \frac{t}{2}\right) e^{t}$



Finally, we will have a short look at matrices.


```python
A = sp.Matrix([[0, 1],
               [1, 0]])
A
```




$\displaystyle \left[\begin{matrix}0 & 1\\1 & 0\end{matrix}\right]$




```python
A = sp.eye(3)
A
```




$\displaystyle \left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right]$




```python
A = sp.zeros(2, 3)
A
```




$\displaystyle \left[\begin{matrix}0 & 0 & 0\\0 & 0 & 0\end{matrix}\right]$



Inversion of a matrix is done with `**-1` or better readable with `.inv()`.


```python
A = sp.eye(2) * 2
A = A * 2
A.inv()
```


```python
A[-2] = x
A
```




$\displaystyle \left[\begin{matrix}4 & 0\\x & 4\end{matrix}\right]$



## To sum up

* SymPy is a very powerful computer algebra package!
* It is light, small, and easy to install through pip and conda.
* Simple to integrate in your Python project.

fa3b535b85d5576971f790957f5f9355674b1292
ipynb
Jupyter Notebook
02_tools-and-packages/04_sympy.ipynb
awiawi/py-algorithms-4-automotive-engineering
4e5c52988931984a40fe9b605d55759033eeed31
MIT
02_tools-and-packages/04_sympy.ipynb
awiawi/py-algorithms-4-automotive-engineering
4e5c52988931984a40fe9b605d55759033eeed31
MIT
02_tools-and-packages/04_sympy.ipynb
awiawi/py-algorithms-4-automotive-engineering
4e5c52988931984a40fe9b605d55759033eeed31
MIT
Qwen/Qwen-72B
1. YES
2. YES