$$ \newcommand{\D}{\displaystyle} \renewcommand{\eqref}[1]{Eq.~(\ref{#1})} $$

 

 

 

10 Sympolic computation with SymPy

In this chapter we provide a very short introduction to SymPy customized for the applications and examples in the current setting. For a more thorough presentation see e.g. [17].

10.1 Introduction

SymPy is a Python library for symbolic mathematics, with the ambition to offer a full-featured computer algebra system (CAS). The library design makes SymPy ideally suited to make symbolic mathematical computations integrated in numerical Python applications.

10.2 Basic features

The SymPy library is implemented in the Python module sympy. To avoid namespace conflicts (e.g. with other modules like NumPy/SciPy) we propose to import the SymPy as:

import sympy

10.2.0.1 Symbols

SymPy introduces the class symbols (or Symbol) to represent mathematical symbols as Python objects. An instance of type symbols has a set of attributes to hold the its properties and methods to operate on those properties. Such symbols may be used to represent and manipulate algebraic expressions. Unlike many symbolic manipulation systems, variables in SymPy must be defined before they are used (for justification see sympy.org)

As an example, let us define a symbolic expression, representing the mathematical expression \( x^2 + xy - y \)

import sympy
x, y = sympy.symbols('x y')
expr = x**2 + x*y -y
expr

Note that we wrote the expression as if "x" and "y" were ordinary Python variables, but instead of being evaluated the expression remains unaltered.

To make the output look nicer we may invoke the pretty print feature of SymPy by:

sympy.init_printing(use_latex=True)
expr

The expression is now ready for algebraic manipulation:

expr + 2
x**2 + x*y -y + 2

and

expr + y
x**2 + x*y 

Note that the result of the above is not \( x^2 + xy -y + y \) but rather \( x^2 + xy \), i.e. the \( -y \) and the \( +y \) are added and found to cancel automatically by SymPy and a simplified expression is outputted accordingly. Appart from rather obvious simplifications like discarding subexpression that add up to zero (e.g. \( y-y \) or \( \sqrt{9}=3 \)), most simplifications are not performed automatically by SymPy.

expr2 = x**2 + 2*x + 1
expr3 = sympy.factor(expr2)
expr3

10.2.0.2 Matrices

Matrices in SymPy are implemented with the Matrix class and are constructed by providing a list of row the vectors in the following manner:

sympy.init_printing(use_latex='mathjax')
M = sympy.Matrix([[1, -1], [2, 1], [4, 4]])
M

A matrix with symbolic elements may be constructed by:

a, b, c, d = sympy.symbols('a b c d')
M = sympy.Matrix([[a, b],[c, d]])
M

The matrices may naturally be manipulated like any other object in SymPy or Python. To illustrate this we introduce another \( 2x2 \)-matrix

n1, n2, n3, n4 =sympy.symbols('n1 n2 n3 n4')
N=sympy.Matrix([[n1, n2],[n3, n4]])
N

The two matrices may then be added, subtracted, multiplied, and inverted by the following simple statements

M+N, M-N, M*N, M.inv()

M=sympy.Matrix([[0, a], [a, 0]])

Diagonaliztion:

L, D = M.diagonalize()
L, D

10.2.0.3 Differentiating and integrating

Consider also the parabolic function which may describe the velocity profile for fully developed flow in a cylinder.

from sympy import integrate, diff, symbols, pi
v0, r = symbols('v0 r')
v = v0*(1 - r**2)
Q = integrate(2*pi*v*r, r)
Q

Q = sympy.factor(Q)
Q

newV = diff(Q, r)/(r*2*pi)
newV
sympy.simplify(newV)

compute \( \int cos(x) \)

from sympy import cos
integrate(cos(x), x)

compute \( \int_{-\infty}^{\infty} sin(x^2) \)

from sympy import sin, oo
integrate(sin(x**2), (x, -oo, oo))

10.2.0.4 limits

perform \( \lim_{x\to 0} \frac{sin(x)}{x} \)

from sympy import limit
limit(sin(x)/x, x, 0)

10.2.0.5 solving equations

solve the algebraic equation \( x^2-4=0 \)

from sympy import solve
solve(x**2 - 4*x, x)

solve the differential equation \( y''-y'=e^t \)

from sympy import dsolve, Function, Eq, exp
y = Function('y')
t = symbols('t')
diffeq = Eq(y(t).diff(t, t) - y(t), exp(t))
dsolve(diffeq, y(t))