In this Python scientific computing, signal processing, optimization, and control theory tutorial you will learn how to automatically compute Jacobians of vector functions in Python by using the symbolic Python library called SymPy. Furthermore, you will learn how to automatically generate code that will return the Jacobian matrix for the specified input vector. That is, you will learn how to write a Python script that will evaluate the value of the Jacobian matrix for the specified input vector (point in -dimensional space). This tutorial is very important for engineers and students who want to develop optimization solvers or signal processing and control engineering algorithms in Python. Namely, a number of optimization solvers and control engineering algorithms are based on the computation of the Jacobians of nonlinear functions.
Here is a brief summary of the video tutorial. First, we provide a brief summary of Jacobian matrices of vector functions. We briefly introduce vector functions and explain how to compute Jacobian matrices of vector functions. Then, in order to test our Python implementation, we construct an example of a nonlinear vector function. We explain how to analytically compute the Jacobian matrix of this function. The computed analytical form of the Jacobian is used to test our Python implementation. Finally, we explain how to compute the Jacobian matrices in Python by using the SymPy library and how to generate Python functions out of computed symbolic expressions.
The YouTube video accompanying this webpage tutorial is given below.
Brief Summary of Jacobians of Vector Functions
Before we explain how to write Python scripts, we first need to briefly summarize the concept of Jacobian matrices. Consider the following vector function that takes as an input the -dimensional vector and it maps this vector into the -dimensional vector:
(1)
where the vector is defined by
(2)
The nonlinear vector function produces the -dimensional vector
(3)
whose entries are functions , that map the entries of the vector into scalar numbers.
The Jacobian matrix of the function is by dimensional matrix of partial derivatives defined by
(4)
The first row of this matrix consists of partial derivatives of with respect to , respectively. Similarly, the second row of this matrix consists of partial derivatives of with respect to , respectively. In the same manner, we construct the other rows of the Jacobian matrix.
Python Script for Symbolically Computing the Jacobian Matrix
Here, we present the Python script for symbolically computing the Jacobian matrix and for creating the Python function that will return the numerical values of the Jacobian matrix for the given input vector . To verify the Python implementation, let us consider the following test-case function
(5)
where is
(6)
and
(7)
The Jacobian of this function is
(8)
By computing these partial derivatives, we obtain
(9)
The Python script shown below is used to symbolically compute the Jacobian matrix and to generate a Python function that returns the numerical values of the Jacobian matrix.
# import the necessary libraries
import numpy as np
# sympy library - we import everything
from sympy import *
# this function is used to generate nice
# prints of symbolic expressions
init_printing()
# define the symbolic vector x
x=MatrixSymbol('x',4,1)
# here, we define our function f as 4 by 1 matrix
f=Matrix([[x[0]*x[1]],
[sin(x[0])],
[cos(x[2])],
[x[2]*E**(x[3])]])
# here, we symbolically compute the Jacobian matrix
JacobianSymbolic=f.jacobian(x)
# here, we create a Python function that returns the
# numerical value of the Jacobian
JacobianFunction=lambdify(x,JacobianSymbolic)
# we create a test case vector
testCaseVector=np.array([[1],[1],[1],[1]])
# here, we evaluate the numerical value of the Jacobian matrix
JacobianNumerical=JacobianFunction(testCaseVector)
We define the symbolic vector “x” for our computation as follows
# define the symbolic vector x
x=MatrixSymbol('x',4,1)
The nonlinear vector function “f” is defined by
=Matrix([[x[0]*x[1]],
[sin(x[0])],
[cos(x[2])],
[x[2]*E**(x[3])]])
Here, it should be kept in mind that the SymPy library uses “E” to denote the exponential function . We use the following code line to compute the Jacobian matrix
# here, we symbolically compute the Jacobian matrix
JacobianSymbolic=f.jacobian(x)
Then, we create a Python function out of the return symbolic matrix as follows
# numerical value of the Jacobian
JacobianFunction=lambdify(x,JacobianSymbolic)
To do that, we use the SymPy function “lambdify”. This function is used to create Python functions out of symbolic expressions. The first argument of this function is the symbolic variable, and the second argument is the symbolic expression.
Finally, we evaluate the Jacobian at a test vector.
# we create a test case vector
testCaseVector=np.array([[1],[1],[1],[1]])
# here, we evaluate the numerical value of the Jacobian matrix
JacobianNumerical=JacobianFunction(testCaseVector)
The result that is stored in “JacobianNumerical” is a NumPy array (matrix) of numerical values that can be used to further computations.