3

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)
Frederik Petri
  • 451
  • 8
  • 24
  • 2
    I's appear that **Output2** is the correct one: (may you changed something in the code!!) (-0.37553122668322314, -0.2065883731602419, 0.01484188589166425) – AziMez Oct 20 '22 at 12:33
  • Hmm. The correct one should be Output1. I am trying to reproduce another persons calculations. He have the same input and gets (0.20736419033763884, -0.37378682006270764, 0.03473104416868859) but doesnt show the inbetweeen calculations. The code is identical. So the output keeps changing every second time I run the code in Pycharm – Frederik Petri Oct 21 '22 at 06:19
  • Using your code, my result is ***Output2***. – g2m.agent Aug 26 '23 at 18:09

1 Answers1

2

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])
Masilila
  • 504
  • 4
  • 9