# Lecture 5

# Sympy - Symbolic algebra in Python

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

The latest version of this IPython notebook lecture is available at http://github.com/jrjohansson/scientific-python-lectures.

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

1

%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 - A python module that can be used in any Python program, or in an IPython session, that provides powerful CAS features.
- Sage - 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`

:

1

from sympy import *

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

1
2
3
4
5
6

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:

1

x = Symbol('x')

1

(pi + x)**2

1
2

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

1

type(a)

```
sympy.core.symbol.Symbol
```

We can add assumptions to symbols when we create them:

1

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

1

x.is_imaginary

1

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

1

x > 0

### Complex numbers

The imaginary unit is denoted `I`

in Sympy.

1

1+1*I

1

I**2

1

(x * I + 1)**2

### Rational numbers

There are three different numerical types in SymPy: `Real`

, `Rational`

, `Integer`

:

1
2

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

1

r1

1

r1+r2

1

r1/r2

## 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.

1

pi.evalf(n=50)

1

y = (x + pi)**2

1

N(y, 5) # same as evalf

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:

1

y.subs(x, 1.5)

1

N(y.subs(x, 1.5))

The `subs`

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

1

y.subs(x, a+pi)

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

1

import numpy

1

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

1

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

1
2

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:

1
2

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)

1

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:

1
2
3

%%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
```

1
2
3

%%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

1

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

1

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

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:

1

sin(a+b)

1

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

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:

1

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

### 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:

1
2

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

1
2

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

1

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

### apart and together

To manipulate symbolic expressions of fractions, we can use the `apart`

and `together`

functions:

1

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

1

f1

1

apart(f1)

1

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

1

f2

1

together(f2)

Simplify usually combines fractions but does not factor:

1

simplify(f2)

## 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:

1

y

1

diff(y**2, x)

For higher order derivatives we can do:

1

diff(y**2, x, x)

1

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

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

1

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

1

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

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

1

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

## Integration

Integration is done in a similar fashion:

1

f

1

integrate(f, x)

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

1

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

and also improper integrals

1

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

Remember, `oo`

is the SymPy notation for inifinity.

### Sums and products

We can evaluate sums and products using the functions: ‘Sum’

1

n = Symbol("n")

1

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

1

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

1

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

Products work much the same way:

1

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

## Limits

Limits can be evaluated using the `limit`

function. For example,

1

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

We can use ‘limit’ to check the result of derivation using the `diff`

function:

1

f

1

diff(f, x)

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

1

h = Symbol("h")

1

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

OK!

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

keywork argument:

1

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

1

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

## 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:

1

series(exp(x), x)

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:

1

series(exp(x), x, 1)

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

1

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

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:

1
2

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

1
2

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

1

expand(s1 * s2)

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

method:

1

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

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

1

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

## Linear algebra

### Matrices

Matrices are defined using the `Matrix`

class:

1
2

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

1
2

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

1
2

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

With `Matrix`

class instances we can do the usual matrix algebra operations:

1

A**2

1

A * b

And calculate determinants and inverses, and the like:

1

A.det()

1

A.inv()

## Solving equations

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

function:

1

solve(x**2 - 1, x)

1

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

System of equations:

1

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

In terms of other symbolic expressions:

1

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

## 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!

1

from sympy.physics.quantum import *

## States

We can define symbol states, kets and bras:

1

Ket('psi')

1

Bra('psi')

1
2
3
4

u = Ket('0')
d = Ket('1')
a, b = symbols('alpha beta', complex=True)

1

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

1

Dagger(phi)

1

Dagger(phi) * d

Use `qapply`

to distribute a mutiplication:

1

qapply(Dagger(phi) * d)

1

qapply(Dagger(phi) * u)

### Operators

1
2

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

Check if they are commuting!

1

A * B == B * A

1

expand((A+B)**3)

1
2

c = Commutator(A,B)
c

We can use the `doit`

method to evaluate the commutator:

1

c.doit()

We can mix quantum operators with C-numbers:

1
2

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

To expand the commutator, use the `expand`

method with the `commutator=True`

keyword argument:

1
2

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

1

Dagger(Commutator(A, B))

1

ac = AntiCommutator(A,B)

1

ac.doit()

#### 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}$

1
2

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

1
2

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

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

1

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

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

1
2
3

%reload_ext version_information
%version_information numpy, sympy

Software | Version |
---|---|

Python | 3.3.2+ (default, Feb 28 2014, 00:52:16) [GCC 4.8.1] |

IPython | 2.2.0 |

OS | posix [linux] |

numpy | 1.8.2 |

sympy | 0.7.5 |

Tue Aug 26 22:57:37 2014 JST |