Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Symphy中的inv()未返回正确的值_Python_Matrix_Sympy - Fatal编程技术网

Python Symphy中的inv()未返回正确的值

Python Symphy中的inv()未返回正确的值,python,matrix,sympy,Python,Matrix,Sympy,我有一个可变密度矩阵,Qθ1和θ2为同类型符号 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] 当我调用逆函数时,我得到: In[13]: Q_inv=Q.inv

我有一个可变密度矩阵,
Q
<代码>θ1和
θ2
为同类型
符号

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]
当我调用逆函数时,我得到:

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]
我应该得到的是:

Out[X]:  [cos(theta1),  sin(theta1),  0,    0]
         [-sin(theta1),  cos(theta1), 0,    0]
         [          0,            0,  1, -980]
         [          0,            0,  0,    1]

你有没有想过这里会出什么问题?

这其实没什么问题。在第一个矩阵条目中,输出中有
-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]

这其实没什么错。在第一个矩阵条目中,输出中有
-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]

非常感谢。我评估了excel中的差异,结果不一样,一定是抄错了什么。非常感谢。我在excel中评估了这些差异,结果是不同的,一定是复制了错误的东西。