symPy中的inv()没有返回正确的值

Chr*_*ris 1 python matrix sympy

我有一个MutableDenseMatrix , Q. theta1并且theta2属于SymPy类型symbol.

In[12]:  Q
Out[12]: [cos(theta1), -sin(theta1), 0,   0]
         [sin(theta1),  cos(theta1), 0,   0]
         [          0,            0, 1, 980]
         [          0,            0, 0,   1]
Run Code Online (Sandbox Code Playgroud)

当我打电话给逆时,我得到:

In[13]:  Q_inv=Q.inv()
Out[13]: [-sin(theta1)**2/cos(theta1) + 1/cos(theta1), sin(theta1), 0,    0]
         [                               -sin(theta1), cos(theta1), 0,    0]
         [                                          0,           0, 1, -980]
         [                                          0,           0, 0,    1]
Run Code Online (Sandbox Code Playgroud)

当我应该得到的是:

Out[X]:  [cos(theta1),  sin(theta1),  0,    0]
         [-sin(theta1),  cos(theta1), 0,    0]
         [          0,            0,  1, -980]
         [          0,            0,  0,    1]
Run Code Online (Sandbox Code Playgroud)

关于这里可能出现什么问题的任何想法?

Saj*_*ngh 5

对此没有任何错误.在您的第一个矩阵条目中,您-sin(theta1)**2/cos(theta1) + 1/cos(theta1)的输出和cos(theta1)预期结果中,实际上,由于1 - sin(theta1)**2 = cos(theta1)**2标准三角标识,它们是等效的.

sympy有一个被调用的函数trigsimp会将方程式减少到你想要的形式.

>>> Q
[cos(theta1), -sin(theta1), 0,   0],
[sin(theta1),  cos(theta1), 0,   0],
[          0,            0, 1, 980],
[          0,            0, 0,   1]
>>> Q.inv()
[-sin(theta1)**2/cos(theta1) + 1/cos(theta1), sin(theta1), 0,    0],
[                               -sin(theta1), cos(theta1), 0,    0],
[                                          0,           0, 1, -980],
[                                          0,           0, 0,    1]
>>> 
>>> sympy.trigsimp(Q.inv())
[ cos(theta1), sin(theta1), 0,    0],
[-sin(theta1), cos(theta1), 0,    0],
[           0,           0, 1, -980],
[           0,           0, 0,    1]
Run Code Online (Sandbox Code Playgroud)