# Examples of  how to use SymPy

SymPy is a Python package for symbolic manipulation. In this notebook we look at a few examples of useful operations, such as differentiation, integration, simplification and solving polynomials.

SymPy suppports many more operations. The documentation can be found at: https://docs.sympy.org/latest/index.html


```python
from sympy import *
```

You have to tell SymPy which variables are to be treated as symbols. In this notebook we'll make `x`, `y` and `z` symbols.


```python
x, y, z = symbols('x y z')
```

## Defining equations and displaying them

We can now write expressions, and assign them to a variable.


```python
a = (x + x**2)/(x*sin(y)**2 + x*cos(y)**2)
a
```




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




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




$\displaystyle x^{2} + 1$



Simple simplifications are automatically applied


```python
b - 1
```




$\displaystyle x^{2}$



If you define multiple expressions in a cell you can display them with the `display()` command


```python
a = x**2 + x
display(a)

b = 2*x + 3
display(b)

display(a+b)
```


$\displaystyle x^{2} + x$



$\displaystyle 2 x + 3$



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


## Simplification

In simple cases SymPy will automatically simplify. More complicated expressions can simplified using the `simplify()` command


```python
a = (x-2)*(x+3)/((x-2)*(x-9))
display(a)

a = (x**2 +x -6)/(x**2 - 11*x+18)
display(a)
simplify(a)
```


$\displaystyle \frac{x + 3}{x - 9}$



$\displaystyle \frac{x^{2} + x - 6}{x^{2} - 11 x + 18}$





$\displaystyle \frac{x + 3}{x - 9}$



`simplify()` will also use trigonometric identities


```python
a = (x + x**2)/(x*sin(y)**2 + x*cos(y)**2)
display(a)
display(simplify(a))
```


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



$\displaystyle x + 1$


## Differentiation
Expressions can be differentiated using the `diff()` command. The first argument is the expression and the second argument is the variable you want to differential with respect too.


```python
a = x**2 + 3*x + 4*y**2
display(diff(a,x))
display(diff(a,y))
```


$\displaystyle 2 x + 3$



$\displaystyle 8 y$


You can also use trigonometric functions, such as `sin()`, `cos()`, `sinh()`, and their inverses `asin()`, `acos()`, `asinh()` etc


```python
diff(asinh(x),x)
```




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



It's easy to get complicated expressions. It is often useful to simplify the result.


```python
res = diff((x**2 +x -6)/(x**2 - 11*x+18))
display(res)
display(simplify(res))
```


$\displaystyle \frac{\left(11 - 2 x\right) \left(x^{2} + x - 6\right)}{\left(x^{2} - 11 x + 18\right)^{2}} + \frac{2 x + 1}{x^{2} - 11 x + 18}$



$\displaystyle - \frac{12}{x^{2} - 18 x + 81}$


## Integration

You can also integrate using the `integrate()` command.


```python
a = x/(x**2+2*x+1)
display(a)

integrate(a, x)
```


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





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



You can also do definite integration:


```python
b = integrate(sin(x), (x,0,3))
display(b)
```


$\displaystyle 1 - \cos{\left(3 \right)}$


SymPy will give the exact result. You can numerically evaluate it using `N()`. You can pass a second argument for the precision you wish to evaluate the result to.


```python
display(N(b))
display(N(b, 50))
```


$\displaystyle 1.98999249660045$



$\displaystyle 1.9899924966004454572715727947312613023936790966156$


If SymPy is not sure how to perform the integral it will just return it an unevaluated expression:


```python
integrate(cos(x)/(x**2+1),(x,1,5))
```




$\displaystyle \int\limits_{1}^{5} \frac{\cos{\left(x \right)}}{x^{2} + 1}\, dx$



## Solving equations

SymPy can also solve some equations, such as polynomials.


```python
solveset((x-2)*(x+3), x)
```




$\displaystyle \left\{-3, 2\right\}$




```python
solveset(x**2 +x -1, x)
```




$\displaystyle \left\{- \frac{1}{2} + \frac{\sqrt{5}}{2}, - \frac{\sqrt{5}}{2} - \frac{1}{2}\right\}$



Here a polynomial we encountered whilst looking for period-2 orbits related to root finding:

$$ 16 x_p^9 - x_p(3 x_p^2 - 1)\left[12x_p^6 - (3x_p^2 -1)^2\right] = 0$$

Let's find the roots


```python
solveset(16*x**9 - x*(3*x**2 - 1)*(12*x**6 - (3*x**2-1)**2),x)
```




$\displaystyle \left\{-1, 0, 1, - \frac{\sqrt{5}}{5}, \frac{\sqrt{5}}{5}, - \frac{\sqrt{2} \cos{\left(\frac{\operatorname{atan}{\left(\frac{\sqrt{7}}{3} \right)}}{2} \right)}}{2} - \frac{\sqrt{2} i \sin{\left(\frac{\operatorname{atan}{\left(\frac{\sqrt{7}}{3} \right)}}{2} \right)}}{2}, - \frac{\sqrt{2} \cos{\left(\frac{\operatorname{atan}{\left(\frac{\sqrt{7}}{3} \right)}}{2} \right)}}{2} + \frac{\sqrt{2} i \sin{\left(\frac{\operatorname{atan}{\left(\frac{\sqrt{7}}{3} \right)}}{2} \right)}}{2}, \frac{\sqrt{2} \cos{\left(\frac{\operatorname{atan}{\left(\frac{\sqrt{7}}{3} \right)}}{2} \right)}}{2} - \frac{\sqrt{2} i \sin{\left(\frac{\operatorname{atan}{\left(\frac{\sqrt{7}}{3} \right)}}{2} \right)}}{2}, \frac{\sqrt{2} \cos{\left(\frac{\operatorname{atan}{\left(\frac{\sqrt{7}}{3} \right)}}{2} \right)}}{2} + \frac{\sqrt{2} i \sin{\left(\frac{\operatorname{atan}{\left(\frac{\sqrt{7}}{3} \right)}}{2} \right)}}{2}\right\}$



## Derive the weights for Simpson's method

Recall that Lagrange Polynomial through points $f(a), f(b), f(c)$ is given by 

$$P_2(x) = f(a)\frac{(x-b)(x-c)}{(a-b)(a-c)} + f(b)\frac{(x-a)(x-c)}{(b-a)(b-c)} + f(c)\frac{(x-a)(x-b)}{(c-a)(c-b)}$$

We integrate this to get Simpson's method:

$$ \int^b_a P_2(x) \, dx = \frac{b-a}{6}\left[f(a) + 4 f\left(\frac{a+b}{2}\right) + f(b)\right]$$

Let's do this integration with SymPy


```python
a, b, c = symbols('a b c')
f = Function('f')
```


```python
c = (a+b)/2

P2 = f(a)*(x-b)*(x-c)/((a-b)*(a-c)) + f(b)*(x-a)*(x-c)/((b-a)*(b-c)) + f(c)*(x-a)*(x-b)/((c-a)*(c-b))
```


```python
integral = integrate(P2,(x,a,b))
display(integral)
```


$\displaystyle - \frac{a^{3} \left(2 f{\left(a \right)} + 2 f{\left(b \right)} - 4 f{\left(\frac{a}{2} + \frac{b}{2} \right)}\right)}{3 a^{2} - 6 a b + 3 b^{2}} + \frac{a^{2} \left(a f{\left(a \right)} + 3 a f{\left(b \right)} - 4 a f{\left(\frac{a}{2} + \frac{b}{2} \right)} + 3 b f{\left(a \right)} + b f{\left(b \right)} - 4 b f{\left(\frac{a}{2} + \frac{b}{2} \right)}\right)}{2 a^{2} - 4 a b + 2 b^{2}} - \frac{a \left(a^{2} f{\left(b \right)} + a b f{\left(a \right)} + a b f{\left(b \right)} - 4 a b f{\left(\frac{a}{2} + \frac{b}{2} \right)} + b^{2} f{\left(a \right)}\right)}{a^{2} - 2 a b + b^{2}} + \frac{b^{3} \left(2 f{\left(a \right)} + 2 f{\left(b \right)} - 4 f{\left(\frac{a}{2} + \frac{b}{2} \right)}\right)}{3 a^{2} - 6 a b + 3 b^{2}} - \frac{b^{2} \left(a f{\left(a \right)} + 3 a f{\left(b \right)} - 4 a f{\left(\frac{a}{2} + \frac{b}{2} \right)} + 3 b f{\left(a \right)} + b f{\left(b \right)} - 4 b f{\left(\frac{a}{2} + \frac{b}{2} \right)}\right)}{2 a^{2} - 4 a b + 2 b^{2}} + \frac{b \left(a^{2} f{\left(b \right)} + a b f{\left(a \right)} + a b f{\left(b \right)} - 4 a b f{\left(\frac{a}{2} + \frac{b}{2} \right)} + b^{2} f{\left(a \right)}\right)}{a^{2} - 2 a b + b^{2}}$



```python
res = simplify(integral)
display(res)
```


$\displaystyle - \frac{a f{\left(a \right)}}{6} - \frac{a f{\left(b \right)}}{6} - \frac{2 a f{\left(\frac{a}{2} + \frac{b}{2} \right)}}{3} + \frac{b f{\left(a \right)}}{6} + \frac{b f{\left(b \right)}}{6} + \frac{2 b f{\left(\frac{a}{2} + \frac{b}{2} \right)}}{3}$



```python
factor(res)
```




$\displaystyle - \frac{\left(a - b\right) \left(f{\left(a \right)} + f{\left(b \right)} + 4 f{\left(\frac{a}{2} + \frac{b}{2} \right)}\right)}{6}$




```python

```

b347fce381584ec21db58f40fff2c1b662644c8f
ipynb
Jupyter Notebook
SymbolicManipulation/SymPyExamples.ipynb
gerryb123/nielsexamples
ca1247475d94a0fcdf07ee4b37b58b70c69ca207
MIT
2020-02-15T21:30:37.000Z
2021-09-21T12:03:13.000Z
SymbolicManipulation/SymPyExamples.ipynb
gerryb123/nielsexamples
ca1247475d94a0fcdf07ee4b37b58b70c69ca207
MIT
SymbolicManipulation/SymPyExamples.ipynb
gerryb123/nielsexamples
ca1247475d94a0fcdf07ee4b37b58b70c69ca207
MIT
2020-02-13T14:27:47.000Z
2022-02-05T14:17:10.000Z
Qwen/Qwen-72B
1. YES
2. YES