CS 184 home page
A note on these notes:
These notes on quaternions were created as a resource for
students taking
CS184 at UC Berkeley.
I am not doing any research related to quaternions and I do not claim to have
discovered anything original about them.
If your questions about quaternions are not answered here, please see the
references
at the end of this page for more information.

CS184: Using Quaternions to Represent Rotation
If you would like a hard copy of these notes, a gzipped postscript version can
be found in the file quatnotes.ps.
The LaTeX source can
be found in the file quatnotes.tex.
If you are interested in better understanding quaternions,
look at the
proof that quaternions can be used to compute rotations
Quaternions
Complex Numbers (a brief review)
As you know, the system of complex numbers is defined in terms of
i, a square root of 1.
Although i is not a real number, we can write any complex
number in terms of real numbers, using a real and a complex part.
We can also define a conjugate and a magnitude (or absolute value) for these numbers.
Using the properties above, we can describe multiplication for complex
numbers
Quaternions
Quaternions are an extension of complex numbers.
Instead of just i, we have three different numbers that are all
square roots of 1 labelled i, j, and k.
When you multiply two of these numbers together, they behave similarly
to cross products of the unit basis vectors.
The conjugate and magnitude of a quaternion are found in much the same
way as complex conjugate and magnitude.
Unit quaternions 

Quaternions are associative 

Quaternions are not commutative 

The inverse of a quaternion refers to the multiplicative
inverse (or 1/q) and can be computed by q^{1}=q'/(q*q')
If a quaternion q has length 1,
we say that q is a unit quaternion.
The inverse of a unit quaternion is its conjugate, q^{1}=q'
We can represent a quaternion in several ways,
 as a linear combination of 1, i, j, and k,
 as a vector of the four coefficients in this linear combination,
 or as a scalar for the coefficient of 1
and a vector for the coefficients of the imaginary terms.
We can write the product of two quaternions in terms of the (s,v) representation
using standard vector products in the following way:
Representing Rotations with Quaternions
We will compute a rotation
about the unit vector, u by an angle theta.
The quaternion that computes this rotation is
We will represent a point p in space by the quaternion P=(0,p)
We compute the desired rotation of that point by this formula:
You may want to confirm that q is a unit quaternion,
since that will allow us to use the fact that
the inverse of q is q' if q is a unit quaternion.
Concatenating Rotations
Suppose we want to perform two rotations on an object.
This may come up in a manipulation interface where each movement of
the mouse adds another rotation to the current object pose.
This is very easy and numerically stable with a quaternion
representation.
Suppose q_{1} and q_{2} are unit quaternions representing two
rotations. We want to perform q_{1} first and then q_{2}.
To do this, we apply q_{2} to the result of q_{1}, regroup
the product using associativity, and find that the composite rotation
is represented by the quaternion q_{2}*q_{1}.
Therefore, the only time we need to compute the matrix is when we want
to transform the object. For other operations we need only look at
the quaternions. A matrix product requires many more operations than
a quaternion product so we can save a lot of time and preserve more
numerical accuracy with quaternions than with matrices.
For the mathematically inclined
(ref: Topics in Algebra by I.N.Herstein pp120126)
The complex numbers are a field.
This means that:
 Complex numbers have an addition operation and a multiplication operation.
 Both operations are commutative and associative, i.e.
z_{1}+z_{2} = z_{2}+z_{1} and
(z_{1}+z_{2})+z_{3}
= z_{1}+(z_{2}+z_{3})
z_{1}z_{2} = z_{2}z_{1} and
(z_{1}z_{2})z_{3}
= z_{1}(z_{2}z_{3})
 The addition operation has an identity, 0, and an inverse, z+(z)=0.
 The multiplication operation has an identity, 1, and an
inverse for all numbers but z=0, z(z^{1})=1,
or z^{1}=1/z.
The inverse of a complex number is z^{1}=z'/(zz').
 The operations are distributive
(z_{1}+z_{2})z_{3}
= z_{1}z_{3}+z_{2}z_{3}
and
z_{3}(z_{1}+z_{2})
= z_{3}z_{1}+z_{3}z_{2}
Essentially what this says is that complex numbers act just like
numbers as we know them. We can add, multiply, subtract, and divide
them and we can use any of our standard algebraic manipulations.
Other examples of fields are the real numbers and the rational numbers.
The quaternions have all of the same properties except that
quaternion multiplication is not commutative.
In general,
q_{1}*q_{2} != q_{2}*q_{1}.
We call this structure a division ring.
This means that we can do any kind of arithmetic with quaternions as
long as we are careful to note the order of multiplication.
The inverse of a quaternion refers to the multiplicative
inverse (or 1/q) and can be computed by q^{1}=q'/(q*q')
The set of all 3x3 matrices (or all 4x4 matrices) with
standard matrix addition and multiplication is a ring but not
a division ring, because not all matrices have inverses.
The set of all 3D vectors with vector addition and
crossproduct is also a ring, but not a division ring, since the
set of vectors does not even have a multiplicative identity.
(Can you think of a vector v such that u x v = u for all u?)
A Matrix Representation for Quaternion Multiplication
(ref: Graphics Gems II, pp351354  warning: the final matrix is transposed!)
We can use the rules above to compute the product of two quaternions.
If you examine each term in this product, you can see that
each term depends linearly on the coefficients for q_{1}.
Also each term depends linearly on the coefficients for q_{2}.
So, we can write the product of two quaternions in terms of a matrix
multiplication.
When the matrix Lrow(q_{1}) multiplies a row vector q_{2}, the
result is a row vector representation for q_{1} * q_{2}.
When the matrix Rrow(q_{2}) multiplies a row vector q_{1}, the
result is also a row vector representation for q_{1} * q_{2}.
Computing Rotation Matrices from Quaternions
Now we have all the tools we need to use quaternions to generate a
rotation matrix for the given rotation.
We have a matrix form for leftmultiplication by q
and a matrix form for rightmultiplication by q'.
The resulting rotation matrix is the product of these two matrices.
q * P * q'
= q * ( P * q')
= q * (P R_{row}(q'))
= (P R_{row}(q')) L_{row}(q)
= P (R_{row}(q') L_{row}(q))
= P Q_{row}(q)
So using this matrix, we could compute P_{rotated} another way:
Euler Angles
In many fields Euler angles are used to represent rotations.
Any rotation can be broken down into a series of three rotations about
the major axes.
We can simulate any arbitrary rotation with one rotation about the
xaxis, one about the yaxis, and then one about the zaxis.
For example, consider an airplane pointing along the xaxis with
the zaxis pointing up.
We can represent any pose by
 the "roll" about the xaxis along the plane,
 the "pitch" about the yaxis which extends along the wings of
the plane,
 and the "yaw" or "heading" about the zaxis
as a vector (roll,pitch,yaw).
This representation is useful and intuitive in some cases,
such as this one, but Euler angles have some drawbacks.
 There is no universal standard for Euler rotations.
Different fields use different sequences of Euler angles, for
example some physicists use zyz as opposed to the
xyz system described above.
 Since any rotation can be represented by either a set of
Euler angles or a matrix, we should be able to convert between
them. However, computing the required angles is expensive and
can introduce errors.
 Interpolation between two poses represented in this system does
not follow the great arc between the rotations, but involves
wild swings about the canonical axes.
 Graphics Gems II, pp. 351354, pp. 377380
 Computer Graphics, Hearn & Baker, pp. 419420 & pp. 617618
 "Animating Rotation with Quaternion Curves", Ken Shoemake,
Computer Graphics V.19 N.3, 1985
Last modified: Wed Feb 2 11:01:38 2000
Laura Downs (laura@cs.berkeley.edu)