# Tutorial Brief

SymPy is symbolic mathematics library written completely in Python and doesn't require any dependencies.

Finding Help:

- http://docs.sympy.org/latest/index.html
- http://sympy.org/en/features.html
- http://nbviewer.ipython.org/github/jrjohansson/scientific-python-lectures/blob/master/Lecture-5-Sympy.ipynb

<table>
<tr>
    <td></td>
    <td><h4>NumPy</h4> Base N-dimensional array package </td>
    <td></td>
    <td><h4>SciPy</h4> Fundamental library for scientific computing </td>
    <td></td>
    <td><h4>Matplotlib</h4> Comprehensive 2D Plotting </td>
</tr>
<tr>
    <td></td>
    <td><h4>IPython</h4> Enhanced Interactive Console </td>
    <td style="background:Lavender;"></td>
    <td style="background:Lavender;"><h4>SymPy</h4> Symbolic mathematics </td>
    <td></td>
    <td><h4>Pandas</h4> Data structures & analysis </td>
</tr>
</table>

> ###SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) while keeping the code as simple as possible in order to be comprehensible and easily extensible. SymPy is written entirely in Python and does not require any external libraries.
> **http://sympy.org/**

# Import SymPy


```python
from sympy import *
```


```python
3 + math.sqrt(3)
```




    4.732050807568877




```python
expr = 3 * sqrt(3)
expr
```




    3*sqrt(3)




```python
init_printing(use_latex='mathjax')
```


```python
expr
```




$$3 \sqrt{3}$$




```python
expr = sqrt(8)
expr
```




$$2 \sqrt{2}$$



# `symbols()` & `Symbol()`


```python
x, y = symbols("x y")
```


```python
expr = x**2 + y**2
expr
```




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




```python
expr = (x+y)**3
expr
```




$$\left(x + y\right)^{3}$$



## Assumptions for symbols


```python
a = Symbol("a")
```


```python
a.is_imaginary
```


```python
b = Symbol("b", integer=True)
```


```python
b.is_imaginary
```




    False




```python
c = Symbol("c", positive=True)
```


```python
c.is_positive
```




    True




```python
c.is_imaginary
```




    False



# Imaginary Numbers


```python
I
```




$$i$$




```python
I ** 2
```




$$-1$$



# `Rational()`


```python
Rational(1,3)
```




$$\frac{1}{3}$$




```python
Rational(1,3) + Rational(1,2)
```




$$\frac{5}{6}$$



# Numerical evaluation


```python
expr = Rational(1,3) + Rational(1,2)
N(expr)
```




$$0.833333333333333$$




```python
N(pi, 100)
```




$$3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068$$




```python
pi.evalf(100)
```




$$3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068$$



# `subs()`


```python
expr = x**2 + 2*x + 1
expr
```




$$x^{2} + 2 x + 1$$




```python
expr.subs(x, 1)
```




$$4$$




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




$$\pi x^{2}$$




```python
expr.subs(x, 3)
```




$$9 \pi$$




```python
N(_)
```




$$28.2743338823081$$



# `factor()` and `expand()`


```python
expr = (x + y) ** 2
expr
```




$$\left(x + y\right)^{2}$$




```python
expand(expr)
```




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




```python
factor(_)
```




$$\left(x + y\right)^{2}$$



# `simplify()`


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




$$\frac{1}{x} \left(\frac{7 x}{3} + 4\right)$$




```python
simplify(expr)
```




$$\frac{7}{3} + \frac{4}{x}$$




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




$$\frac{7}{3} + \frac{4}{x}$$




```python
expr = sin(x)/cos(x)
expr
```




$$\frac{\sin{\left (x \right )}}{\cos{\left (x \right )}}$$




```python
simplify(expr)
```




$$\tan{\left (x \right )}$$



# `apart()` and `together()`


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




$$\frac{1}{x^{2} + 2 x}$$




```python
apart(expr)
```




$$- \frac{1}{2 x + 4} + \frac{1}{2 x}$$




```python
together(_)
```




$$\frac{1}{x \left(x + 2\right)}$$



# Calculus


```python
diff(sin(x), x)
```




$$\cos{\left (x \right )}$$




```python
diff(log(x**2 + 1) + 2*x, x)
```




$$\frac{2 x}{x^{2} + 1} + 2$$




```python
integrate(cos(x), x)
```




$$\sin{\left (x \right )}$$




```python
Integral(sin(x), (x,0,pi))
```




$$\int_{0}^{\pi} \sin{\left (x \right )}\, dx$$




```python
N(_)
```




$$2.0$$



# `Sum()`


```python
expr = Sum(1/(x**2 + 2*x), (x, 1, 10))
expr
```




$$\sum_{x=1}^{10} \frac{1}{x^{2} + 2 x}$$




```python
expr.doit()
```




$$\frac{175}{264}$$



# `Product()`


```python
expr = Product(1/(x**2 + 2*x), (x, 1, 10))
expr
```




$$\prod_{x=1}^{10} \frac{1}{x^{2} + 2 x}$$




```python
expr.doit()
```




$$\frac{1}{869100503040000}$$



# `Solve()`


```python
expr = 2*x + 1
solve(expr)
```




$$\begin{bmatrix}- \frac{1}{2}\end{bmatrix}$$




```python
expr = x**2 - 1
solve(expr)
```




$$\begin{bmatrix}-1, & 1\end{bmatrix}$$




```python
expr_1 = 2*x + y + 3
expr_2 = 2*y - x
solve([expr_1, expr_2],(x,y))
```




$$\begin{Bmatrix}x : - \frac{6}{5}, & y : - \frac{3}{5}\end{Bmatrix}$$



# Units


```python
from sympy.physics import units as u
```


```python
5. * u.milligram
```




$$5.0 \cdot 10^{-6} kg$$




```python
1./2 * u.inch
```




$$0.0127 m$$




```python
1. * u.nano
```




$$1.0 \cdot 10^{-9}$$




```python
u.watt
```




$$\frac{kg m^{2}}{s^{3}}$$




```python
u.ohm
```




$$\frac{kg m^{2}}{A^{2} s^{3}}$$



## Converting from Kilometers/hours to Miles/hours


```python
kmph = u.km / u.hour
mph = u.mile / u.hour

N(mph / kmph)
```




$$1.609344$$




```python
80 * N(mph / kmph)
```




$$128.74752$$



# Working with NumPy / Pandas and Matplotlib


```python
def sympy_expr(x_val):
    expr = x**2 + sqrt(3)*x - Rational(1,3)
    return expr.subs(x, x_val)
```


```python
sympy_expr(3)
```




$$3 \sqrt{3} + \frac{26}{3}$$




```python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
```


```python
list1 = np.arange(1,1000)
list2 = pd.Series(list1)
```


```python
%timeit [sympy_expr(item) for item in list1]
%timeit [sympy_expr(item) for item in list2]
```

    1 loops, best of 3: 103 ms per loop
    10 loops, best of 3: 105 ms per loop



```python
%timeit np.vectorize(sympy_expr)(list1)
%timeit list2.apply(sympy_expr)
```

    10 loops, best of 3: 776 ms per loop
    1 loops, best of 3: 1.09 s per loop



```python
expr = x**2 + sqrt(3)*x - Rational(1,3)

lf = lambdify(x, expr)
```


```python
%timeit lf(list1)
%timeit lf(list2)
```

    1000 loops, best of 3: 242 µs per loop
    100 loops, best of 3: 4.8 ms per loop



```python
fig = plt.figure()
axes = fig.add_subplot(111)

x_vals = np.linspace(-5.,5.)
y_vals = lf(x_vals)

axes.grid()
axes.plot(x_vals, y_vals)

plt.show();
```

926edb616d641eb9e63150032fafe7e4ca5865d9
ipynb
Jupyter Notebook
.ipynb_checkpoints/sympy-checkpoint.ipynb
nkarast/notebooks
a6bd1188ada67f487560c13611c6d714cd50b887
MIT
.ipynb_checkpoints/sympy-checkpoint.ipynb
nkarast/notebooks
a6bd1188ada67f487560c13611c6d714cd50b887
MIT
.ipynb_checkpoints/sympy-checkpoint.ipynb
nkarast/notebooks
a6bd1188ada67f487560c13611c6d714cd50b887
MIT
Qwen/Qwen-72B
1. YES
2. YES