What does multiplication of two quaternions give?
I'm using quaternions as a means to rotate an object in the application I'm developing. If one quaternion represents a rotation and the second quaternion represents another rotation, what does their multiplication represent? Many web sites talk about formulas and matrices, but I want to know what exactly are we doing multiplication for?
Solution 1:
The multiplication of quaternions represents composing the two rotations: perform one rotation and then perform the other one. It's clear that this should represent a rotation (imagine rotating, say, a bowling ball in place). What's not necessarily immediately clear — and this is what in some sense 'motivates' the quaternion representation of a rotation — is the fact that that every rotation in three dimensions can be written as a rotation by some amount about a single axis: for instance, the combination of a rotation by $\frac\pi4$ about the $x$-axis followed by a rotation by $\frac\pi3$ about the axis $(1,1,1)$ can still be written as a single rotation about some axis.
As pointed out in the comments, composition of rotations isn't necessarily commutative (and thus the multiplication of quaternions can't be); for instance, imagine labeling the six faces of a cube $\pm X, \pm Y, \pm Z$ according to which way they're facing, and then looking at the $+X$ face with the $+Y$ axis to your right and the $+Z$ axis up. Now, perform two (clockwise, $90^\circ$) rotations: one about the $+Z$ axis and one about the $+Y$ axis. Doing the rotation about $Z$ first will bring the $+Y$ face of the cube to face you, with the $+Z$ face still up; then rotating about $Y$ will bring the $-Z$ face of the cube facing you, with $+Y$ on top. By contrast, if you rotate about the $+Y$ face first then $-Z$ will be facing you, with $+X$ up and $+Y$ to the right; then rotating about the $+Z$ axis will have $+Y$ facing you with $+X$ up.
Also, you need to be a little careful about 'mixing metaphors' with your representations: quaternions represent rotations and multiplication of quaternions represents composition of those represented rotations, but — for instance — if $q$ is some rotation and $v$ is a vector, then multiplying $qv$ (representing $v$ via the 'fully imaginary' quaternion $v_x{\bf i}+v_y{\bf j}+v_z{\bf k}$) does not represent the rotation of that vector according to the given rotation (it's hard to see how it could, since generally such a multiplication would have a non-zero real component); instead, the result of rotating by the specified quaternion is calculated by $v_{\rm{rot}} = q^{-1}vq$ (and it can be shown that this value is also purely imaginary, so it can be converted 'back' to a three-vector consistently).
Solution 2:
Multiplication of quaternions works pretty much the same way the multiplication of rotation (or transformation) matrices works. So multiplication of two quaternions combines the rotations, just as multiplying matrices does (e.g. in OpenGL glRotate() function).
A nice thing is that multiplication of two normalized quaternions again produces a normalized quaternion.
Quaternion inversion (or just conjugate for the normalized case) creates the inverse rotation (the same rotation in the opposite direction). This is arguably easier to compute (on current computers) than to calculate inverse of a rotation matrix (just have to negate w in quaternion, instead of transposing a rotation matrix).
Solution 3:
Consider the quaternions as a four-dimensional real algebra $\mathbb{H}$, where elements have the form $a+bi+cj+dk$ subject to the muliplication rules $i^2=j^2=k^2=ijk=-1$. Further, consider the function $f:\mathbb{R}^3\to\mathbb{H}$ defined by
$f(x_1,x_2,x_3)=x_1i+x_2j+x_3k$
and the imaginary projection $\iota:\mathbb{H}\to\mathbb{R}^3$ given by
$\iota(a+bi+ck+dk)=(b,c,d)$.
Then, the cross product of two vectors $x,y\in\mathbb{R}^3$ can be given by
$x\times y=\iota(f(x)f(y))$.
A similar procedure also works for the seven-dimensional cross product and the octonions, with the same sort of maps $f':\mathbb{R}^7\to\mathbb{O}$ and $\iota':\mathbb{O}\to\mathbb{R}^7$
But as was pointed out by fretty, one needs to be careful about commutativity in $\mathbb{H}$ (and associativity for that matter in the case of $\mathbb{O}$).