Chris
Chris

Reputation: 10101

inv() in symPy not returning proper value

I have a MutableDenseMatrix, Q. theta1 and theta2 are of SymPy type 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]

When I call the inverse, I get:

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]

When what I should be getting is:

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

Any thoughts on what might be going wrong here?

Upvotes: 1

Views: 71

Answers (1)

Sajjan Singh
Sajjan Singh

Reputation: 2553

There's nothing actually wrong about that. In your first matrix entry, you have -sin(theta1)**2/cos(theta1) + 1/cos(theta1) in your output and cos(theta1) in the expected result, which are, in fact, equivilent since 1 - sin(theta1)**2 = cos(theta1)**2 by the standard trigonometric identity.

sympy has a function called trigsimp that will reduce the equation to the form you want.

>>> 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]

Upvotes: 5

Related Questions