Afir
Afir

Reputation: 483

Reverse calculation of matrix in Python

I need to do the reverse calculation of matrix, given by:

rotation=np.matrix([[1, 0, 0, 0], [0, math.cos(4*math.radians(a)), math.sin(4*math.radians(a)), 0], [0, math.sin(4 * math.radians(a)), -math.cos(4 * math.radians(a)), 0], [0, 0, 0, -1]])
a=60

X=np.matrix('1 ;-1 ;0 ;0')

a1=rotation*X

a1=[[ 1.       ]
 [ 0.5      ]
 [ 0.8660254]
 [ 0.       ]]

Is it possible to do the reverse calculation to find the value of a, given rotation and x?

Thank you.

Upvotes: 0

Views: 216

Answers (1)

hiro protagonist
hiro protagonist

Reputation: 46899

if i am guessing right, you mean this:

first, your matrix is not a rotation matrix; the sign is in the wrong place...

then i will ignore the factor of 4; you may reinsert that yourself.

import numpy as np
from numpy.linalg import norm
from numpy import dot, cos, sin, arccos

x = np.array((1, -1, 0, 0))
y = np.array((1, 0.5, 0.8660254, 0))

# just considering indices 1 and 2
cos_a = dot(x[1:3], y[1:3])/(norm(x[1:3]) * norm(y[1:3]))

a_rad = arccos(cos_a)
a_deg = np.rad2deg(a_rad)

print(a_deg)  # 120

and to check that the (correct) brotation indeed reproduces y:

rot = np.array((
    (1, 0, 0 , 0),
    (0, cos(a_rad), -sin(a_rad), 0),
    (0, sin(a_rad), cos(a_rad), 0),
    (0, 0, 0, 1))
)

print(dot(rot, x))  # [ 1.         0.5       -0.8660254  0.       ]

Upvotes: 1

Related Questions