如何解Python中的矩阵微分方程?

How to solve matrix differential equations in Python?

dx/dt = Ax 其中 Ax 属于 n x n 数组。 我曾尝试在 scipy.integrate 中使用 solve_ivpodeint 功能,但这两个功能仅适用于 n x 1 数组。

积分器期望 ODE 函数使用一个平坦的一维数组作为状态变量,returns 一个同样平坦的导数向量。此输入数组包含矩阵的条目。要对其执行矩阵运算,您需要从平面输入数组构建一个矩阵,最后将其反转为导数函数的输出

x0 = x0.reshape(-1);         # make data 1-dimensional
def odefun(t,x):
    x=x.reshape([n,n]);      # restore to matrix form
    dx=A.dot(x);             # perform matrix operations
    return dx.reshape(-1);   # return 1-dimensional vector

sol = solve_ivp(odefun, [t0, tf], x0)