Compute the Jacobian matrix in Python

前端 未结 6 1734
温柔的废话
温柔的废话 2021-02-02 01:24
import numpy as np


a = np.array([[1,2,3],
              [4,5,6],
              [7,8,9]])


b = np.array([[1,2,3]]).T

c = a.dot(b) #function

jacobian = a # as partial         


        
6条回答
  •  有刺的猬
    2021-02-02 01:51

    In python 3, you can try sympy package:

    import sympy as sym
    
    def Jacobian(v_str, f_list):
        vars = sym.symbols(v_str)
        f = sym.sympify(f_list)
        J = sym.zeros(len(f),len(vars))
        for i, fi in enumerate(f):
            for j, s in enumerate(vars):
                J[i,j] = sym.diff(fi, s)
        return J
    
    Jacobian('u1 u2', ['2*u1 + 3*u2','2*u1 - 3*u2'])
    

    which gives out:

    Matrix([[2,  3],[2, -3]])
    

提交回复
热议问题