# Summary

1. Equivalent in power to Gibbs vectors, with dot and cross products.
2. Easier than vectors for 3D rotations, e.g.,
1. combining multiple rotations
2. animating a rotation
3. other possibilities for rotations:
1. matrices
2. Euler_Angles - very bad for animation, and suffer from Gimbal_lock
3. Rotation3D
4. Invented by WR Hamilton in 1843 as a 3D analog to complex numbers, to do 3D rotations. However, what we use today is a distilled essence of the complicated way that Hamilton presented them. (That is true for many mathematical ideas.)

1. Quaternions start from the intuitive axis-angle API.
2. Animating a large rotation in small steps (by varying the angle slowly) is easy. In contrast, stepping the 3 Euler angles does not work well, and there's no obvious way to gradually apply a {$3\times3$} rotation matrix, {$M$}. (You could compute {$M^{1/100}$} and apply it 100 times, but that computation is messy.)
3. When combining multiple rotations, the axis and angle of the combo is easy to find.
4. Having only 4 parameters to represent the 3 degrees of freedom of a 3D rotation is the right number. Using only 3 parameters, as Euler angles do, causes gimbal lock. That is, you cannot always represent a smooth rotation by smooth changes of the 3 parameters. OTOH, using 9 parameters, as with a matrix, gives too much opportunity for roundoff errors causing the matrix not to be exactly a rotation. (You can snap the matrix back to a rotation matrix, but that's messy.)

# The math

1. Review complex numbers: {$z=x+iy$}. {$x$} and {$y$} are real. The only things we know about {$i$} are these:
1. We can multiply it by a real, and add it to a real, {$2+3i$}.
2. We can add multiples, e.g.: {$2i+3i=5i$}.
3. When multiplying {$i$} by {$i$}, we can replace {$i^2$} by {$-1$}. (It is wrong to call {$i$} the square root of -1. Doing that leads to paradoxes because -1 has 2 square roots. There is no way to tell which one is the positive square root; that concept has no meaning for an imaginary, and trying to do that causes paradoxes.)
2. Quaternions have 3 things like {$i$} (they're called indeterminates): {$i$}, {$j$}, and {$k$}.
3. You can add them and form expressions like {$1+2i+3j+4k$}.
4. When multiplying, you combine them thus:
1. {$i^2=j^2=k^2= -1$}
2. {$ij=-ji=k, jk=-kj=i, ki=-ik=j$} Note that they are not commutative. That is required since 3D rotations are not commutative.
5. To rotate using quaternions, take a rotation with axis {$(a_x,a_y, a_z)$} and angle {$\theta$}. The axis must be normalized: {$a_x^2+a_y^2+a_z^2=1$}.
6. Form the quaternion
{$$q= \cos\left(\frac{\theta}{2}\right) + \sin\left(\frac{\theta}{2}\right) \left(a_x i + a_y j + a_z k \right)$$}
7. Form its conjugate
{$$q^*= \cos\left(\frac{\theta}{2}\right) - \sin\left(\frac{\theta}{2}\right) \left(a_x i + a_y j + a_z k \right)$$}
8. The point you want to rotate is {$(p_x,p_y,p_z)$}; form the quaternion
{$$p = p_x i + p_y j + p_z k$$}
9. Then the rotated {$p$} is
{$$p' = q p q^*$$}
10. To rotate by {$q_1$} and then {$q_2$}, compute
{$$p' = q_2 q_1 p q_1^* q_2^*$$}
11. Optimize it by computing the combined rotation
{$$q = q_2 q_1$$}
12. and then applying it:
{$$p' = q p q^*$$}

# Sample question

What rotation does this quaternion represent: {$q = 0.707 + 0.707 j$}

Answer: a {$90^\circ$} rotation about the axis {$(0,1,0)$}

# Examples

Let the point {$p=(1,2)$}. The corresponding complex number is {$c=1+2i$}. Suppose you want to rotate it by {$\theta=90^\circ=\pi/2$} radians. That is equivalent to multiplying {$c$} by {$e^{i\theta}=e^{i\pi/2}=i$}.

So, {$c'=c e^{i\theta} = (1+2i)i = -2+i$}.

The corresponding 2D point is {$(-2,1)$}.

Now to quaternions in general.

Let {$q_1=(1,2,0,0)$} and {$q_2=(3,0,4,0)$}. Then {$q_1+q_2=(4,2,4,0)$}.

Now to 3D and quaternions.

For example, the 3D point {$(1,0,2)$} corresponds to the quaternion {$1i+0j+2k$}.