slides_python_5
slides_python_5
Hendrik Speleers
SymPy: Symbolic Mathematics
●
Overview
– A symbolic calculator
– Algebraic manipulation
●
Substitution, simplification, factorization, ...
– Calculus
●
Series, limits, differentiation and integration
– Linear algebra
●
Matrix manipulation and decompositions
– Solvers
●
Linear and nonlinear (system of) equations
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
SymPy
– Symbolic Python
– Python extension for symbolic mathematics
●
Performing algebraic manipulations on symbolic expressions
●
Evaluating expressions with arbitrary precision
●
Similar to: Mathematica, Maple
●
Keeping code as simple as possible and easily extensible
– Import convention
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
A symbolic calculator
– A simple demo
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
A symbolic calculator
– Symbolic variables need to be explicitly defined as symbols
In [1]: x = sym.symbols('x')
In [2]: x, y = sym.symbols('x y')
In [3]: x, y = sym.symbols('x,y')
In [4]: x, y, z = sym.symbols('x:z')
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
A symbolic calculator
– Data types
● Symbolic: sym.Symbol
● Numeric: sym.Integer, sym.Rational, sym.Float
In [1]: x = sym.Symbol('x')
In [2]: x + 1/2 # Symbol + Float
Out[2]: x + 0.5
In [3]: x + sym.Rational(1, 2) # Symbol + Rational
Out[3]: x + 1/2
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
A symbolic calculator
– Symbolic constants: sym.pi, sym.E, sym.I, sym.oo
– Symbolic functions:
● Number-theory functions: sym.factorial(n), sym.binomial(n, k)
● Power functions: sym.exp(x), sym.log(x), sym.sqrt(x), sym.root(x, n)
● Trigonometric functions: sym.cos(x), sym.sin(x), sym.tan(x)
●
Many more...
In [1]: f = 4*sym.pi**2
In [2]: sym.sqrt(f)
Out[2]: 2*pi
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
Basic manipulation
– Substitution
● Single variable: expr.subs(old, new)
● Multiple variables: expr.subs(iterable)
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
Basic manipulation
– Numerical evaluation
● Arbitrary precision: expr.evalf(n=15, subs=None)
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
Basic manipulation
– Numerical evaluation
● Be careful when combining with numpy
In [1]: a = sym.exp(2).evalf()
In [2]: type(a)
Out[2]: sympy.core.numbers.Float
In [3]: a_array = np.array([a, a])
In [4]: np.sin(a_array)
--------------------------------------
AttributeError ----> 1 np.sin(a_array)
'Float' object has no attribute 'sin'
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
Basic manipulation
– Numerical evaluation
● Be careful when combining with numpy
In [1]: a = sym.exp(2).evalf()
In [2]: a_float = float(a)
In [3]: type(a_float)
Out[3]: float
In [4]: a_array = np.array([a_float, a_float])
In [5]: np.sin(a_array)
Out[5]: array([0.89385495, 0.89385495])
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
Algebraic manipulation
– Simplification
● General purpose: sym.simplify(expr) or expr.simplify()
● Several more specific functions (trigsimp, powsimp, combsimp, ...)
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
Algebraic manipulation
– Polynomial manipulation
● Expansion: sym.expand(expr) or expr.expand()
● Factorisation: sym.factor(expr) or expr.factor()
● Collect powers: sym.collect(expr, syms) or expr.collect(syms)
●
Algebraic manipulation
– Trigonometric manipulation
● Simplification: sym.trigsimp(expr) or expr.trigsimp()
● Expansion: sym.expand_trig(expr)
In [1]: sym.trigsimp(sym.sin(x)/sym.sec(x))
Out[1]: sin(2*x)/2
In [2]: sym.trigsimp(sym.sinh(x)/sym.tanh(x))
Out[2]: cosh(x)
In [3]: sym.expand_trig(sym.sin(x + y))
Out[3]: sin(x)*cos(y) + sin(y)*cos(x)
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
Calculus
– Sums and products
● Syntax: sym.summation(expr, *args), sym.product(expr, *args)
● The range represented by tuples (index, a, b) is collected in *args
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
Calculus
– Taylor series
● Syntax: sym.series(expr, *args) or expr.series(*args)
● The variable, expansion point and order are collected in *args
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
Calculus
– Limits
● Syntax: sym.limit(expr, *args) or expr.limit(*args)
● The variable and limit point are collected in *args
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
Calculus
– Differentiation
● Syntax: sym.diff(expr, *args) or expr.diff(*args)
● Variables and/or the derivative order are collected in *args
In [1]: sym.sin(x).diff()
Out[1]: cos(x)
In [2]: sym.diff(sym.sin(x), x, 3)
Out[2]: -cos(x)
In [3]: sym.diff(sym.sin(x*y), x, 2, y, 1)
Out[3]: -y*(x*y*cos(x*y) + 2*sin(x*y))
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
Calculus
– Integration
● Syntax: sym.integrate(expr, *args) or expr.integrate(*args)
● Indefinite: variables are collected in *args
● Definite: tuples (var, a, b) are collected in *args
In [1]: sym.cos(x).integrate()
Out[1]: sin(x)
In [2]: sym.integrate(sym.cos(x), x)
Out[2]: sin(x)
In [3]: sym.integrate(sym.exp(-x), (x, 0, sym.oo))
Out[3]: 1
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
Calculus
– Numerical evaluation
● For integrals/sums/products: combine evalf with sym.Integral/sym.Sum/sym.Product
●
Can be useful when symbolic computation is very hard
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
Linear algebra
– Matrix data type
– Matrix creation via sym.Matrix
●
List of lists, flattened list, or function as input
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
Linear algebra
– Special matrices
● Common: sym.eye(rows, cols=None), sym.ones(...), sym.zeros(...)
● Diagonal: sym.diag(*values)
In [1]: A = sym.eye(2)
In [2]: B = sym.ones(2, 3)
In [3]: C = sym.diag(3, B, 2)
In [4]: C
Out[4]: Matrix([[3, 0, 0, 0, 0],
...: [0, 1, 1, 1, 0],
...: [0, 1, 1, 1, 0],
...: [0, 0, 0, 0, 2]])
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
Linear algebra
– Matrix manipulation
●
Indexing and slicing (pass by value)
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
Linear algebra
– Matrix manipulation
● Access: M.row(i), M.col(i)
● Deletion (in-place): M.row_del(i), M.col_del(i)
● Insertion (copy): M.row_insert(i, matrix), M.col_insert(i, matrix)
In [7]: M.row(1)
Out[7]: Matrix([[3, 4, x]])
In [8]: M.row_insert(0, sym.Matrix([[1, -2, 3]]))
Out[8]: Matrix([[1, -2, 3],
...: [0, 1, 2],
...: [3, 4, x]])
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
Linear algebra
– Matrix operations
● Basic arithmetic operations (+, -, *, ** )
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
Linear algebra
– Matrix operations
● Transpose: M.T
● Determinant: M.det()
● Inverse: M.inv()
● Nullspace: M.nullspace()
● Columnspace: M.columnspace()
● Eigenvalues: M.eigenvals(), M.eigenvects()
In [5]: M.det()
Out[5]: -x - 2
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
Linear algebra
– Matrix decompositions
● Diag: P, D = M.diagonalize()
● LU: L, U, perm = M.LUdecomposition()
● Cholesky: C = M.cholesky()
● QR: Q, R = M.QRdecomposition()
– And solvers of M * x = b based on decompositions
● Diag: x = M.diagonal_solve(b)
● LU: x = M.LUsolve(b)
● Cholesky: x = M.cholesky_solve(b)
● QR: x = M.QRsolve(b)
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
Linear algebra
– Example
Lab Calc
2023-2024
SymPy: Symbolic Mathematics
●
Solvers
– Univariate equation
● general: sym.solveset(equation, symbol=None)
● polynomial: sym.roots(equation, symbol=None)
●
Equation assumed to equal zero
●
Solvers
– System of equations
● System of linear eqns.: sym.linsolve(eqns, *symbols)
● System of nonlinear eqns.: sym.nonlinsolve(eqns, *symbols)
●
Equations assumed to equal zero
●
Pretty printing
– Default printing is often string-based
In [1]: sym.Integral(sym.sqrt(1/x), x)
Out[1]: Integral(sqrt(1/x), x)
In [1]: sym.init_printing(pretty_print=True)
In [2]: sym.Integral(sym.sqrt(1/x), x)
Out[2]:
Lab Calc
2023-2024