sympy版本:1.2
假设求解矩阵方程
使用qtconsole输入下面程序进行求解
In [26]: from sympy import *
In [27]: from sympy.abc import *
In [28]: A=Matrix([[4,2,3],[1,1,0],[-1,2,3]])
In [29]: A
Out[29]:
Matrix([
[ 4, 2, 3],
[ 1, 1, 0],
[-1, 2, 3]])
In [30]: B=A-2*diag(1,1,1)
In [31]: B
Out[31]:
Matrix([
[ 2, 2, 3],
[ 1, -1, 0],
[-1, 2, 1]])
In [32]: B.inv()*A
Out[32]:
Matrix([
[ 3, -8, -6],
[ 2, -9, -6],
[-2, 12, 9]])
将结果验证一下:
In [38]: X=B.inv()*A
In [39]: X
Out[39]:
Matrix([
[ 3, -8, -6],
[ 2, -9, -6],
[-2, 12, 9]])
In [40]: A*X-A-2*X
Out[40]:
Matrix([
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
求解矩阵方程过程中注意的问题是左乘还是右乘问题,在此例中是B.inv()*A
,如果矩阵方程变为
In [35]: X=A*B.inv()
In [36]: X
Out[36]:
Matrix([
[ 3, -8, -6],
[ 2, -9, -6],
[-2, 12, 9]])
将结果验证一下:
X=A*B.inv()
X
Out[36]:
Matrix([
[ 3, -8, -6],
[ 2, -9, -6],
[-2, 12, 9]])
X*A-A-2*X
Out[37]:
Matrix([
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])