Symbolic Mathematics in Python
Symbolic Mathematics in Python
Symbolic Mathematics in Python
Table Of Contents
3.2.1. First Steps with SymPy
3.2.2. Algebraic manipulations
3.2. Sympy : Symbolic Mathematics in Python
3.2.3. Calculus
3.2.4. Equation solving Author: Fabian Pedregosa
3.2.5. Linear Algebra
3.2.5.1. Matrices
3.2.5.2. Differential Equations
Objectives
1. Evaluate expressions with arbitrary precision.
2. Perform algebraic manipulations on symbolic expressions.
Custom Search
3. Perform basic calculus tasks (limits, differentiation and
integration) with symbolic expressions.
What is SymPy? SymPy is a Python library for symbolic mathematics. It aims to be an alternative to
systems such as Mathematica or Maple while keeping the code as simple as possible and easily
extensible. SymPy is written entirely in Python and does not require any external libraries.
Chapters contents
First Steps with SymPy
Using SymPy as a calculator
Symbols
Algebraic manipulations
Expand
Simplify
Calculus
Limits
Differentiation
Series expansion
Integration
Equation solving
Linear Algebra
Matrices
Differential Equations
The Rational class represents a rational number as a pair of two Integers: the numerator and the
denominator, so Rational(1, 2) represents 1/2, Rational(5, 2) 5/2 and so on:
>>> a
1/2
>>> a*2
1
SymPy uses mpmath in the background, which makes it possible to perform computations using
arbitrary-precision arithmetic. That way, some special constants, like , , (Infinity), are treated as
symbols and can be evaluated with arbitrary precision:
>>> sym.pi.evalf()
3.14159265358979
Exercises
1. Calculate with 100 decimals.
3.2.1.2. Symbols
In contrast to other Computer Algebra Systems, in SymPy you have to declare symbolic variables
explicitly:
>>> x + y + x - y >>>
2*x
>>> (x + y) ** 2
(x + y)**2
Symbols can now be manipulated using some of python operators: +, -`, ``*, ** (arithmetic), &, |, ~ ,
>>, << (boolean).
Printing
Sympy allows for control of the display of the output. From here we use the following setting for
printing:
SymPy is capable of performing powerful algebraic manipulations. We’ll take a look into some of the most
frequently used: expand and simplify.
3.2.2.1. Expand
Use this to expand an algebraic expression. It will try to denest powers and multiplications:
3.2.2.2. Simplify
Use simplify if you would like to transform an expression into a simpler form:
Simplification is a somewhat vague term, and more precises alternatives to simplify exists: powsimp
(simplification of exponents), trigsimp (for trigonometric expressions) , logcombine, radsimp,
together.
Exercises
1. Calculate the expanded form of .
3.2.3. Calculus
3.2.3.1. Limits
Limits are easy to use in SymPy, they follow the syntax limit(function, variable, point), so to
compute the limit of as , you would issue limit(f, x, 0):
>>> sym.limit(x ** x, x, 0)
1
3.2.3.2. Differentiation
You can differentiate any SymPy expression using diff(func, var). Examples:
>>> sym.diff(sym.tan(x), x)
2
tan (x) + 1
SymPy also knows how to compute the Taylor series of an expression at a point. Use series(expr,
var):
Exercises
1. Calculate
3.2.3.4. Integration
SymPy has support for indefinite and definite integration of transcendental elementary and special
functions via integrate() facility, which uses the powerful extended Risch-Norman algorithm and some
heuristics and pattern matching. You can integrate elementary functions:
SymPy is able to solve algebraic equations, in one and several variables using solveset():
As you can see it takes as first argument an expression that is supposed to be equaled to 0. It also has
(limited) support for transcendental equations:
(-3, 1)
Another alternative in the case of polynomial equations is factor. factor returns the polynomial factorized
into irreducible terms, and is capable of computing the factorization over various domains:
>>> f = x ** 4 - 3 * x ** 2 + 1 >>>
>>> sym.factor(f)
/ 2 \ / 2 \
\x - x - 1/*\x + x - 1/
SymPy is also able to solve boolean equations, that is, to decide if a certain boolean expression is
satisfiable or not. For this, we use the function satisfiable:
This tells us that (x & y) is True whenever x and y are both True. If an expression cannot be true, i.e.
no values of its arguments can make the expression True, it will return False:
Exercises
1. Solve the system of equations ,
2. Are there boolean values x, y that make (~x | y) & (~y | x) true?
3.2.5.1. Matrices
>>> A**2
[x*y + 1 2*x ]
[ ]
[ 2*y x*y + 1]
SymPy is capable of solving (some) Ordinary Differential. To solve differential equations, use dsolve.
First, create an undefined function by passing cls=Function to the symbols function:
f and g are now undefined functions. We can call f(x), and it will represent an unknown function:
Keyword arguments can be given to this function in order to help if find the best possible resolution
system. For example, if you know that it is a separable equations, you can use keyword
hint='separable' to force dsolve to resolve it as a separable equation:
Exercises
1. Solve the Bernoulli differential equation