> This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python.


# 15.1. Diving into symbolic computing with SymPy

SymPy is a pure Python package for symbolic mathematics.

First, we import SymPy, and enable rich display LaTeX-based printing in the IPython notebook (using the MathJax Javascript library).


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

With NumPy and the other packages we have been using so far, we were dealing with numbers and numerical arrays. With SymPy, we deal with symbolic variables. It's a radically different shift of paradigm, which mathematicians may be more familiar with.

To deal with symbolic variables, we need to declare them.


```python
var('x y')
```

The var function creates symbols and injects them into the namespace. This function should only be used in interactive mode. In a Python module, it is better to use the symbol function which returns the symbols.


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

We can create mathematical expressions with these symbols.


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

Are these expressions equal?


```python
expr1 == expr2
```

These expressions are mathematically equal, but not syntactically identical. To test whether they are equal, we can ask SymPy to simplify the difference algebraically.


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

A very common operation with symbolic expressions is substitution of a symbol by another symbol, expression, or a number.


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


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

A rational number cannot be written simply as "1/2" as this Python expression evaluates to 0. A possibility is to use a SymPy object for 1, for example using the function S.


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

Exactly-represented numbers can be evaluated numerically with evalf:


```python
_.evalf()
```

We can transform this *symbolic* function into an actual Python function that can be evaluated on NumPy arrays, using the `lambdify` function.


```python
f = lambdify(x, expr1)
```


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

> You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).

> [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages).

fe1f4e3fb883538b4ae2630c007e1ecafb9b8c24
ipynb
Jupyter Notebook
notebooks/chapter15_symbolic/01_sympy_intro.ipynb
hidenori-t/cookbook-code
750f546ed87b09d28532884b8074b96cd8d32a38
BSD-2-Clause
2015-01-01T18:15:54.000Z
2022-03-06T16:15:07.000Z
notebooks/chapter15_symbolic/01_sympy_intro.ipynb
bndxn/cookbook-code
90c31341edccf039187e6a3809fb336f83bb758f
BSD-2-Clause
2015-02-25T22:08:09.000Z
2018-09-28T08:41:38.000Z
notebooks/chapter15_symbolic/01_sympy_intro.ipynb
bndxn/cookbook-code
90c31341edccf039187e6a3809fb336f83bb758f
BSD-2-Clause
2015-01-02T13:53:11.000Z
2022-03-18T21:05:16.000Z
Qwen/Qwen-72B
1. YES
2. YES