# Sympy - Symbolic algebra in Python

J.R. Johansson (robert@riken.jp) http://dml.riken.jp/~rob/

The latest version of this [IPython notebook](http://ipython.org/notebook.html) lecture is available at [http://github.com/jrjohansson/scientific-python-lectures](http://github.com/jrjohansson/scientific-python-lectures).

The other notebooks in this lecture series are indexed at [http://jrjohansson.github.com](http://jrjohansson.github.com).


```
%pylab inline
```

    
    Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.kernel.zmq.pylab.backend_inline].
    For more information, type 'help(pylab)'.


## Introduction

There are two notable Computer Algebra Systems (CAS) for Python:

* [SymPy](http://sympy.org/en/index.html) - A python module that can be used in any Python program, or in an IPython session, that provides powerful CAS features. 
* [Sage](http://www.sagemath.org/) - Sage is a full-featured and very powerful CAS enviroment that aims to provide an open source system that competes with Mathematica and Maple. Sage is not a regular Python module, but rather a CAS environment that uses Python as its programming language.

Sage is in some aspects more powerful than SymPy, but both offer very comprehensive CAS functionality. The advantage of SymPy is that it is a regular Python module and integrates well with the IPython notebook. 

In this lecture we will therefore look at how to use SymPy with IPython notebooks. If you are interested in an open source CAS environment I also recommend to read more about Sage.

To get started using SymPy in a Python program or notebook, import the module `sympy`:


```
from sympy import *
```

To get nice-looking $\LaTeX$ formatted output run:


```
init_printing()

# or with older versions of sympy/ipython, load the IPython extension
#%load_ext sympy.interactive.ipythonprinting
# or
#%load_ext sympyprinting
```

## Symbolic variables

In SymPy we need to create symbols for the variables we want to work with. We can create a new symbol using the `Symbol` class:


```
x = Symbol('x')
```


```
(pi + x)**2
```




$$\left(x + \pi\right)^{2}$$




```
# alternative way of defining symbols
a, b, c = symbols("a, b, c")
```


```
type(a)
```




    sympy.core.symbol.Symbol



We can add assumptions to symbols when we create them:


```
x = Symbol('x', real=True)
```


```
x.is_imaginary
```




$$False$$




```
x = Symbol('x', positive=True)
```


```
x > 0
```




$$True$$



### Complex numbers

The imaginary unit is denoted `I` in Sympy. 


```
1+1*I
```




$$1 + i$$




```
I**2
```




$$-1$$




```
(x * I + 1)**2
```




$$\left(i x + 1\right)^{2}$$



### Rational numbers

There are three different numerical types in SymPy: `Real`, `Rational`, `Integer`: 


```
r1 = Rational(4,5)
r2 = Rational(5,4)
```


```
r1
```




$$\frac{4}{5}$$




```
r1+r2
```




$$\frac{41}{20}$$




```
r1/r2
```




$$\frac{16}{25}$$



## Numerical evaluation

SymPy uses a library for artitrary precision as numerical backend, and has predefined SymPy expressions for a number of mathematical constants, such as: `pi`, `e`, `oo` for infinity.

To evaluate an expression numerically we can use the `evalf` function (or `N`). It takes an argument `n` which specifies the number of significant digits.


```
pi.evalf(n=50)
```




$$3.1415926535897932384626433832795028841971693993751$$




```
y = (x + pi)**2
```


```
N(y, 5) # same as evalf
```




$$\left(x + 3.1416\right)^{2}$$



When we numerically evaluate algebraic expressions we often want to substitute a symbol with a numerical value. In SymPy we do that using the `subs` function:


```
y.subs(x, 1.5)
```




$$\left(1.5 + \pi\right)^{2}$$




```
N(y.subs(x, 1.5))
```




$$21.5443823618587$$



The `subs` function can of course also be used to substitute Symbols and expressions:


```
y.subs(x, a+pi)
```




$$\left(a + 2 \pi\right)^{2}$$



We can also combine numerical evolution of expressions with NumPy arrays:


```
import numpy
```


```
x_vec = numpy.arange(0, 10, 0.1)
```


```
y_vec = numpy.array([N(((x + pi)**2).subs(x, xx)) for xx in x_vec])
```


```
fig, ax = subplots()
ax.plot(x_vec, y_vec);
```

However, this kind of numerical evolution can be very slow, and there is a much more efficient way to do it: Use the function `lambdify` to "compile" a Sympy expression into a function that is much more efficient to evaluate numerically:


```
f = lambdify([x], (x + pi)**2, 'numpy')  # the first argument is a list of variables that
                                         # f will be a function of: in this case only x -> f(x)
```


```
y_vec = f(x_vec)  # now we can directly pass a numpy array and f(x) is efficiently evaluated
```

The speedup when using "lambdified" functions instead of direct numerical evaluation can be significant, often several orders of magnitude. Even in this simple example we get a significant speed up:


```
%%timeit

y_vec = numpy.array([N(((x + pi)**2).subs(x, xx)) for xx in x_vec])
```

    10 loops, best of 3: 20.4 ms per loop



```
%%timeit

y_vec = f(x_vec)
```

    100000 loops, best of 3: 3.67 µs per loop


## Algebraic manipulations

One of the main uses of an CAS is to perform algebraic manipulations of expressions. For example, we might want to expand a product, factor an expression, or simply an expression. The functions for doing these basic operations in SymPy are demonstrated in this section.

### Expand and factor

The first steps in an algebraic manipulation 


```
(x+1)*(x+2)*(x+3)
```




$$\left(x + 1\right) \left(x + 2\right) \left(x + 3\right)$$




```
expand((x+1)*(x+2)*(x+3))
```




$$x^{3} + 6 x^{2} + 11 x + 6$$



The `expand` function takes a number of keywords arguments which we can tell the functions what kind of expansions we want to have performed. For example, to expand trigonometric expressions, use the `trig=True` keyword argument:


```
sin(a+b)
```




$$\sin{\left (a + b \right )}$$




```
expand(sin(a+b), trig=True)
```




$$\sin{\left (a \right )} \cos{\left (b \right )} + \sin{\left (b \right )} \cos{\left (a \right )}$$



See `help(expand)` for a detailed explanation of the various types of expansions the `expand` functions can perform.

The opposite a product expansion is of course factoring. The factor an expression in SymPy use the `factor` function: 


```
factor(x**3 + 6 * x**2 + 11*x + 6)
```




$$\left(x + 1\right) \left(x + 2\right) \left(x + 3\right)$$



### Simplify

The `simplify` tries to simplify an expression into a nice looking expression, using various techniques. More specific alternatives to the `simplify` functions also exists: `trigsimp`, `powsimp`, `logcombine`, etc. 

The basic usages of these functions are as follows:


```
# simplify expands a product
simplify((x+1)*(x+2)*(x+3))
```




$$\left(x + 1\right) \left(x + 2\right) \left(x + 3\right)$$




```
# simplify uses trigonometric identities
simplify(sin(a)**2 + cos(a)**2)
```




$$1$$




```
simplify(cos(x)/sin(x))
```




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



### apart and together

To manipulate symbolic expressions of fractions, we can use the `apart` and `together` functions:


```
f1 = 1/((a+1)*(a+2))
```


```
f1
```




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




```
apart(f1)
```




$$- \frac{1}{a + 2} + \frac{1}{a + 1}$$




```
f2 = 1/(a+2) + 1/(a+3)
```


```
f2
```




$$\frac{1}{a + 3} + \frac{1}{a + 2}$$




```
together(f2)
```




$$\frac{2 a + 5}{\left(a + 2\right) \left(a + 3\right)}$$



Simplify usually combines fractions but does not factor: 


```
simplify(f2)
```




$$\frac{2 a + 5}{\left(a + 2\right) \left(a + 3\right)}$$



## Calculus

In addition to algebraic manipulations, the other main use of CAS is to do calculus, like derivatives and integrals of algebraic expressions.

### Differentiation

Differentiation is usually simple. Use the `diff` function. The first argument is the expression to take the derivative of, and the second argument is the symbol by which to take the derivative:


```
y
```




$$\left(x + \pi\right)^{2}$$




```
diff(y**2, x)
```




$$4 \left(x + \pi\right)^{3}$$



For higher order derivatives we can do:


```
diff(y**2, x, x)
```




$$12 \left(x + \pi\right)^{2}$$




```
diff(y**2, x, 2) # same as above
```




$$12 \left(x + \pi\right)^{2}$$



To calculate the derivative of a multivariate expression, we can do:


```
x, y, z = symbols("x,y,z")
```


```
f = sin(x*y) + cos(y*z)
```

$\frac{d^3f}{dxdy^2}$


```
diff(f, x, 1, y, 2)
```




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



## Integration

Integration is done in a similar fashion:


```
f
```




$$\sin{\left (x y \right )} + \cos{\left (y z \right )}$$




```
integrate(f, x)
```




$$x \cos{\left (y z \right )} + \begin{cases} 0 & \text{for}\: y = 0 \\- \frac{\cos{\left (x y \right )}}{y} & \text{otherwise} \end{cases}$$



By providing limits for the integration variable we can evaluate definite integrals:


```
integrate(f, (x, -1, 1))
```




$$2 \cos{\left (y z \right )}$$



and also improper integrals


```
integrate(exp(-x**2), (x, -oo, oo))
```




$$\sqrt{\pi}$$



Remember, `oo` is the SymPy notation for inifinity.

### Sums and products

We can evaluate sums and products using the functions: 'Sum'


```
n = Symbol("n")
```


```
Sum(1/n**2, (n, 1, 10))
```




$$\sum_{n=1}^{10} n^{-2}$$




```
Sum(1/n**2, (n,1, 10)).evalf()
```




$$1.54976773116654$$




```
Sum(1/n**2, (n, 1, oo)).evalf()
```




$$1.64493406684823$$



Products work much the same way:


```
Product(n, (n, 1, 10)) # 10!
```




$$\prod_{n=1}^{10} n$$



## Limits

Limits can be evaluated using the `limit` function. For example, 


```
limit(sin(x)/x, x, 0)
```




$$1$$



We can use 'limit' to check the result of derivation using the `diff` function:


```
f
```




$$\sin{\left (x y \right )} + \cos{\left (y z \right )}$$




```
diff(f, x)
```




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



$\displaystyle \frac{\mathrm{d}f(x,y)}{\mathrm{d}x} = \frac{f(x+h,y)-f(x,y)}{h}$


```
h = Symbol("h")
```


```
limit((f.subs(x, x+h) - f)/h, h, 0)
```




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



OK!

We can change the direction from which we approach the limiting point using the `dir` keywork argument:


```
limit(1/x, x, 0, dir="+")
```




$$\infty$$




```
limit(1/x, x, 0, dir="-")
```




$$-\infty$$



## Series

Series expansion is also one of the most useful features of a CAS. In SymPy we can perform a series expansion of an expression using the `series` function:


```
series(exp(x), x)
```




$$1 + x + \frac{1}{2} x^{2} + \frac{1}{6} x^{3} + \frac{1}{24} x^{4} + \frac{1}{120} x^{5} + \mathcal{O}\left(x^{6}\right)$$



By default it expands the expression around $x=0$, but we can expand around any value of $x$ by explicitly include a value in the function call:


```
series(exp(x), x, 1)
```




$$e + e x + \frac{1}{2} e x^{2} + \frac{1}{6} e x^{3} + \frac{1}{24} e x^{4} + \frac{1}{120} e x^{5} + \mathcal{O}\left(x^{6}\right)$$



And we can explicitly define to which order the series expansion should be carried out:


```
series(exp(x), x, 1, 10)
```




$$e + e x + \frac{1}{2} e x^{2} + \frac{1}{6} e x^{3} + \frac{1}{24} e x^{4} + \frac{1}{120} e x^{5} + \frac{1}{720} e x^{6} + \frac{1}{5040} e x^{7} + \frac{1}{40320} e x^{8} + \frac{1}{362880} e x^{9} + \mathcal{O}\left(x^{10}\right)$$



The series expansion includes the order of the approximation, which is very useful for keeping track of the order of validity when we do calculations with series expansions of different order:


```
s1 = cos(x).series(x, 0, 5)
s1
```




$$1 - \frac{1}{2} x^{2} + \frac{1}{24} x^{4} + \mathcal{O}\left(x^{5}\right)$$




```
s2 = sin(x).series(x, 0, 2)
s2
```




$$x + \mathcal{O}\left(x^{2}\right)$$




```
expand(s1 * s2)
```




$$x + \mathcal{O}\left(x^{2}\right)$$



If we want to get rid of the order information we can use the `removeO` method:


```
expand(s1.removeO() * s2.removeO())
```




$$\frac{1}{24} x^{5} - \frac{1}{2} x^{3} + x$$



But note that this is not the correct expansion of $\cos(x)\sin(x)$ to $5$th order:


```
(cos(x)*sin(x)).series(x, 0, 6)
```




$$x - \frac{2}{3} x^{3} + \frac{2}{15} x^{5} + \mathcal{O}\left(x^{6}\right)$$



## Linear algebra

### Matrices

Matrices are defined using the `Matrix` class:


```
m11, m12, m21, m22 = symbols("m11, m12, m21, m22")
b1, b2 = symbols("b1, b2")
```


```
A = Matrix([[m11, m12],[m21, m22]])
A
```




$$\left[\begin{smallmatrix}m_{11} & m_{12}\\m_{21} & m_{22}\end{smallmatrix}\right]$$




```
b = Matrix([[b1], [b2]])
b
```




$$\left[\begin{smallmatrix}b_{1}\\b_{2}\end{smallmatrix}\right]$$



With `Matrix` class instances we can do the usual matrix algebra operations:


```
A**2
```




$$\left[\begin{smallmatrix}m_{11}^{2} + m_{12} m_{21} & m_{11} m_{12} + m_{12} m_{22}\\m_{11} m_{21} + m_{21} m_{22} & m_{12} m_{21} + m_{22}^{2}\end{smallmatrix}\right]$$




```
A * b
```




$$\left[\begin{smallmatrix}b_{1} m_{11} + b_{2} m_{12}\\b_{1} m_{21} + b_{2} m_{22}\end{smallmatrix}\right]$$



And calculate determinants and inverses, and the like:


```
A.det()
```




$$m_{11} m_{22} - m_{12} m_{21}$$




```
A.inv()
```




$$\left[\begin{smallmatrix}\frac{1}{m_{11}} + \frac{m_{12} m_{21}}{m_{11}^{2} \left(m_{22} - \frac{m_{12} m_{21}}{m_{11}}\right)} & - \frac{m_{12}}{m_{11} \left(m_{22} - \frac{m_{12} m_{21}}{m_{11}}\right)}\\- \frac{m_{21}}{m_{11} \left(m_{22} - \frac{m_{12} m_{21}}{m_{11}}\right)} & \frac{1}{m_{22} - \frac{m_{12} m_{21}}{m_{11}}}\end{smallmatrix}\right]$$



## Solving equations

For solving equations and systems of equations we can use the `solve` function:


```
solve(x**2 - 1, x)
```




$$\begin{bmatrix}-1, & 1\end{bmatrix}$$




```
solve(x**4 - x**2 - 1, x)
```




$$\begin{bmatrix}- i \sqrt{- \frac{1}{2} + \frac{1}{2} \sqrt{5}}, & i \sqrt{- \frac{1}{2} + \frac{1}{2} \sqrt{5}}, & - \sqrt{\frac{1}{2} + \frac{1}{2} \sqrt{5}}, & \sqrt{\frac{1}{2} + \frac{1}{2} \sqrt{5}}\end{bmatrix}$$



System of equations:


```
solve([x + y - 1, x - y - 1], [x,y])
```




$$\begin{Bmatrix}x : 1, & y : 0\end{Bmatrix}$$



In terms of other symbolic expressions:


```
solve([x + y - a, x - y - c], [x,y])
```




$$\begin{Bmatrix}x : \frac{1}{2} a + \frac{1}{2} c, & y : \frac{1}{2} a - \frac{1}{2} c\end{Bmatrix}$$



## Quantum mechanics: noncommuting variables

How about non-commuting symbols? In quantum mechanics we need to work with noncommuting operators, and SymPy has a nice support for noncommuting symbols and even a subpackage for quantum mechanics related calculations!


```
from sympy.physics.quantum import *
```

## States

We can define symbol states, kets and bras:


```
Ket('psi')
```




$${\left|\psi\right\rangle }$$




```
Bra('psi')
```




$${\left\langle \psi\right|}$$




```
u = Ket('0')
d = Ket('1')

a, b = symbols('alpha beta', complex=True)
```


```
phi = a * u + sqrt(1-abs(a)**2) * d; phi
```




$$\alpha {\left|0\right\rangle } + \sqrt{- \left\lvert{\alpha}\right\rvert^{2} + 1} {\left|1\right\rangle }$$




```
Dagger(phi)
```




$$\overline{\alpha} {\left\langle 0\right|} + \overline{\sqrt{- \left\lvert{\alpha}\right\rvert^{2} + 1}} {\left\langle 1\right|}$$




```
Dagger(phi) * d
```




$$\left(\overline{\alpha} {\left\langle 0\right|} + \overline{\sqrt{- \left\lvert{\alpha}\right\rvert^{2} + 1}} {\left\langle 1\right|}\right) {\left|1\right\rangle }$$



Use `qapply` to distribute a mutiplication:


```
qapply(Dagger(phi) * d)
```




$$\overline{\alpha} \left\langle 0 \right. {\left|1\right\rangle } + \overline{\sqrt{- \left\lvert{\alpha}\right\rvert^{2} + 1}} \left\langle 1 \right. {\left|1\right\rangle }$$




```
qapply(Dagger(phi) * u)
```




$$\overline{\alpha} \left\langle 0 \right. {\left|0\right\rangle } + \overline{\sqrt{- \left\lvert{\alpha}\right\rvert^{2} + 1}} \left\langle 1 \right. {\left|0\right\rangle }$$



### Operators


```
A = Operator('A')
B = Operator('B')
```

Check if they are commuting!


```
A * B == B * A
```




$$False$$




```
expand((A+B)**3)
```




$$A B A + A \left(B\right)^{2} + \left(A\right)^{2} B + \left(A\right)^{3} + B A B + B \left(A\right)^{2} + \left(B\right)^{2} A + \left(B\right)^{3}$$




```
c = Commutator(A,B)
c
```




$$\left[A,B\right]$$



We can use the `doit` method to evaluate the commutator:


```
c.doit()
```




$$A B - B A$$



We can mix quantum operators with C-numbers:


```
c = Commutator(a * A, b * B)
c
```




$$\alpha \beta \left[A,B\right]$$



To expand the commutator, use the `expand` method with the `commutator=True` keyword argument:


```
c = Commutator(A+B, A*B)
c.expand(commutator=True)
```




$$- \left[A,B\right] B + A \left[A,B\right]$$




```
Dagger(Commutator(A, B))
```




$$- \left[A^{\dagger},B^{\dagger}\right]$$




```
ac = AntiCommutator(A,B)
```


```
ac.doit()
```




$$A B + B A$$



#### Example: Quadrature commutator

Let's look at the commutator of the electromagnetic field quadatures $x$ and $p$. We can write the quadrature operators in terms of the creation and annihilation operators as:

$\displaystyle x = (a + a^\dagger)/\sqrt{2}$

$\displaystyle p = -i(a - a^\dagger)/\sqrt{2}$



```
X = (A + Dagger(A))/sqrt(2)
X
```




$$\frac{1}{2} \sqrt{2} \left(A^{\dagger} + A\right)$$




```
P = -I * (A - Dagger(A))/sqrt(2)
P
```




$$- \frac{1}{2} \sqrt{2} i \left(- A^{\dagger} + A\right)$$



Let's expand the commutator $[x,p]$


```
Commutator(X, P).expand(commutator=True).expand(commutator=True)
```




$$- i \left[A^{\dagger},A\right]$$



Here we see directly that the well known commutation relation for the quadratures

$[x,p]=i$

is a directly related to

$[A, A^\dagger]=1$ 

(which SymPy does not know about, and does not simplify).

For more details on the quantum module in SymPy, see:

* http://docs.sympy.org/0.7.2/modules/physics/quantum/index.html
* http://nbviewer.ipython.org/urls/raw.github.com/ipython/ipython/master/docs/examples/notebooks/sympy_quantum_computing.ipynb

## Further reading

* http://sympy.org/en/index.html - The SymPy projects web page.
* https://github.com/sympy/sympy - The source code of SymPy.
* http://live.sympy.org - Online version of SymPy for testing and demonstrations.

## Versions


```
%reload_ext version_information

%version_information numpy, sympy
```




<table><tr><th>Software</th><th>Version</th></tr><tr><td>Python</td><td>3.3.2+ (default, Feb 28 2014, 00:52:16) [GCC 4.8.1]</td></tr><tr><td>IPython</td><td>2.2.0</td></tr><tr><td>OS</td><td>posix [linux]</td></tr><tr><td>numpy</td><td>1.8.2</td></tr><tr><td>sympy</td><td>0.7.5</td></tr><tr><td colspan='2'>Tue Aug 26 22:57:37 2014 JST</td></tr></table>



e6ba61e27d4369049af6f6486f86b22b7912dc5e
ipynb
Jupyter Notebook
notebooks/example_Lecture-5-Sympy.ipynb
include/pandas
a9996e96c4630eda88356e9033660b571e46afd7
BSD-3-Clause
2017-11-30T13:16:47.000Z
2019-12-18T21:27:21.000Z
Lecture-5-Sympy.ipynb
a301-teaching/scientific-python-lectures
12c2d6976230ad3e604153e2b40b3282fa168c86
CC-BY-3.0
Lecture-5-Sympy.ipynb
a301-teaching/scientific-python-lectures
12c2d6976230ad3e604153e2b40b3282fa168c86
CC-BY-3.0
2015-10-22T09:38:34.000Z
2017-03-14T05:24:56.000Z
Qwen/Qwen-72B
1. YES
2. YES