SymPy - Symbolic algebra in Python#

%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 environment 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
../../_images/d6b7649201d89920634536e9a954b10aca6355b6b500898f8740c16dc53d7781.png
# 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
../../_images/f677f37bdc590b156b5c9dd85a95f73bd21e7446bccdf453afe075ec59b890f3.png

Complex numbers#

The imaginary unit is denoted I in SymPy.

1+1*I
../../_images/36231cd0b550a14ea3e1669a737144b4162936f58cd63938c35f5baec3595742.png
I**2
../../_images/b51e8043844819f2d3fc1b18ae1287bda18488443750fce5c0faebd3bd6d66f5.png
(x * I + 1)**2
../../_images/1edb819f3370fc8ee35dc7d77da928630a603fe3e1181ab4546fd39783ed3905.png

Rational numbers#

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

r1 = Rational(4,5)
r2 = Rational(5,4)
r1
../../_images/85e0b78af0af8c33b0a6655782e0591008617ce4f2074ccb5a29a177abdd4bd7.png
r1+r2
../../_images/47b86ae30e7cd9ac2001218c4579d4c276d466c917823cd5fdbc0fc8fc403964.png
r1/r2
../../_images/b4ae868fcf45b90ffa4d2ca04a2090c86044d05ba7e0d452dd6171c577ef09ee.png

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.

pi.evalf(n=50)
../../_images/dd2b2a820ad4defce4782ad0633b160b3149494a51495c754c6d96d5fdd1fa1c.png
y = (x + pi)**2
N(y, 5) # same as evalf
../../_images/ac7e234d0ec2bb89fe47e76084f383c9f101d18b00a6aa1c869b8c74b92bb81e.png

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)
../../_images/667b5f7931da6d34bfd0e62ebc86083f585d5dd865863a69ecb44d743e69c0e9.png
N(y.subs(x, 1.5))
../../_images/b6f1a279dc6d0a56565e1827b961b1523ad533ab9ca1e79685fa23805efe3d53.png

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

y.subs(x, a+pi)
../../_images/3cef1fa5e815275a760faeb1e007f8516d4004128fd5f6b6de347cf3800ef4ab.png

We can also combine numerical evaluation 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 = plt.subplots()
ax.plot(x_vec, y_vec);
../../_images/217cda7a076e9f246a84a7209b2666a0004e63f31a00a78f716c7a3b02858355.png

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:

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: 28.2 ms per loop
%%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

(x+1)*(x+2)*(x+3)
../../_images/f1fa17d2cf46bd4f9a8f1a8e5b4c080b1ffd6709b0bab74aa2e48e60e8553e7c.png
expand((x+1)*(x+2)*(x+3))
../../_images/fe2fe8ad30eb36c5df2bdf9c9aab5a8a422a856629d4fd783d545c5ae0243fd9.png

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)
../../_images/a454e1967418fa065a4873d38db140fe270af04443ffedc18926979300700386.png
expand(sin(a+b), trig=True)
../../_images/ae1628393b80ddb0dc9784f3fd6e46fbbb06f951afdd6b3ffe396dfeea98900d.png

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:

factor(x**3 + 6 * x**2 + 11*x + 6)
../../_images/f1fa17d2cf46bd4f9a8f1a8e5b4c080b1ffd6709b0bab74aa2e48e60e8553e7c.png

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 (sometimes) expands a product
simplify((x+1)*(x+2)*(x+3))
../../_images/f1fa17d2cf46bd4f9a8f1a8e5b4c080b1ffd6709b0bab74aa2e48e60e8553e7c.png
# simplify uses trigonometric identities
simplify(sin(a)**2 + cos(a)**2)
../../_images/211db032cb5eb7f0e2e80b65f62b950d5040072ce8843ff5428c9b282b31387e.png
simplify(cos(x)/sin(x))
../../_images/ba0e03d06a1d1f877b78c72992ffb6393be65f9e8229017a534498903c9b8403.png

apart and together#

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

f1 = 1/((a+1)*(a+2))
f1
../../_images/02b4a57e5d88296fbec763bf58fc5b5e56233394585402f2e240d49e7d6be8c8.png
apart(f1)
../../_images/e865227327e86939da87990042de91f25cf1840809fce0ad991272860ee99a14.png
f2 = 1/(a+2) + 1/(a+3)
f2
../../_images/09ff25e4e6352aed9e11ea5b9d5e7385e531ed0adafb04b4bb44918f549a86f1.png
together(f2)
../../_images/a3d06935ae893a227c9b9cf5bc472540e5004e211e7a712fc9753c4c555dd291.png

Simplify usually combines fractions but does not factor:

simplify(f2)
../../_images/a3d06935ae893a227c9b9cf5bc472540e5004e211e7a712fc9753c4c555dd291.png

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
../../_images/d6b7649201d89920634536e9a954b10aca6355b6b500898f8740c16dc53d7781.png
diff(y**2, x)
../../_images/abf6f5a6a9cddef8d26910a9b7f718c8846455772de758c1faccca85ee0296c6.png

For higher order derivatives we can do:

diff(y**2, x, x)
../../_images/88119e971181c06a8511b18a99534f0841d25b2a1507bd4733ae05a82da4b41c.png
diff(y**2, x, 2) # same as above
../../_images/88119e971181c06a8511b18a99534f0841d25b2a1507bd4733ae05a82da4b41c.png

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)
../../_images/5d4db90f9eade388c44896505bda69dbd300ebba7d8154d246e1357cb9f71db3.png

Integration#

Integration is done in a similar fashion:

f
../../_images/f68e9def70fb3ccbfabb11d810c66de94f92827c38c0cb8f1644ffa02d0d2304.png
integrate(f, x)
../../_images/1cdc325eeb910e18186ee666ce458d722388ba23bcad33afd59dd5790124d01f.png

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

integrate(f, (x, -1, 1))
../../_images/59b1273dd10ffb8bd52f56ef2924c7386db367535dd2aef1b411bb1fcc4c3e95.png

and also improper integrals

integrate(exp(-x**2), (x, -oo, oo))
../../_images/94d6b1803132d143fc9a0fbfa0d2754a94224b788753d8578975bc1939e4993d.png

Remember, oo is the SymPy notation for inifinity.

Sums and products#

We can evaluate sums using the function Sum:

n = Symbol("n")
Sum(1/n**2, (n, 1, 10))
../../_images/63826235758e61041b400b998201e7e18d05b7039d831c9df48f7f3e81f4a4eb.png
Sum(1/n**2, (n,1, 10)).evalf()
../../_images/16230a96b45c35fdb5c47916bcbc1ad5b02053b713666a478c76dcba1e308dea.png
Sum(1/n**2, (n, 1, oo)).evalf()
../../_images/71f3078e8b4db5dd0b500a41a9ad9953cc9e250c37c059ed805ef0ad3ebc0e7e.png

Products work much the same way:

Product(n, (n, 1, 10)) # 10!
../../_images/4631a83e8dca47111a5d639847c3968b30af8b6b977236fc668e7c21d1d6a9b7.png

Limits#

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

limit(sin(x)/x, x, 0)
../../_images/211db032cb5eb7f0e2e80b65f62b950d5040072ce8843ff5428c9b282b31387e.png

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

f
../../_images/f68e9def70fb3ccbfabb11d810c66de94f92827c38c0cb8f1644ffa02d0d2304.png
diff(f, x)
../../_images/26b3a37ca4a74aabc376c7ad93d0eec7020f4c2afe653f0a35b67bb2a580ec87.png

\(\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)
../../_images/26b3a37ca4a74aabc376c7ad93d0eec7020f4c2afe653f0a35b67bb2a580ec87.png

OK!

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

limit(1/x, x, 0, dir="+")
../../_images/554fa104cc1d90b50793efba856aa9588f0bc0b1c9642d19a00a5ec0a3ba7466.png
limit(1/x, x, 0, dir="-")
../../_images/582d92b79b0d1ea13cb5f4c937158f2661eb4d4da8709f89341baacef27d501e.png

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)
../../_images/b9905bb097542af85b0a6cdcd9592f63cd579e8e91e907a0f42bf3b7f0beb28f.png

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)
../../_images/8a39fc4dc4da1c21e65e9e7f75363c5751bde5cb3d1728651e01366c699d6f1e.png

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

series(exp(x), x, 1, 10)
../../_images/8ee060717633aabb2774597cdc77e8d370980335adb2e124ce344424b79cb978.png

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
../../_images/5f85e67b2019c3636fd7114e15ff6a423ec3e1ccb131ac2139fd3b853e41e184.png
s2 = sin(x).series(x, 0, 2)
s2
../../_images/8beecd7af52f8243200020a1ccafa652a17225f3466e67e2f422ae53634d6a9e.png
expand(s1 * s2)
../../_images/8beecd7af52f8243200020a1ccafa652a17225f3466e67e2f422ae53634d6a9e.png

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

expand(s1.removeO() * s2.removeO())
../../_images/474a503f1f27358ed2b622216b24cff64886d5de57ebd97cc2c910f772e5d987.png

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)
../../_images/9619969b6d7bda5e36935c7856ca3cd8cd2e9fd6c718412b73e80abde08b641e.png

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
../../_images/c789a02113f799fb67efd99fb1555e8e0193f3c6957ec2c04b470a8f23c8545e.png
b = Matrix([[b1], [b2]])
b
../../_images/fcb83071c9ab6f64ec80c0ffed2430e9254022f7b76a9fcf82e1a72b74179ef7.png

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

A**2
../../_images/edc2e765b0906af4edeaadaeb42a91bd507a5268fd8aaee1ddc3d31539a18662.png
A * b
../../_images/9fcb6a6ee95dfba662f6d8ee2f670d103fa12adc5485fb5e7740cbba29989297.png

And calculate determinants and inverses, and the like:

A.det()
../../_images/b356f88e405d44bd3ccabedf9b1a754c745648cbb4318d896c37e50103e84cb7.png
A.inv()
../../_images/ea3788210d90a21f601632d93606383d475b56676f211a9be1e2a743bedcc6f4.png

Solving equations#

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

solve(x**2 - 1, x)
../../_images/774841399c92200322ccbe1ada1ffc23e2ed0280d99184d9d649df6368477742.png
solve(x**4 - x**2 - 1, x)
../../_images/38dbbeea6fbbe4f90ffef1e6f7fce39452f3d7368b09c6b04b6f588a0b13d5c3.png

System of equations:

solve([x + y - 1, x - y - 1], [x,y])
../../_images/65668222efdc6f9d25fb206ac139f29f013e4c9dbf23eda150c214af5c9e6ad7.png

In terms of other symbolic expressions:

solve([x + y - a, x - y - c], [x,y])
../../_images/a5f58847e54ce02ce251be4d81f1c21cc4cb072e5173ee0c507ac8d6fdbc480a.png

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, matplotlib, sympy
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