# 15.1. Diving into symbolic computing with SymPy

https://ipython-books.github.io/151-diving-into-symbolic-computing-with-sympy/

### Ref

* Math constant - https://docs.sympy.org/0.7.1/modules/mpmath/functions/constants.html


```python
from sympy import *
init_printing()
```


```python
var('x y')   # use in interactive session
```


```python
x, y = symbols('x y')  # use in script
```


```python
expr1 = (x + 1) ** 2
expr2 = x**2 + 2 * x + 1
```


```python
expr1, expr2
```


```python
expr1 == expr2
```




    False




```python
simplify(expr1 - expr2)
```


```python
simplify(expr2)
```


```python
expr1.subs(x, expr1)
```


```python
expr1.subs(x, pi)
```


```python
v = expr1.subs(x, S(1) / 2)
```


```python
type(v)
```




    sympy.core.numbers.Rational




```python
v.evalf()
```


```python
pi.evalf()               # convert sympy expression to numerical value
```


```python
exp(1).evalf()
```


```python
f = lambdify(x, expr1)   # convert sympy expression to python function
```


```python
import numpy as np
y2 = f(np.linspace(-2., 2., 5))
```


```python
type(y2)
```




    numpy.ndarray




```python
len(y2)
```


```python
type(f)
```




    function




```python
f(-1)
```

### Plot function


```python
%matplotlib inline
var('x y')
y = x**2
plot(y, (x, -4, 4))
```


```python

```

ab33549d3fd8a30039de6d81d3d7a71fff1c0a52
ipynb
Jupyter Notebook
chapter15_symbolic/01_sympy_intro.ipynb
wgong/cookbook-2nd-code
8ca2e5b3c90fee6605f4155e6b9dfb783ce46807
MIT
chapter15_symbolic/01_sympy_intro.ipynb
wgong/cookbook-2nd-code
8ca2e5b3c90fee6605f4155e6b9dfb783ce46807
MIT
chapter15_symbolic/01_sympy_intro.ipynb
wgong/cookbook-2nd-code
8ca2e5b3c90fee6605f4155e6b9dfb783ce46807
MIT
Qwen/Qwen-72B
1. YES
2. YES