SymPy is a Python library for symbolic mathematics. It can parse mathematical expressions, substitute, differentiate, integrate and evaluate them, as well as solve algebraic and differential equations. It is also itself written entirely in Python, with a focus keeping the code comprehensible and easily extensible. There is also a related project SymEngine, which is written in C++ with a focus on speed, which offers a much faster implementation of a subset of SymPy's functionality.
Press Spacebar
to go to the next slide (or ?
to see all navigation shortcuts)
Lunch Time Python, Scientific Software Center, Heidelberg University
conda install sympy
python -m pip install sympy
Or try it out online:
sympy.symbols
import sympy as sp
# Define a single symbol x named 'x':
x = sp.symbols("x")
x
type(x)
sympy.core.symbol.Symbol
# Define multiple symbols at once:
y, z, t, nu = sp.symbols("y z t nu")
# Can use different name for symbol & object (not recommended!)
confusing = sp.symbols("alpha")
print(confusing)
alpha
+
, -
, *
, /
)expr = x + y - 2 * z
expr
type(expr)
sympy.core.add.Add
expr.args
(x, y, -2*z)
type(expr.args[2])
sympy.core.mul.Mul
expr.args[2].args
(-2, z)
x + 1
type((1 + x).args[0])
sympy.core.numbers.One
type((123 + x).args[0])
sympy.core.numbers.Integer
# be careful with unexpected Python maths!
x + 1 / 2
# explicit construction of Rational
x + sp.Rational(1, 2)
# explicit construction of Integer
x + sp.Integer(1) / 2
# equivalent but now all operations involve a sympy expr:
(2 * x + 1) / 2
sympy.functions
contains many built-in analytic functionsexpr = sp.sin(x) * sp.exp(y) + sp.gamma(t)
expr
.subs
takes a list of (old, new)
pairs(x + 1).subs(x, 1)
(x * sp.cos(y)).subs([(x, 1), (y, 0.2)])
(x * y).subs(x, y**3)
sympify
converts a string into a SymPy expressionexpr = sp.sympify("x")
type(expr)
sympy.core.symbol.Symbol
expr == x
True
expr = sp.sympify("cos(x) + a")
expr
expr.args[0]
expr.args[1].args[0] == x
True
evalf
for one off numerical evaluation of an expressionSymbol : number
pairslambdify
for efficient repeated evaluation of an expressionsin
, cos
with numpy equivalentssp.sqrt(2)
sp.sqrt(2).evalf()
expr = sp.sqrt(1 + x)
expr
expr.evalf(subs={x: 1})
f = sp.lambdify(x, expr)
f(1)
1.4142135623730951
import numpy as np
a = np.array([1, 2, 3, 5.21])
f(a)
array([1.41421356, 1.73205081, 2. , 2.49198716])
sp.cos(x) ** 2 + sp.sin(x) ** 2
sp.simplify(sp.cos(x) ** 2 + sp.sin(x) ** 2)
sp.gamma(x) / sp.gamma(x - 3)
sp.simplify(sp.gamma(x) / sp.gamma(x - 3))
(z + t) ** 2 * (x + 2 * y) ** 6
print(sp.expand((z + t) ** 2 * (x + 2 * y) ** 6))
t**2*x**6 + 12*t**2*x**5*y + 60*t**2*x**4*y**2 + 160*t**2*x**3*y**3 + 240*t**2*x**2*y**4 + 192*t**2*x*y**5 + 64*t**2*y**6 + 2*t*x**6*z + 24*t*x**5*y*z + 120*t*x**4*y**2*z + 320*t*x**3*y**3*z + 480*t*x**2*y**4*z + 384*t*x*y**5*z + 128*t*y**6*z + x**6*z**2 + 12*x**5*y*z**2 + 60*x**4*y**2*z**2 + 160*x**3*y**3*z**2 + 240*x**2*y**4*z**2 + 192*x*y**5*z**2 + 64*y**6*z**2
sp.factor(
t**2 * x**6
+ 12 * t**2 * x**5 * y
+ 60 * t**2 * x**4 * y**2
+ 160 * t**2 * x**3 * y**3
+ 240 * t**2 * x**2 * y**4
+ 192 * t**2 * x * y**5
+ 64 * t**2 * y**6
+ 2 * t * x**6 * z
+ 24 * t * x**5 * y * z
+ 120 * t * x**4 * y**2 * z
+ 320 * t * x**3 * y**3 * z
+ 480 * t * x**2 * y**4 * z
+ 384 * t * x * y**5 * z
+ 128 * t * y**6 * z
+ x**6 * z**2
+ 12 * x**5 * y * z**2
+ 60 * x**4 * y**2 * z**2
+ 160 * x**3 * y**3 * z**2
+ 240 * x**2 * y**4 * z**2
+ 192 * x * y**5 * z**2
+ 64 * y**6 * z**2
)
sympy.diff
differentiates expressionsdiff(f, x)
: $df/dx$diff(f, x, 3)
: $d^3f/dx^3$diff(f, x, y, z)
: $d^3f/dxdydz$expr
sp.diff(expr, x)
sympy.integrate
integrates expressions(Symbol, lower_limit, upper_limit)
is providedexpr
sp.integrate(expr, x)
sp.integrate(expr, (x, 0, 1))
sp.diff(sp.integrate(expr, x), x)
sympy.limit
takes limit of expression at singular pointsinc = sp.sin(x) / x
sinc.subs(x, 0)
sinc.limit(x, 0)
sympy.series
expands an expression around a pointsinc.series(x, 0, 10)
expr = sp.cos(x + 7 * x**2) + sp.sin(x - 2 * x**3)
%time _ = (sp.series(expr, n=100))
CPU times: user 5.49 s, sys: 15.9 ms, total: 5.51 s Wall time: 5.51 s
if "google.colab" in str(get_ipython()):
!pip install symengine -qqq
import symengine as se
%time _ = se.series(expr, n=100)
CPU times: user 1.38 ms, sys: 43 µs, total: 1.42 ms Wall time: 4.24 ms