# SymPy - Symbolic algebra in Python¶

J.R. Johansson (jrjohansson at gmail.com)

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

In :
%matplotlib inline
import matplotlib.pyplot as plt


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

In :
from sympy import *


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

In :
init_printing()

# or with older versions of sympy/ipython, load the IPython extension
# or


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

In :
x = Symbol('x')

In :
(pi + x)**2

Out:
$$\left(x + \pi\right)^{2}$$
In :
# alternative way of defining symbols
a, b, c = symbols("a, b, c")

In :
type(a)

Out:
sympy.core.symbol.Symbol

We can add assumptions to symbols when we create them:

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

In :
x.is_imaginary

Out:
False
In :
x = Symbol('x', positive=True)

In :
x > 0

Out:
$$\mathrm{True}$$

### Complex numbers¶

The imaginary unit is denoted I in SymPy.

In :
1+1*I

Out:
$$1 + i$$
In :
I**2

Out:
$$-1$$
In :
(x * I + 1)**2

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

### Rational numbers¶

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

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

In :
r1

Out:
$$\frac{4}{5}$$
In :
r1+r2

Out:
$$\frac{41}{20}$$
In :
r1/r2

Out:
$$\frac{16}{25}$$

## Numerical evaluation¶

SymPy uses a library for arbitrary 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.

In :
pi.evalf(n=50)

Out:
$$3.1415926535897932384626433832795028841971693993751$$
In :
y = (x + pi)**2

In :
N(y, 5) # same as evalf

Out:
$$\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:

In :
y.subs(x, 1.5)

Out:
$$\left(1.5 + \pi\right)^{2}$$
In :
N(y.subs(x, 1.5))

Out:
$$21.5443823618587$$

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

In :
y.subs(x, a+pi)

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

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

In :
import numpy

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

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

In :
fig, ax = plt.subplots()
ax.plot(x_vec, y_vec); However, this kind of numerical evaluation 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:

In :
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)

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

In :
%%timeit

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

10 loops, best of 3: 28.2 ms per loop

In :
%%timeit

y_vec = f(x_vec)

The slowest run took 8.86 times longer than the fastest. This could mean that an intermediate result is being cached
100000 loops, best of 3: 2.93 µ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 simplify 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

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

Out:
$$\left(x + 1\right) \left(x + 2\right) \left(x + 3\right)$$
In :
expand((x+1)*(x+2)*(x+3))

Out:
$$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:

In :
sin(a+b)

Out:
$$\sin{\left (a + b \right )}$$
In :
expand(sin(a+b), trig=True)

Out:
$$\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 of product expansion is of course factoring. To factor an expression in SymPy use the factor function:

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

Out:
$$\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:

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

Out:
$$\left(x + 1\right) \left(x + 2\right) \left(x + 3\right)$$
In :
# simplify uses trigonometric identities
simplify(sin(a)**2 + cos(a)**2)

Out:
$$1$$
In :
simplify(cos(x)/sin(x))

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

### apart and together¶

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

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

In :
f1

Out:
$$\frac{1}{\left(a + 1\right) \left(a + 2\right)}$$
In :
apart(f1)

Out:
$$- \frac{1}{a + 2} + \frac{1}{a + 1}$$
In :
f2 = 1/(a+2) + 1/(a+3)

In :
f2

Out:
$$\frac{1}{a + 3} + \frac{1}{a + 2}$$
In :
together(f2)

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

Simplify usually combines fractions but does not factor:

In :
simplify(f2)

Out:
$$\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:

In :
y

Out:
$$\left(x + \pi\right)^{2}$$
In :
diff(y**2, x)

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

For higher order derivatives we can do:

In :
diff(y**2, x, x)

Out:
$$12 \left(x + \pi\right)^{2}$$
In :
diff(y**2, x, 2) # same as above

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

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

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

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


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

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

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

## Integration¶

Integration is done in a similar fashion:

In :
f

Out:
$$\sin{\left (x y \right )} + \cos{\left (y z \right )}$$
In :
integrate(f, x)

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

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

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

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

and also improper integrals

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

Out:
$$\sqrt{\pi}$$

Remember, oo is the SymPy notation for inifinity.

### Sums and products¶

We can evaluate sums using the function Sum:

In :
n = Symbol("n")

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

Out:
$$\sum_{n=1}^{10} \frac{1}{n^{2}}$$
In :
Sum(1/n**2, (n,1, 10)).evalf()

Out:
$$1.54976773116654$$
In :
Sum(1/n**2, (n, 1, oo)).evalf()

Out:
$$1.64493406684823$$

Products work much the same way:

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

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

## Limits¶

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

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

Out:
$$1$$

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

In :
f

Out:
$$\sin{\left (x y \right )} + \cos{\left (y z \right )}$$
In :
diff(f, x)

Out:
$$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}$

In :
h = Symbol("h")

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

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

OK!

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

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

Out:
$$\infty$$
In :
limit(1/x, x, 0, dir="-")

Out:
$$-\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:

In :
series(exp(x), x)

Out:
$$1 + x + \frac{x^{2}}{2} + \frac{x^{3}}{6} + \frac{x^{4}}{24} + \frac{x^{5}}{120} + \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:

In :
series(exp(x), x, 1)

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

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

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

Out:
$$e + e \left(x - 1\right) + \frac{e}{2} \left(x - 1\right)^{2} + \frac{e}{6} \left(x - 1\right)^{3} + \frac{e}{24} \left(x - 1\right)^{4} + \frac{e}{120} \left(x - 1\right)^{5} + \frac{e}{720} \left(x - 1\right)^{6} + \frac{e}{5040} \left(x - 1\right)^{7} + \frac{e}{40320} \left(x - 1\right)^{8} + \frac{e}{362880} \left(x - 1\right)^{9} + \mathcal{O}\left(\left(x - 1\right)^{10}; x\rightarrow1\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:

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

Out:
$$1 - \frac{x^{2}}{2} + \frac{x^{4}}{24} + \mathcal{O}\left(x^{5}\right)$$
In :
s2 = sin(x).series(x, 0, 2)
s2

Out:
$$x + \mathcal{O}\left(x^{2}\right)$$
In :
expand(s1 * s2)

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

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

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

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

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

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

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

## Linear algebra¶

### Matrices¶

Matrices are defined using the Matrix class:

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

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

Out:
$$\left[\begin{matrix}m_{11} & m_{12}\\m_{21} & m_{22}\end{matrix}\right]$$
In :
b = Matrix([[b1], [b2]])
b

Out:
$$\left[\begin{matrix}b_{1}\\b_{2}\end{matrix}\right]$$

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

In :
A**2

Out:
$$\left[\begin{matrix}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{matrix}\right]$$
In :
A * b

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

And calculate determinants and inverses, and the like:

In :
A.det()

Out:
$$m_{11} m_{22} - m_{12} m_{21}$$
In :
A.inv()

Out:
$$\left[\begin{matrix}\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{matrix}\right]$$

## Solving equations¶

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

In :
solve(x**2 - 1, x)

Out:
$$\left [ -1, \quad 1\right ]$$
In :
solve(x**4 - x**2 - 1, x)

Out:
$$\left [ - i \sqrt{- \frac{1}{2} + \frac{\sqrt{5}}{2}}, \quad i \sqrt{- \frac{1}{2} + \frac{\sqrt{5}}{2}}, \quad - \sqrt{\frac{1}{2} + \frac{\sqrt{5}}{2}}, \quad \sqrt{\frac{1}{2} + \frac{\sqrt{5}}{2}}\right ]$$

System of equations:

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

Out:
$$\left \{ x : 1, \quad y : 0\right \}$$

In terms of other symbolic expressions:

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

Out:
$$\left \{ x : \frac{a}{2} + \frac{c}{2}, \quad y : \frac{a}{2} - \frac{c}{2}\right \}$$

## Versions¶

In :
%reload_ext version_information

%version_information numpy, matplotlib, sympy

Out:
SoftwareVersion
Python2.7.10 64bit [GCC 4.2.1 (Apple Inc. build 5577)]
IPython3.2.1
OSDarwin 14.1.0 x86_64 i386 64bit
numpy1.9.2
matplotlib1.4.3
sympy0.7.6
Sat Aug 15 11:37:37 2015 JST