I've received some code (which I didn't write) and decided at some point to write test cases for the Quaternion math implementation.
I used Wolfram alpha to get the result q1 * q2, where:
q1 = (4.0 + 1.0i + 2.0j + 3.0k)
q2 = (8.0 - 5.0i + 6.0j - 7.0k)
Wolfram alpha calculates the result to be (46 - 44i + 32j + 12k)
My code calculates the result to be (46 + 20i + 48j - 20k)
This is not equal to Wolfram Alpha's calculation of q1 * q2 (which meant my test failed), but interestingly, it's equal to q2 * q1.
Similarly, q2 * q1 calculated by my code is equal to Wolfram Alpha's result of q1 * q2
Normally I would think that my multiplication function is performed incorrectly, but the function is used several times in the program with correct results.
I was wondering if this is considered a bug, or just a design choice. Is this quaternion multiplication performed in a "left-handed" manner, compared to the norm, which uses right handed math? I do know that handedness matters for rotation matrices, but I haven't read anything about Quaternions. Is there another explanation for this?
It sounds like your multiplication function is actually trying to give the net quaternion for a composition of rotations. That is, you have a function
mult(q1, q2)that finds the net quaternionqnetcorresponding to a rotation byq1first andq2subsequent to that.More explicitly, if the quaternion $q_1$ corresponds to a rotation map $R_1$, then for any vector $a$ (which we represent as a pure imaginary quaternion), the rotation takes the form
$$R_1(a) = q_1 a q_1^{-1}$$
And if you have another rotation $R_2$ associated with a quaternion $q_2$, then the whole rotation takes the form
$$R_2 R_1(a) = q_2 q_1 a q_1^{-1} q_2^{-1}$$
So the net quaternion of the rotation is $q_\text{net} = q_2 q_1$.
Still, I consider it a somewhat easy trap to write the multiplication function to switch left and right in this way, convenient though it may be to keep track of which rotation is "first".