```python
from __future__ import division
from sympy import *

x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)

init_printing(use_latex='mathjax') # this makes the output look like real math symbols
```


```python
diff(x**4 + cos(x**2), x)
```




$$4 x^{3} - 2 x \sin{\left (x^{2} \right )}$$




```python
integrate(x**4+ cos(x**2), x)
```




$$\frac{x^{5}}{5} + \frac{\sqrt{2} \sqrt{\pi} C\left(\frac{\sqrt{2} x}{\sqrt{\pi}}\right) \Gamma{\left(\frac{1}{4} \right)}}{8 \Gamma{\left(\frac{5}{4} \right)}}$$




```python
test = integrate(x**4+ cos(x**2)*sin(x**2), x)
```


```python
test
```




$$\frac{x^{5}}{5} - \frac{3 \sqrt{\pi} S\left(\frac{2 x}{\sqrt{\pi}}\right)}{16 \Gamma{\left(\frac{1}{4} \right)}} \Gamma{\left(- \frac{3}{4} \right)}$$




```python
sqrt(8)
```




$$2 \sqrt{2}$$




```python
x+y**2
```




$$x + y^{2}$$




```python
I**2
```




$$-1$$




```python
Rational(1, 55)
```




$$\frac{1}{55}$$




```python
rat = Rational(1, 55) + Rational(58, 327)
rat
```




$$\frac{3517}{17985}$$



### N() does a numarica eval


```python
N(rat)
```




$$0.195551848762858$$




```python
N(pi, 1000) # 1000 is how many decimals I want
```




$$3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198$$




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




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




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




$$121$$




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




$$\pi x^{2}$$




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




$$100 \pi$$




```python
N(_)
```




$$314.159265358979$$




```python
expr = (x+y + 5)**10
expr
```




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




```python
expand(expr)
```




$$x^{10} + 10 x^{9} y + 50 x^{9} + 45 x^{8} y^{2} + 450 x^{8} y + 1125 x^{8} + 120 x^{7} y^{3} + 1800 x^{7} y^{2} + 9000 x^{7} y + 15000 x^{7} + 210 x^{6} y^{4} + 4200 x^{6} y^{3} + 31500 x^{6} y^{2} + 105000 x^{6} y + 131250 x^{6} + 252 x^{5} y^{5} + 6300 x^{5} y^{4} + 63000 x^{5} y^{3} + 315000 x^{5} y^{2} + 787500 x^{5} y + 787500 x^{5} + 210 x^{4} y^{6} + 6300 x^{4} y^{5} + 78750 x^{4} y^{4} + 525000 x^{4} y^{3} + 1968750 x^{4} y^{2} + 3937500 x^{4} y + 3281250 x^{4} + 120 x^{3} y^{7} + 4200 x^{3} y^{6} + 63000 x^{3} y^{5} + 525000 x^{3} y^{4} + 2625000 x^{3} y^{3} + 7875000 x^{3} y^{2} + 13125000 x^{3} y + 9375000 x^{3} + 45 x^{2} y^{8} + 1800 x^{2} y^{7} + 31500 x^{2} y^{6} + 315000 x^{2} y^{5} + 1968750 x^{2} y^{4} + 7875000 x^{2} y^{3} + 19687500 x^{2} y^{2} + 28125000 x^{2} y + 17578125 x^{2} + 10 x y^{9} + 450 x y^{8} + 9000 x y^{7} + 105000 x y^{6} + 787500 x y^{5} + 3937500 x y^{4} + 13125000 x y^{3} + 28125000 x y^{2} + 35156250 x y + 19531250 x + y^{10} + 50 y^{9} + 1125 y^{8} + 15000 y^{7} + 131250 y^{6} + 787500 y^{5} + 3281250 y^{4} + 9375000 y^{3} + 17578125 y^{2} + 19531250 y + 9765625$$




```python
factor(_)
```




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




```python
new_expr = integrate(_, x)
new_expr
```




$$\frac{x^{12}}{132} + x^{11} \left(\frac{y}{11} + \frac{5}{11}\right) + x^{10} \left(\frac{y^{2}}{2} + 5 y + \frac{25}{2}\right) + x^{9} \left(\frac{5 y^{3}}{3} + 25 y^{2} + 125 y + \frac{625}{3}\right) + x^{8} \left(\frac{15 y^{4}}{4} + 75 y^{3} + \frac{1125 y^{2}}{2} + 1875 y + \frac{9375}{4}\right) + x^{7} \left(6 y^{5} + 150 y^{4} + 1500 y^{3} + 7500 y^{2} + 18750 y + 18750\right) + x^{6} \left(7 y^{6} + 210 y^{5} + 2625 y^{4} + 17500 y^{3} + 65625 y^{2} + 131250 y + 109375\right) + x^{5} \left(6 y^{7} + 210 y^{6} + 3150 y^{5} + 26250 y^{4} + 131250 y^{3} + 393750 y^{2} + 656250 y + 468750\right) + x^{4} \left(\frac{15 y^{8}}{4} + 150 y^{7} + 2625 y^{6} + 26250 y^{5} + \frac{328125 y^{4}}{2} + 656250 y^{3} + 1640625 y^{2} + 2343750 y + \frac{5859375}{4}\right) + x^{3} \left(\frac{5 y^{9}}{3} + 75 y^{8} + 1500 y^{7} + 17500 y^{6} + 131250 y^{5} + 656250 y^{4} + 2187500 y^{3} + 4687500 y^{2} + 5859375 y + \frac{9765625}{3}\right) + x^{2} \left(\frac{y^{10}}{2} + 25 y^{9} + \frac{1125 y^{8}}{2} + 7500 y^{7} + 65625 y^{6} + 393750 y^{5} + 1640625 y^{4} + 4687500 y^{3} + \frac{17578125 y^{2}}{2} + 9765625 y + \frac{9765625}{2}\right)$$




```python
temp = new_expr.subs(x, 1)
temp.subs(y, 2)
```




$$\frac{10383422873}{44}$$




```python

```




$$\frac{y^{10}}{2} + \frac{80 y^{9}}{3} + \frac{2565 y^{8}}{4} + 9156 y^{7} + 85967 y^{6} + 554616 y^{5} + \frac{9959865 y^{4}}{4} + \frac{23044730 y^{3}}{3} + \frac{31169301 y^{2}}{2} + \frac{206547056 y}{11} + \frac{1346704211}{132}$$




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




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




```python
simplify(_)
```




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




```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)}$$




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




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




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




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




```python
N(_)
```




$$2.0$$




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




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




```python
N(_)
```




$$0.662878787878788$$




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




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




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




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




$$2 x + 10$$




```python
solve(expr)
```




$$\left [ -5\right ]$$




```python
expr = x**2 - 10
expr

```




$$x^{2} - 10$$




```python
solve(expr)
```




$$\left [ - \sqrt{10}, \quad \sqrt{10}\right ]$$



### Solve systems of equations!!! :D


```python
expr_1 = 2*x + y
expr_2 = 2*y - x + 21
```


```python
solve([expr_1, expr_2], (x, y))
```




$$\left \{ x : \frac{21}{5}, \quad y : - \frac{42}{5}\right \}$$



# Units 


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


```python
5. * u.microgram
```




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




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




$$0.00508 m$$




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




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




```python
u.watt
```




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




```python
1. * u.watts
```




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




```python

```




$$\frac{kg}{602214179000000000000000000}$$




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

N(mph/kmph)
```




$$1.609344$$



# Plotting


```python
from sympy.plotting import plot, plot_parametric
```


```python
expr = sin(2*sin(x**3))
plot(expr, (x, 0, 5))
```


```python
expr = diff(x**5 + sin(x), x)
```


```python
expr
```




$$5 x^{4} + \cos{\left (x \right )}$$




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


```python
def sympy_expr(x_val):
    expr = x**2 + sqrt(3)*x - Rational(1, 3)
    return expr.subs(x, x_val)
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 loop, best of 3: 618 ms per loop
    1 loop, best of 3: 633 ms per loop



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

    1 loop, best of 3: 455 ms per loop
    1 loop, best of 3: 450 ms per loop



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


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

    The slowest run took 43.16 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 7.35 µs per loop
    1000 loops, best of 3: 282 µs 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()
```


```python

```

d5a7144740110445a434027fcfb18693e44e16b0
ipynb
Jupyter Notebook
jupyter-notebooks/miscellaneous/Sympy intro.ipynb
alexwhb/algorithm-practice
b15cffcefbd2b68ad280dd37ef1afc0305bf9d62
MIT
2015-10-29T01:51:48.000Z
2015-10-29T01:51:48.000Z
jupyter-notebooks/miscellaneous/Sympy intro.ipynb
alexwhb/algorithm-practice
b15cffcefbd2b68ad280dd37ef1afc0305bf9d62
MIT
jupyter-notebooks/miscellaneous/Sympy intro.ipynb
alexwhb/algorithm-practice
b15cffcefbd2b68ad280dd37ef1afc0305bf9d62
MIT
Qwen/Qwen-72B
1. YES
2. YES