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.
i * i = -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.

z = a + b i, z' = a - b i, |z| = sqrt(z * z') = sqrt(a^2 + b^2)

Using the properties above, we can describe multiplication for complex numbers

z1 = a1 + b1 i, z2 = a2 + b2 i, z1 * z2 = (a1 a2 - b1 b2) + (a1 b2 + b1 a2) i

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.
i * i = -1, j * j = -1, k * k = -1

When you multiply two of these numbers together, they behave similarly to cross products of the unit basis vectors.

i * j = - j * i = k, j * k = - k * j = i, k * i = - i * k = j

The conjugate and magnitude of a quaternion are found in much the same way as complex conjugate and magnitude.

q = w + x i + y j + z k, q' = w - x i - y j - z k, |q| = sqrt(q * q') = sqrt(w^2 + x^2 + y^2 + z^2)
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,


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 q1 and q2 are unit quaternions representing two rotations. We want to perform q1 first and then q2. To do this, we apply q2 to the result of q1, regroup the product using associativity, and find that the composite rotation is represented by the quaternion q2*q1.


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 pp120-126)

The complex numbers are a field. This means that:

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, q1*q2 != q2*q1. 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 cross-product 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, pp351-354 - 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 q1. Also each term depends linearly on the coefficients for q2. So, we can write the product of two quaternions in terms of a matrix multiplication.

When the matrix Lrow(q1) multiplies a row vector q2, the result is a row vector representation for q1 * q2.

When the matrix Rrow(q2) multiplies a row vector q1, the result is also a row vector representation for q1 * q2.

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 left-multiplication by q

and a matrix form for right-multiplication by q'.

The resulting rotation matrix is the product of these two matrices.

q * P * q' = q * ( P * q') = q * (P Rrow(q')) = (P Rrow(q')) Lrow(q) = P (Rrow(q') Lrow(q)) = P Qrow(q)


So using this matrix, we could compute Protated 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 x-axis, one about the y-axis, and then one about the z-axis. For example, consider an airplane pointing along the x-axis with the z-axis pointing up. We can represent any pose by

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.

References



Last modified: Wed Feb 2 11:01:38 2000
Laura Downs (laura@cs.berkeley.edu)