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.

In [1]:
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.

In [2]:
expression = x**2. + y**2. + z ** 2.
expression
Out[2]:
x**2.0 + y**2.0 + z**2.0

Two expressions may be added together to form a new one.

In [3]:
other_expression = x**2.
expression += other_expression
expression
Out[3]:
2*x**2.0 + y**2.0 + z**2.0

One can form sympy Matrix objects.

In [4]:
sp.Matrix([[1,2],[3,4]])
Out[4]:
Matrix([
[1, 2],
[3, 4]])

An important Matrix function is eye(n), which forms a $n \times n$ identity matrix.

In [5]:
sp.eye(3)
Out[5]:
Matrix([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])

One can stuff expressions into matrices, too.

In [6]:
# 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
Out[6]:
Matrix([
[x**2.0 + y**2 - z**2.0],
[           2*x + y + z]])

One may compute the Jacobian of vector valued functions, too.

In [7]:
function_matrix.jacobian([x,y,z]) # pass in a list of Sympy Symbols to take the Jacobian
Out[7]:
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 Symbols to specific values.

In [8]:
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)
Out[8]:
-4.00000000000000

One can even valuate the Jacobian of functions.

In [9]:
J_mat = function_matrix.jacobian([x,y,z]).subs(values)
J_mat
Out[9]:
Matrix([
[2.0, 4.0, -6.0],
[  2,   1,    1]])

To convert a Sympy Matrix into a Numpy array, one may use the following:

In [10]:
np.array(J_mat)
Out[10]:
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.

In [11]:
J=np.array(J_mat).astype(np.float64)
J
Out[11]:
array([[ 2.,  4., -6.],
       [ 2.,  1.,  1.]])

At this point, one cna do all the usual stuff one would in Numpy.

In [12]:
J.T@J
Out[12]:
array([[  8.,  10., -10.],
       [ 10.,  17., -23.],
       [-10., -23.,  37.]])

Symp's Lambdify can help increase the speed of Sympy's numerical computations.

In [13]:
function_matrix.subs(values)
Out[13]:
Matrix([
[-4.0],
[ 7.0]])
In [14]:
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)
Out[14]:
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.

In [15]:
lam_f_mat(*[1,2,3])
Out[15]:
array([[-4.],
       [ 7.]])