[Math] Why does multiplying a Quaternion and a Vector yield these results


Today I coded the multiplication of quaternions and vectors in Java. This is less of a coding question and more of a math question though:

Quaternion a = Quaternion.create(0, 1, 0, Spatium.radians(90));
Vector p = Vector.fromXYZ(1, 0, 0);
System.out.println(a + " * " + p + " = " + Quaternion.product(a, p));
System.out.println(a + " * " + p + " = " + Quaternion.product2(a, p));

What I am trying to do is rotate a point $\mathbf{p}$ using the quaternion $\mathbf{q}$. The functions product() and product2() calculate the product in two different ways, so I am quite certain that the output is correct:

(1.5707964 + 0.0i + 1.0j + 0.0k) * (1.0, 0.0, 0.0) = (-1.0, 0.0, -3.1415927)
(1.5707964 + 0.0i + 1.0j + 0.0k) * (1.0, 0.0, 0.0) = (-1.0, 0.0, -3.1415927)

However, I can't wrap my head around why the result is the way it is. I expected to rotate $\mathbf{p}$ 90 degrees around the y-axis, which should have resulted in (0.0, 0.0, -1.0).

Wolfram Alpha's visualization also suggests the same:

So what am I doing wrong here? Are really both the functions giving invalid results or am I not understanding something about quaternions?

Best Answer

I dont well understand your code, but it seems that you have multiplied only one way the quaternion by the vector, and this is wrong.

The rotation of the vector $\vec v = \hat i$ by $\theta=\pi/2$ around the axis $\mathbf{u}=\hat j$ is represented by means of quaternions as ( see Representing rotations using quaternions):

$$ R_{\mathbf{u},\pi/2}(\vec v)= e^{\frac{\pi}{4}\hat j}\cdot \hat i \cdot e^{-\frac{\pi}{4}\hat j} $$ where the exponential is given by the formula ( see Exponential Function of Quaternion - Derivation): $$ e^{\frac{\pi}{4}\hat j}=\cos\frac{\pi}{4} +\hat j \sin\frac{\pi}{4} $$ so we have: $$ R_{\mathbf{u},\pi/2}(\vec v)=\left( \frac{\sqrt{2}}{2}+\hat j\frac{\sqrt{2}}{2} \right)(\hat i) \left(\frac{\sqrt{2}}{2}-\hat j\frac{\sqrt{2}}{2} \right)= $$ $$ =\frac{1}{2}\hat i-\frac{1}{2}\hat k-\frac{1}{2}\hat k-\frac{1}{2}\hat i=-\hat k $$

Note we need two multiplications by a unitary quaternion and its inverse, with an angle that is one half of the final rotation angle.

Related Question