Quaternions and Rotations: (Com S 477/577 Notes)
Quaternions and Rotations: (Com S 477/577 Notes)
Yan-Bin Jia
1 Introduction
The development of quaternions is attributed to W. R. Hamilton in 1843. Legend has it that
Hamilton was walking with his wife Helen at the Royal Irish Academy when he was suddenly
struck by the idea of adding a fourth dimension in order to multiply triples. Excited by this
breakthrough, as the couple passed the Broome Bridge of the Royal Canal, he carved the newfound
quaternion equations
i2 = j 2 = k2 = ijk = −1
into the stone of the bridge. This event is marked by a plaque at the exact location today. Hamilton
spent the rest of his life working on quaternions, which became the first non-commutative algebra
to be studied.
Up until now we have learned that a rotation in R3 about an axis through the origin can be
represented by a 3 × 3 orthogonal matrix with determinant 1. However, the matrix representation
seems redundant because only four of its nine elements are independent. Also the geometric inter-
pretation of such a matrix is not clear until we carry out several steps of calculation to extract the
rotation axis and angle. Furthermore, to compose two rotations, we need to compute the prod-
uct of the two corresponding matrices, which requires twenty-seven multiplications and eighteen
additions.
Quaternions are very efficient for analyzing situations where rotations in R3 are involved. A
quaternion is a 4-tuple, which is a more concise representation than a rotation matrix. Its geo-
metric meaning is also more obvious as the rotation axis and angle can be trivially recovered. The
quaternion algebra to be introduced will also allow us to easily compose rotations. This is because
quaternion composition takes merely sixteen multiplications and twelve additions.
2 Quaternion Algebra
The set of quaternions, together with the two operations of addition and multiplication, form a
non-commutative ring.1 The standard orthonormal basis for R3 is given by three unit vectors
∗
Sections 2–6 are based on Chapters 3–6 of the book [7] by J. B. Kuipers, and Sections 1 (partially) and 7 are
based on the essay by S. Oldenburger [6] who took the course.
1
For the purpose of this course, you don’t really need to know what a ring is although it can be found in a standard
algebra text such as the book by Hungerford [4] or Jacobson [5].
1
i = (1, 0, 0), j = (0, 1, 0), k = (0, 0, 1). A quaternion q is defined as the sum of a scalar q0 and a
vector q = (q1 , q2 , q3 ); namely,
q = q0 + q = q0 + q1 i + q2 j + q3 k.
i2 = j 2 = k2 = ijk = −1,
ij = k = −ji,
jk = i = −kj,
ki = j = −ik.
pq = (p0 + p1 i + p2 j + p3 k)(q0 + q1 i + q2 j + q3 k)
= p0 q0 − (p1 q1 + p2 q2 + p3 q3 ) + p0 (q1 i + q2 j + q3 k) + q0 (p1 i + p2 j + p3 k)
+(p2 q3 − p3 q2 )i + (p3 q1 − p1 q3 )j + (p1 q2 − p2 q1 )k.
Whew! It is too long to remember or even to understand what is going on. Fortunately, we can
utilize the inner product and cross product of two vectors in R3 to write the above quaternion
product in a more concise form:
pq = p0 q0 − p · q + p0 q + q0 p + p × q. (1)
In the above, p = (p1 , p2 , p3 ) and q = (q1 , q2 , q3 ) are the vector parts of p and q, respectively.
p = 3 + i − 2j + k,
q = 2 − i + 2j + 3k.
We single out their vector parts p = (1, −2, 1) and q = (−1, 2, 3) and calculate their inner and cross products:
p·q = −2,
i j k
p×q = 1 −2 1
−1 2 3
= −8i − 4j.
2
By (1) the quaternion product is then
We see that the product of two quaternions is still a quaternion with scalar part p0 q0 − p · q and
vector part p0 q + q0 p + p × q. The set of quaternions is closed under multiplication and addition.
It is not difficult to verify that multiplication of quaternions is distributive over addition. The
identity quaternion has real part 1 and vector part 0.
(q ∗ )∗ = q0 − (−q) = q,
q + q ∗ = 2q0 ,
q ∗ q = (q0 − q)(q0 + q)
= q0 q0 − (−q) · q + q0 q + (−q)q0 + (−q) × q by (1)
= q02 +q·q
= q02 + q12 + q22 + q32
= qq ∗ .
(pq)∗ = q ∗ p∗ . (2)
√
The norm of a quaternion q, denoted by |q|, is the scalar |q| = q ∗ q. A quaternion is called a
unit quaternion if its norm is 1. The norm of the product of two quaternions p and q is the product
of the individual norms, for we have
|pq|2 = (pq)(pq)∗
= pqq ∗ p∗
= p|q|2 p∗
= pp∗ |q|2
= |p|2 |q|2 .
We can easily verify that q −1 q = qq −1 = 1. In the case q is a unit quaternion, the inverse is its
conjugate q ∗ .
3
3 Quaternion Rotation Operator
How can a quaternion, which lives in R4 , operate on a vector, which lives in R3 ? First, we note
that a vector v ∈ R3 is a pure quaternion whose real part is zero. Let us consider a unit quaternion
q = q0 + q only. That q02 + kqk2 = 1 implies that there must exist some angle θ such that
cos2 θ = q02 ,
sin2 θ = kqk2 .
In fact, there exists a unique θ ∈ [0, π] such that cos θ = q0 and sin θ = kqk. The unit quaternion
can now be written in terms of the angle θ and the unit vector u = q/kqk:
q = cos θ + u sin θ.
Pure Quaternions
R3 R4
v = 0 +v
v
Quaternions
4
Theorem 1 For any unit quaternion
θ θ
q = q0 + q = cos + u sin , (4)
2 2
and for any vector v ∈ R3 the action of the operator
Lq (v) = qvq ∗
on v is equivalent to a rotation of the vector through an angle θ about u as the axis of rotation.
where in the last step above we introduced u = q/kqk. Denote n⊥ = u × n. So the last equation
becomes
Lq (n) = (q02 − kqk2 )n + 2q0 kqkn⊥ . (5)
Note that n⊥ and n have the same length:
π
kn⊥ k = kn × uk = knk · kuk sin = knk.
2
Finally, we rewrite (5) into the form
2 θ 2 θ θ θ
Lq (n) = cos − sin n + 2 cos sin n⊥
2 2 2 2
= cos θn + sin θn⊥ .
Namely, the resulting vector is a rotation of n through an angle θ in the plane defined by n and
n⊥ . See the figure below. This vector is clearly orthogonal to the rotation axis.
L q( n)
θ
q n
5
We substitute the unit quaternion form (4) into (3) to obtain the resulting vector from rotating
a vector v about the axis u through θ:
2 θ 2 θ θ θ θ θ
Lp (v) = cos − sin v + 2 u sin · v u sin + 2 cos u sin × v
2 2 2 2 2 2
= cos θ · v + (1 − cos θ)(u · v)u + sin θ · (u × v). (6)
Example 2. Consider a rotation about an axis defined by (1, 1, 1) through an angle of 2π/3. About this
axis, the basis vectors i, j, and k generate the same cone when rotated through 2π. We define a unit vector
1
u = √ (1, 1, 1).
3
Let the rotation angle θ = 2π/3. The quaternion q defines the rotation is then given as
θ θ
q = cos + u sin
2 2
1 1 1 1
= + i + j + k.
2 2 2 2
Let us compute the effect of rotation on the basis vector i = (1, 0, 0). We obtain the resulting vector
using (6):
√
1 1 1 1
1 1 1 1 3 1
v = − 0 + 1+ ·√ ·√ 1 + ·√ 1 × 0
2 2 3 3 2 3
0 1 1 0
1
1
−2 2 0
1 1
= 0 + 2 + 2
0 1
−1
2 2
= j.
The rotation of v under the operator Lq can also be interpreted from the perspective of an
observer attached to the vector v. What he sees happening is that the coordinate frame rotates
through the angle −θ about the same axis defined by the quaternion.
is a rotation of the coordinate frame about the axis u through an angle θ while v is not rotated.
Equivalently, the operator Lq∗ rotates the vector v with respect to the coordinate frame through
an angle −θ about q.
The quaternion operator Lq (v) = qvq ∗ may be interpreted as a point or vector rotation with
respect to the (fixed) coordinate frame. The quaternion operator Lq∗ (v) = q ∗ vq may be interpreted
as a coordinate frame rotation with respect to the (fixed) space of points.
6
4 Quaternion Operator Sequences
Let p and q be two unit quaternions. We first apply the operator Lp to the vector u and obtain
the vector v. To v we then apply the operator Lq and obtain the vector w. Equivalently, we apply
the composition Lq ◦ Lp of the two operators:
w = Lq (v)
= qvq ∗
= q(pup∗ )q ∗
= (qp)u(qp)∗
= Lqp (u).
Because p and q are unit quaternions, so is the product qp. Hence the above equation describes a
rotation operator whose defining quaternion is the product of the two quaternions p and q. The
axis and angle of the composite rotation is given by the product qp.
Similarly, consider the quaternion operators Lp∗ (u) = p∗ up and Lq∗ (v) = q ∗ vq which carry
out rotations of the coordinate system determined by quaternions p and q, respectively. Then the
quaternion product pq defines an operator L(pq)∗ , which represents a sequence of operators Lp∗
followed by Lq∗ . The axis and angle of rotation of L(pq)∗ are those represented by the quaternion
product pq.
Example 3. We now use the quaternion method to find the axis and angle of the composite rotation in
the Satellite tracking example from the notes titled “Space Rotations”. Recall that the tracking application
takes a rotation about the z-axis through a bearing angle α followed by a rotation about the new y-axis
through an elevation angle β. After these two rotations, the new x-axis points toward the satellite. The two
rotations are respectively described by the two quaternions below:
α α
p = cos + sin k,
2 2
β β
q = cos + sin j.
2 2
Since we are rotating the coordinate frame, the two operators Lp∗ and Lq∗ are applied sequentially. The
composite rotation operator is L(pq)∗ , which transforms coordinates in the station frame to those in the
tracking frame. And the quaternion describing the composition rotation is the product pq which is as
follows.
α α β β
pq = cos + sin k cos + sin j
2 2 2 2
α β α β α β α β
= cos cos + cos sin j + sin cos k + sin sin (k × j)
2 2 2 2 2 2 2 2
α β α β α β α β
= cos cos − sin sin i + cos sin j + sin cos k.
2 2 2 2 2 2 2 2
The axis of the composite rotation is defined by the vector
α β α β α β
v = − sin sin , cos sin , sin cos . (7)
2 2 2 2 2 2
And the angle of rotation θ satisfies
θ α β
cos = cos cos ,
2 2 2
θ
sin = kvk.
2
7
The cosine is same as obtained in Section 3 of the handouts titled “Rotation in the Space” for we have
θ
cos θ = 2 cos2 −1
2
α β
= 2 cos2 cos2 − 1
2 2
cos α + 1 cos β + 1
= 2 · −1
2 2
cos α cos β + cos α + cos β − 1
= .
2
Note that the rotation axis and angle in that section transforms coordinates in the tracking frame to those
in the station frame. This explains why the axis v in (7) is opposite to the one obtained in that section
while the angle is the same.
q1
q5
rotation
p1 q7 q3
p5 translation q4
p3
q2
p7 q6
p4 p2
p6
Model
Data
8
We begin by computing the centroids of the two sets of points:
n
1X
p̄ = pi ;
n
i=1
n
1 X
q̄ = qi.
n
i=1
The relative coordinates of all the points to their centroids are obtained as, for 1 ≤ i ≤ n,
p′i = pi − p̄;
q′i = q i − q̄.
Clearly, we have
n n n n
X X X 1X
p′i = pi − np̄ = pi − n · pi = 0; (9)
n
i=1 i=1 i=1 i=1
n n n n
X X X 1 X
q ′i = qi − nq̄ = qi − n · q i = 0. (10)
n
i=1 i=1 i=1 i=1
Let us rewrite the objective function in (8) in terms of p̄, q̄, p′i , q ′i :
n
X n
X
kRpi + b − q i k2 = kRp′i − q ′i + Rp̄ − q̄ + bk2
i=1 i=1
n
X
= (Rp′i − q ′i + Rp̄ − q̄ + b) · (Rp′i − q ′i + Rp̄ − q̄ + b)
i=1
n n
!
X X
= kRp′i − q ′i k2 + 2 (Rp′i − q ′i ) · (Rp̄ − q̄ + b) + nkRp̄ − q̄ + bk2
i=1 i=1
n n n
!
X X X
= kRp′i − q ′i k2 +2 R p′i − q ′i · (Rp̄ − q̄ + b) + nkRp̄ − q̄ + bk2
i=1 i=1 i=1
Xn
= kRp′i − q ′i k2 + nkRp̄ − q̄ + bk2 , by (9) and (10).
i=1
The minimizing translation b should make the second term in the last equation above zero, yielding:
b = q̄ − Rp̄. (11)
Thus we have decomposed the problem of data registration into two phases: the first of which
determines its optimal translation, as given by equation (11), and the second of which determines
the optimal rotation of the set {pi }. Note that every point pi is transformed into R(pi − p̄) + q̄
before matching against q i . Equivalently, to find the best match of the two point sets {pi } and
{q i }, we first translate {pi } to let their centroid coincide with that of {q i }, and then rotate about
the common centroid.
9
By the reasoning so far, the optimal rotation can be solved from the formulation below:
n
X
min kRp′i − q ′i k2 . (12)
R
i=1
Here we present an exact solution to (12) as described in [3] using quaternions. An equivalent
quaternion-based solution is given in [2]. The version of matching two curves (or surfaces), also
assuming pointwise correspondences, is solved exactly in [8] in a somewhat similar manner without
the use of quaternions.
First, we rewrite the summation in (12) as follows:
n
X n
X n
X n
X
kRp′i − q ′i k2 = ′ ′
(Rpi · Rpi ) − 2 ′ ′
(Rpi · q i ) + q ′i · q ′i
i=1 i=1 i=1 i=1
n
X n
X
′ 2 ′ 2
Rp′i · q ′i .
= kpi k + kq i k − 2
i=1 i=1
The first summand in the last equation above does not depend on the rotation, so we need only
minimize the second summand. Equivalently, this can be done through a maximization:
n
X
max Rp′i · q ′i . (13)
R
i=1
The rotation matrix R has nine entries, only four of which are independent due to the or-
thogonality and unit determinant of R. Instead, we represent rotations using unit quaternions.
Essentially, we find the unit quaternion q that maximizes
n
X
(qp′i q ∗ ) · q ′i . (14)
i=1
Here we view quaternions as vectors in R4 . Let q = (q0 , q1 , q2 , q3 )T and q ∗ = (q0 , −q1 , −q2 , −q3 )T .
Also, the points p′1 , . . . , p′n and q ′1 , . . . , q ′n are viewed as 4-tuples with p′i = (0, p′i1 , p′i2 , p′i3 )T and
′ , q ′ , q ′ )T by a slight abuse of notation.
q ′i = (0, qi1 i2 i3
Applying the definition of quaternion product, it is not difficult to show that
Next, we intend to rewrite the summands in (14) as matrix products. For this purpose, we define
matrices
0 −p′i1 −p′i2 −p′i3 ′ ′ ′
0 −qi1 −qi2 −qi3
p′ 0 p′i3 −p′i2 ′ ′ ′
and Qi = qi1 0 −qi3 qi2
Pi = i1 ,
p ′ ′ ′ ′ ′ ′
i2 −pi3 0 pi1 q qi3 0 −qi1
i2
p′i3 p′i2 −p′i1 0 ′
qi3 −qi2′ ′
qi1 0
for 1 ≤ i ≤ n. Then the quaternion products qp′i and q ′i q are equivalent to the matrix products
Pi q and Qi q. We thus have
n
X n
X
(qp′i q ∗ ) · q ′i = (qp′i ) · (q ′i q) from (15)
i=1 i=1
10
n
X
= (Pi q) · (Qi q)
i=1
Xn
= q T PiT Qi q
i=1
n
!
X
T
= q PiT Qi q.
i=1
q T M q = (α1 v 1 + α2 v 2 + α3 v 3 + α4 v 4 )T M (α1 v 1 + α2 v 2 + α3 v 3 + α4 v 4 )
= (α1 v 1 + α2 v 2 + α3 v 3 + α4 v 4 ) · (λ1 α1 v 1 + λ2 α2 v2 + λ3 α3 v 3 + λ4 α4 v4 )
= λ1 α21 + λ2 α22 + λ3 α23 + λ4 α24 .
11
the modern theory of relativity. In signal processing, Quaternion Fourier Transform (QFT) is a
powerful tool. The QFT restores the lost commutative property at the cost of no longer being a
division algebra. It can be used, for instance, to embed a watermark in a color image. Other ap-
plications of QFT include face recognition (jointly with Quaternion Wavelet Transform) and voice
recognition [6].
References
[1] P. J. Besl and N. D. McKay. A method for registration of 3-D shapes. IEEE Transactions on
pattern analysis and machine intelligence, 14(2):239–256, 1992.
[2] O. D. Faugeras and M. Hebert. The representation, recognition, and locating of 3-D objects.
International Journal of Robotics Research, 5(3):27–52, 1986.
[3] B. K. P. Horn. Closed-form solution of absolute orientation using unit quaternions. Journal of
Optical Society of America A, 4(4):629–642, 1987.
[6] S. Oldenburger. Applications of Quaternions. Written project of the course “Problem Solving
Techniques in Applied Computer Science” (Com S 477/577), Department of Computer Science,
Iowa State University, 2005.
[7] J. B. Kuipers. Quaternions and Rotation Sequences. Princeton University Press, 1999.
[8] J. T. Schwartz and M. Sharir. Identification of partially obscured objects in two and three
dimensions by matching noisy characteristic curves. International Journal of Robotics Research,
6(2):29–44, 1987.
12