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:
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
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:
# alternative way of defining symbolsa,b,c=symbols("a, b, c")
We can add assumptions to symbols when we create them:
The imaginary unit is denoted I in Sympy.
There are three different numerical types in SymPy: Real, Rational, Integer:
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.
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:
The subs function can of course also be used to substitute Symbols and expressions:
We can also combine numerical evolution of expressions with NumPy arrays:
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:
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
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
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:
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:
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 productsimplify((x+1)*(x+2)*(x+3))