An Introduction to Sympy¶
Copyright (C) 2020 Andreas Kloeckner
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sympy variables are created using unique string identifiers.
import sympy as sp
import numpy as np
x = sp.Symbol("x")
y = sp.Symbol("y")
z = sp.Symbol("z")
One can form expression from symbols. Sympy expressions are made up of numbers, symbols, and sympy functions.
expression = x**2. + y**2. + z ** 2.
expression
x**2.0 + y**2.0 + z**2.0
Two expressions may be added together to form a new one.
other_expression = x**2.
expression += other_expression
expression
2*x**2.0 + y**2.0 + z**2.0
One can form sympy Matrix
objects.
sp.Matrix([[1,2],[3,4]])
Matrix([ [1, 2], [3, 4]])
An important Matrix
function is eye(n)
, which forms a $n \times n$ identity matrix.
sp.eye(3)
Matrix([ [1, 0, 0], [0, 1, 0], [0, 0, 1]])
One can stuff expressions into matrices, too.
# One can stuff expressions into matrices
f1 = x**2.+y**2-z**2.
f2 = 2*x + y + z
function_matrix = sp.Matrix([f1,f2])
function_matrix
Matrix([ [x**2.0 + y**2 - z**2.0], [ 2*x + y + z]])
One may compute the Jacobian of vector valued functions, too.
function_matrix.jacobian([x,y,z]) # pass in a list of Sympy Symbols to take the Jacobian
Matrix([ [2.0*x**1.0, 2*y, -2.0*z**1.0], [ 2, 1, 1]])
Sympy expressions can be evaluated by passing in a Python dictionary mapping Symbol Symbol
s to specific values.
x_val = 1.0
y_val = 2.0
z_val = 3.0
values={"x":x_val,"y":y_val,"z":z_val}
f1.subs(values)
-4.00000000000000
One can even valuate the Jacobian of functions.
J_mat = function_matrix.jacobian([x,y,z]).subs(values)
J_mat
Matrix([ [2.0, 4.0, -6.0], [ 2, 1, 1]])
To convert a Sympy Matrix
into a Numpy array, one may use the following:
np.array(J_mat)
array([[2.00000000000000, 4.00000000000000, -6.00000000000000], [2, 1, 1]], dtype=object)
After evaluating an expression in Sympy, the return type is a sympy.Float
.
However, this is not readily usable by Numpy. Therefore, consider casting sympy.Float
to a numpy.float64
.
J=np.array(J_mat).astype(np.float64)
J
array([[ 2., 4., -6.], [ 2., 1., 1.]])
At this point, one cna do all the usual stuff one would in Numpy.
J.T@J
array([[ 8., 10., -10.], [ 10., 17., -23.], [-10., -23., 37.]])
Symp's Lambdify
can help increase the speed of Sympy's numerical computations.
function_matrix.subs(values)
Matrix([ [-4.0], [ 7.0]])
from sympy.utilities.lambdify import lambdify
array2mat = [{'ImmutableDenseMatrix': np.array}, 'numpy']
lam_f_mat = lambdify((x,y,z), function_matrix, modules=array2mat)
lam_f_mat(1,2,3)
array([[-4.], [ 7.]])
Or if it is more convenient to have the function evaluation occur from a list of some sort, the Python *
operator on lists can help.
lam_f_mat(*[1,2,3])
array([[-4.], [ 7.]])