# 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?

• The product $z_1z_2$ corresponds geometrically to doing rotation attatched to $z_2$ and then rotation attatched to $z_1$. Be careful about ordering though since quaternion multiplication is noncommutative. – fretty Apr 13 '13 at 11:12

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).

• Your quaternion multiplication for applying rotation is opposite from the standard convention: should be v' = qvq' Not a big deal, as this would merely mean rotation in the opposite direction. – Codesmith Nov 13 '16 at 2:01

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).

• There’s nothing to calculate to find the inverse of a rotation matrix. Rotation matrices can simply be transposed to get their inverse. – johnbakers Jun 9 '19 at 9:57
• @johnbakers As the answer already states, rotation matrix needs to be transposed to get the inverse. contrary to what you're thinking, 3x3 matrix transpose will be about 6 or 9 moves to swap the off-diagonal elements. If you use SIMD, those would be shuffles. Compare that to a single scalar negation that is easy to execute even if the quaternion sits in a SIMD register. – the swine Jun 11 '19 at 1:40
• Sorry I missed that part in your comment. Though this is the first I've seen that merely negating one of the four elements of the quaternion (w) gives the inverse; the calculation of the conjugate or inverse seems to be slightly more involved formula on the various wikis I've looked at. You are saying that just negate the w is the same effect? That would make sense if the elements of the quaternion directly corresponded to the axis and its angle, in which case negating the w would be the inverse, but in quaternions the elements don't directly map that way. – johnbakers Jun 12 '19 at 8:43
• @johnbakers In fact, they do; the quaternion representation of a rotation of $\theta$ degrees about the axis $\mathbf{v}$ (represented as a unit-length purely imaginary quaternion) is $\cos(\theta/2)+\sin(\theta/2)\mathbf{v}$. This implies that the rotation of $-\theta$ about that axis - the inverse - is $\cos(\theta/2)-\sin(\theta/2)\mathbf{v}$, i.e. the conjugate; in terms of computer representation this is negating the x/y/z components of the quaternion. But since $\mathbf{q}$ and $-\mathbf{q}$ represent the same rotation, rather than negating x, y, and z we can just negate w. – Steven Stadnicki Jul 27 '19 at 16:46

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}$).