# SymPy Basics
Adapted from: https://github.com/sympy/sympy/wiki/Quick-examples


```python
from sympy import *
from IPython.display import display
init_printing(order="lex",use_latex='mathjax')
```

# Symbolic Expressions and Calculations


```python
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
#f, g, h = map(Function, 'fgh')
```


```python
eqn = Rational(3,2)*pi + exp(I*x) / (x**2 + y)
eqn
```




$$\frac{3 \pi}{2} + \frac{e^{i x}}{x^{2} + y}$$




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




$$\frac{3 \pi}{2} + \frac{e^{3 i}}{y + 9}$$




```python
exp(I*x).subs(x,pi).evalf()
```




$$-1.0$$




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




$$\left ( x, \quad 2 y\right )$$




```python
exp(pi * sqrt(163)).evalf(50)
```




$$262537412640768743.99999999999925007259719818568888$$




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




$$3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068$$




```python
latex(S(eqn,evaluate=False))
```




    '\\frac{3 \\pi}{2} + \\frac{e^{i x}}{x^{2} + y}'



$$ \frac{3 \pi}{2} + \frac{e^{i x}}{x^{2} + y}$$

## Algebra


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




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




```python
a = 1/x + (x*sin(x) - 1)/x
a
```




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




```python
a.simplify()
```




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




```python
eqn = Eq(x**3 + 2*x**2 + 4*x + 8, 0)
eqn
```




$$x^{3} + 2 x^{2} + 4 x + 8 = 0$$




```python
solve(eqn,x)
```




$$\left [ -2, \quad - 2 i, \quad 2 i\right ]$$




```python
eq1 = Eq(x + 5*y, 2)
eq2 = Eq(-3*x + 6*y, 15)
display(eq1)
display(eq2)
sln = solve([eq1, eq2], [x, y])
sln
```


$$x + 5 y = 2$$



$$- 3 x + 6 y = 15$$





$$\left \{ x : -3, \quad y : 1\right \}$$




```python
display(eq1.subs(sln))
display(eq2.subs(sln))
```


$$\mathrm{True}$$



$$\mathrm{True}$$


## Recurrence Relations

$$
\large\begin{align}
y_0 & =1 \\
y_1 & =4 \\
y_n & =y_n-2y_{n-1}+5y_{n-2} 
\end{align}
$$


```python
f=y(n)-2*y(n-1)-5*y(n-2)
f
```




$$y{\left (n \right )} - 5 y{\left (n - 2 \right )} - 2 y{\left (n - 1 \right )}$$




```python
sln = rsolve(f,y(n),[1,4])
sln
```




$$\left(\frac{1}{2} + \frac{\sqrt{6}}{4}\right) \left(1 + \sqrt{6}\right)^{n} + \left(- \sqrt{6} + 1\right)^{n} \left(- \frac{\sqrt{6}}{4} + \frac{1}{2}\right)$$




```python
for i in range(0,10):
    print(sln.subs(n,i).simplify())
```

    1
    4
    13
    46
    157
    544
    1873
    6466
    22297
    76924


## Sums and Products


```python
a, b = symbols('a b')
s = Sum(6*n**2 + 2**n, (n, a, b))
s
```




$$\sum_{n=a}^{b} \left(2^{n} + 6 n^{2}\right)$$




```python
s.doit()
```




$$- 2^{a} + 2^{b + 1} - 2 a^{3} + 3 a^{2} - a + 2 b^{3} + 3 b^{2} + b$$




```python
s.subs({b:3,a:1}).doit()
```




$$98$$




```python
Sum(b, (b, 1, n)).doit().factor()
```




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




```python
Sum(n*(n+1)/2,(n, 1, b)).doit()
```




$$\frac{b^{3}}{6} + \frac{b^{2}}{2} + \frac{b}{3}$$




```python
for i in range(1,10):
    print(Sum(n*(n+1)/2, (n, 1, b)).doit().subs(b,i))
```

    1
    4
    10
    20
    35
    56
    84
    120
    165



```python
Sum(n, (n, a, b)).subs(a,1).doit()
```




$$\frac{b^{2}}{2} + \frac{b}{2}$$




```python
(x**3/6 + x**2/2 +x/3).factor()
```




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




```python
product(n*(n+1), (n, 1, b))
```




$${2}^{\left(b\right)} b!$$




```python
f=Function('f')
ex=Eq(f(1/x)-3*f(x),x)
```

## Calculus

$$\lim_{x\to 0} \frac{\sin x - x}{x^3} = -\frac{1}{6}$$


```python
((sin(x)-x)/x**3).limit(x,0)
```




$$- \frac{1}{6}$$




```python
(x**2+5*x**3).diff(x)
```




$$15 x^{2} + 2 x$$




```python
(-x).limit(x,oo)
```




$$-\infty$$



$$\int x^2 \cos x  \ dx$$


```python
(x**2 * cos(x)).integrate(x)
```




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



$$\int_0^{\pi/2} x^2 \cos x \ dx$$


```python
integrate(x**2 * cos(x), (x, 0, pi/2))
##(x**2 * cos(x)).integrate(x, 0, pi/2) does not work. 
```




$$-2 + \frac{\pi^{2}}{4}$$



$$  \large f''(x) + 9 f(x) = 1 $$


```python
fn = dsolve(Eq(Derivative(f(x),x,x) + 9*f(x), 1), f(x))
fn
```




$$f{\left (x \right )} = C_{1} \sin{\left (3 x \right )} + C_{2} \cos{\left (3 x \right )} + \frac{1}{9}$$




```python
fla = 3*sin(3*x)+3*cos(3*x)+1/9
fla.diff(x).diff(x).subs(x,3)+9*fla.subs(x,3)
```




$$1.0$$



## Linear Algebra 

a55c5474aee9966b0ac15d9c005a6ccf83b4f64d
ipynb
Jupyter Notebook
Notebooks/SymPy Basics.ipynb
Andrewnetwork/WorkshopScipy
739d24b9078fffb84408e7877862618d88d947dc
MIT
2017-12-16T20:50:07.000Z
2021-11-08T13:05:57.000Z
Notebooks/SymPy Basics.ipynb
Andrewnetwork/WorkshopScipy
739d24b9078fffb84408e7877862618d88d947dc
MIT
2017-12-17T06:10:28.000Z
2018-11-14T15:50:10.000Z
Notebooks/SymPy Basics.ipynb
Andrewnetwork/WorkshopScipy
739d24b9078fffb84408e7877862618d88d947dc
MIT
2017-12-06T20:40:09.000Z
2019-06-01T11:33:57.000Z
Qwen/Qwen-72B
1. YES
2. YES