Reputation: 481
I am trying to multiply a quaternion with a vector following the formula q * v * q_conjugate(q)
I found in this thread. When I run the following code in Python, I get an output that looks correct. However, when I run the code again I get another output. How can that be?
Hope you can help!
def q_conjugate(q):
w, x, y, z = q
return (w, -x, -y, -z)
def q_mult(q1, q2):
w1, x1, y1, z1 = q1
w2, x2, y2, z2 = q2
w = w1 * w2 - x1 * x2 - y1 * y2 - z1 * z2
x = w1 * x2 + x1 * w2 + y1 * z2 - z1 * y2
y = w1 * y2 + y1 * w2 + z1 * x2 - x1 * z2
z = w1 * z2 + z1 * w2 + x1 * y2 - y1 * x2
return w, x, y, z
def qv_mult(q1, v1):
return q_mult(q_mult(q1, v1), q_conjugate(q1))[1:]
v = np.array([0.0, -0.118443, -0.412128, 0.006673])
q = np.array([0.921369, 0.027301, 0.049432, -0.384565])
res = qv_mult(q, v)
Output 1: (0.20736419033763884, -0.37378682006270764, 0.03473104416868859)
Output 2: (-0.37553122668322314, -0.2065883731602419, 0.01484188589166425)
Upvotes: 3
Views: 289
Reputation: 524
Easy answere. Use pyquaternion to rotate the vector. The correct answere should be output 1.
from pyquaternion import Quaternion
Quaternion([0.921369, 0.027301, 0.049432, -0.384565]).conjugate.rotate([-0.122545--0.004102, -0.214168-0.19796, -0.010722--0.017395])
Upvotes: 2