1
Quaternion Computation Neil Dantam [email protected] Institute for Robotics and Intelligent Machines, Georgia Institute of Technology, Atlanta, GA, USA
Work in Progress – Updated 2014-10-01. IV-F Conjugate...... 6 IV-G Exponential...... 6 Abstract—Quaternions are a useful representation for orienta- tion, and dual quaternions extend the representation to handle IV-H Logarithm...... 7 translations as well. This report discusses computations that can IV-I Chaining Transforms...... 7 be performed using quaternions. To accurately compute results IV-J Transforming a point...... 7 near singularities, we provide Taylor series approximations which can be efficiently computed to within machine precision. IV-K Derivatives...... 7 IV-K1 Product Rule...... 7 IV-K2 Angular Velocity...... 7 CONTENTS IV-K3 Translational Velocity....7 I Introduction 1 IV-L Integration...... 7 I-A Notation...... 2 V Implicit Dual Quaternions 7 II Quaternions 2 V-A Chaining transforms...... 7 II-A Representation...... 2 V-B Transforming points...... 8 II-B Multiplication...... 2 V-C Conjugate...... 8 II-B1 Cross and dot product defi- V-D Derivatives...... 8 nition...... 2 II-B2 Matrix definition...... 2 VI Matrices and Euclidean Transforms 8 II-B3 Properties...... 3 VI-A Rotation Matrix...... 8 II-B4 Pure Multiplication.....3 VI-B Transformation Matrix...... 8 II-C Norm...... 3 VI-C Transforming Points...... 8 II-D Conjugate...... 3 II-E Inverse...... 3 VI-D Chaining Transforms...... 8 II-F Exponential...... 3 II-G Logarithm...... 3 References 8 II-H Power...... 4 II-I Pure Exponential Derivative...... 4 Appendix A: History 8 II-J Unit Logarithm Derivative...... 4 II-K Unit Quaternion Angle...... 4 Appendix B: Derivation of Quaternion Multiplication 9 II-L Product Rule...... 4 B-A Derivation of Quaternion Basis Equalities9 B-B Derivation of Quaternion Multiplication9 III Representing Orientation 4 III-A Rotating a vector...... 5 III-B Chaining rotations...... 5 I.INTRODUCTION III-C Angular Derivatives...... 5 III-D Axis-Angle...... 5 Quaternions are a convenient representation for spatial mo- III-E Spherical Linear Interpolation.....5 tion that provides some computational advantages over other III-F Integration...... 5 methods. III-G Finite Difference...... 6 The straightforward definitions of many quaternion quan- tities, particularly exponentials, logarithms, and derivatives, IV Dual Quaternions and Euclidean Transforms 6 contain singularities where a denominator goes to zero. We can IV-A Dual Numbers...... 6 avoid computational problems at these points by computing IV-B Representation...... 6 key factors near the singularity using a Taylor series, though IV-C Construction...... 6 this may require some careful rearrangement of terms to IV-D Multiplication...... 6 identify suitable factors and series. IV-E Matrix Form...... 6 A Taylor series evaluated near point a is: Quaternions
Associative p ⊗ (q ⊗ r) = (p ⊗ q) ⊗ r Distributive p ⊗ (q + r) = p ⊗ q + p ⊗ r ∞ (n) NOT Commutative p ⊗ q 6= q ⊗ p X f (a) ∗ ∗ ∗ f(x) = (x − a)n Conjugate Mul. (p ⊗ q) = q ⊗ p n! Conjugate Add. (p + q)∗ = q∗ + p∗ n=0 TABLE I f 0(a) f 00(a) ALGEBRAIC QUATERNION PROPERTIES = f(a) + (x − a) + (x − a)2 + 1! 2! f (3)(a) (x − a)3 + ... (1) 3! A. Representation To evaluate the infinite series to machine precision, we only We represent a quaternion as a 4-tuple of real numbers: need to compute up the term below floating point round-off. The resulting approximation is a polynomial which can q = w + xi + yj + zk be efficiently evaluated using Horner’s Rule, Algorithm1. = (x y z w) f(n)(a) The coefficients are the terms n! and the indeterminate = H(qv, w) (7) variable is x − a. Note than many Taylor series have zero coefficents for the odd or even terms. We can produce a more Historically, qv is called the vector part of the quaternion compact Horner polynomial by omitting the zero coefficients, and qw the scalar part. using (x − a)2 as the indeterminate variable, and perhaps It is convenient to define quaternion operations in terms of multiplying the whole result by (x − a). vector and matrix operations, so we also the whole quaternion as a column vector. This also provides an in-memory storage representation. Algorithm 1: Horner’s Rule T Input: b0, b1, . . . bn : Coefficients ~q = [x y z w] (8) Input: z : Indeterminate Variable T ~qv = [x y z] (9) Output: y : Result 1 y ← bn A alternate convention stores terms in wxyz order, so when 2 y ← bn−1 + zy using different software packages, it is sometimes necessary 3 y ← bn−2 + zy to convert between orderings. 4 ... 5 y ← b0 + zy B. Multiplication From the definition of the basis elements (2), we obtain a formula for quaternion multiplication. See section B for the detailed derivation. A. Notation 1) Cross and dot product definition: We define quaternion We adopt the following abbreviations to condense notation: multiplication in terms of cross products and dot products of its elements: • Quaternions are typeset as q. • Dual Quaternions are typeset as S. ~qv × ~pv + qw~pv + pw~qv • Vectors are typeset as ~x. q ⊗ p = (10) qwpw − ~qv · ~pv • Matrices are typeset as A. • Time derivatives of variable x are given as x˙. 2) Matrix definition: Expanding the above terms, we can • Sines and cosines are abbreviated as s and c. express quaternion multiplication as matrix multiplication: q ⊗ p = II.QUATERNIONS qw −qz qy qx q q −q q Quaternions are an extension of the complex numbers, using Q ~p = z w x y ~p = L −q q q q basis elements i, j, and k defined as: y x w z −qx −qy −qz qw 2 2 2 i = j = k = ijk = −1 (2) pw pz −py px −pz pw px py From (2), it follows: PR~q = ~q = py −px pw pz −px −py −pz pw jk = −k j = i (3) qxpw + qypz + qwpx − qzpy ki = −ik = j (4) qzpx + qwpy + qypw − qxpz (11) ij = −ji = k (5) qwpz + qzpw + qxpy − qypx −(qypy + qxpx + qzpz − qwpw) A quaternion, then, is: This matrix form is more suitable for efficient implementa- q = w + xi + yj + zk (6) tion computation using SIMD instructions.
2 Quaternions
3) Properties: Quaternion multiplication is associative and I distributive, but it is not commutative. 4) Pure Multiplication: When multiplying by a pure quater- nion, i.e., zero scalar part, we can simplify:
qw −qz qy q q −q |q | ⊗ (v, 0) = z w x v = v q −q q q y x w φ −qx −qy −qz q v q v q v R y z z y w x qw q v q v q v z x − x z + w y (12) q v q v q v x y y x w z Fig. 1. Imaginary Plane for Quaternions −qxvx qyvy −qzvz qw qz −qy −qz qw qx some useful trigonometric ratios for analyzing quaternion (v, 0) ⊗ q = v = qy −qx qw functions: −qx −qy −qz φ = atan2 (|qv| , qw) (19) vyqz vzqy vxqw |qv| vzqx vxqz vyqw sin (φ) = (20) − + (13) |q| vxqy vyqx vzqw qw −vxqx vyqy −vzqz cos (φ) = (21) |q| uyvz − uzvy uzvx − uxvz u × v The quaternion exponential is: (u, 0) ⊗ (v, 0) = = (14) uxvy − uyvx −u · v sin (|q |) q qw v −uxvx − uyvy − uzvz e = e H qv , cos (|qv|) (22) |qv| Thus, the case of multiplying two pure quaternions simpli- fies to the commonly used cross (×) and dot (·) products. When |qv| approaches zero, we can use the Taylor series approximation: C. Norm sin (θ) θ2 θ4 θ6 = 1 − + − + ... (23) p θ 6 120 5040 |q| = ~q · ~q (15) For a pure quaternion, the exponential simplifies to: A unit quaternion has norm of one. ( q sin(|qv |) e = H qv , cos (|qv|) |qv | D. Conjugate qw = 0 =⇒ (24) |eq | = 1 ∗ q = H(−qv, qw) (16) G. Logarithm E. Inverse To compute the logarithm, first consider the angle between the vector and scalar parts of the quaternion. q∗ q−1 = (17) ~q · ~q q |q | φ = cos−1 w = sin−1 v = atan2 (|q | , q ) (25) Note that for unit quaternions, the inverse is equal to the |q| |q| v w conjugate. The atan2 form to compute φ is generally best for numerical F. Exponential stability. The exponential shows the relationship between quaternions and complex numbers. Recall Euler’s formula for complex φ ln q = H qv, ln (|q|) (26) numbers: |qv|
φ iθ When |qv| approaches zero, we can compute as follows: e = cos (θ) + i sin (θ) (18) |qv | which relates the exponential function with angles in the φ φ φ φ complex plane. Similarly for quaternions, we can consider the = |q| = |q| = sin(φ) |q | (27) |qv| v sin (φ) |q| angle between the real and imaginary parts, Figure 1, yielding |q|
3 Representing Orientation
φ Representation Storage Then, sin(φ) can be approximated by Taylor series: Quaternion 4 Axis-Angle 4 θ θ2 7θ4 31θ6 Rotation Vector 3 = 1 + + + + ... (28) Euler Angles 3 sin (θ) 6 360 15120 Rotation Matrix 9 TABLE II For a unit quaternion, the logarithm simplifies to: STORAGE REQUIREMENTSFOR ORIENTATION REPRESENTATIONS φ q = 1 =⇒ ln(q) = H qv, 0 (29) Representation Chain Rotate Point sin (φ) Quaternion 16 multiply, 12 add 15 multiply, 15 add Rotation Matrix 27 multiply, 18 add 9 multiply, 6 add TABLE III H. Power COMPUTATIONAL REQUIREMENTSFOR ORIENTATION REPRESENTATIONS
t t ln q φ q = e (30) d sin(φ) where we compute dt qv as follows: d cos−1 (w) w˙ I. Pure Exponential Derivative φ˙ = = dt sin (φ) Becase quaternion multiplication is not commutative, the φ d sin(φ) φ˙ φφ˙ cos (φ) chain rule does not apply to the quaternion exponential deriva- = − dt sin (φ) sin2 (φ) tive: 1 φ cos (φ) =w ˙ − + (39) d exp (f(q)) df(q) sin2 (φ) sin3 (φ) 6= ⊗ exp (f(q)) (31) dt dt Equations (38) and (39) have a singularity at φ = 0. We The derivative of the exponential for a pure quaternion is: can handle (38) with (28) and (39) with the following Taylor series: 1 φ cos (φ) 1 2 2 √ − + = − − φ2 − φ4 ... (40) φ = |qv| = qv · qv (32) sin2 (φ) sin3 (φ) 3 15 63 d |qv| qv · q˙v φ˙ = = (33) Alternatively, one could also use the Jacobian ∂ ln q = dt φ ∂q 2 q sin (φ) φ − φx − φxy − φxz − x e = H qv, cos (φ) (34) sin(φ) sin3(φ) sin3(φ) sin3(φ) sin2(φ) 2 φ − φxy φ − φy − φyz − y sin3(φ) sin(φ) sin3(φ) sin3(φ) sin2(φ) q 2 de sin (φ) φxz φyz φ φz z ˙ − 3 − 3 − 3 − 2 = − sin (φ) φ = − (qv · q˙v) (35) sin (φ) sin (φ) sin(φ) sin (φ) sin (φ) (41) dt w φ ! deq s φ˙c φ˙s = q˙v + − 2 qv = dt v φ φ φ K. Unit Quaternion Angle s c s q˙ + − (q · q˙ ) q = φ v φ2 φ3 v v v We can compute the angle between the vector forms of two s unit quaternions as follows: s c − φ q˙ + (q · q˙ ) q (36) φ v φ2 v v v −1 ∠(~q1, ~q2) = cos (~q1 · ~q2) = Then, we handle the singularity for φ = 0 using (23) and 2 atan2 (|q − q | , |q + q |) (42) the following: 1 2 1 2 The atan2 form is more accurate [1]. c s 1 φ2 φ4 φ6 2 − 3 = − + − + + ... (37) φ φ 3 30 840 45360 L. Product Rule Because quaternion multiplication is a linear operation (see J. Unit Logarithm Derivative section B), the product rule applies:
The derivative of the unit quaternion logarithm is: d q1 ⊗ q2 = q˙1 ⊗ q2 + q1 ⊗ q˙2 (43) φ dt ln q = q sin (φ) v III.REPRESENTING ORIENTATION d φ d ln q sin(φ) φ = 1 = qv + q˙v (38) A unit quaternion ( q ) can represent an angular dt dt sin (φ) orientation.
4 Representing Orientation
A. Rotating a vector The rotation vector and quaternion derivatives are related as y = v y˙ = v˙ φ = |y| We can rotate point v by unit quaternion q by computing follows, substituting 2 , 2 , and : 0 ∗ v = rot q, v = q ⊗ v ⊗ q . Note that v is augmented with 0 in it’s w position to perform the quaternion multiplication op- y · y˙ φ˙ = (54) eration. Given this 0 value, the computation can be simplified φ to the following: sin (φ) q˙ = −φ˙ sin (φ) = (y · y˙) (55) w φ 0 v = rot q, v = sin (φ) φ˙ sin (φ) φ˙ cos (φ) ∗ q˙v = y˙ − 2 y + y = q ⊗ v ⊗ q = 2~qv × (~qv × v + qwv) + v (44) φ φ φ sin(φ) ! sin (φ) cos (φ) − which we can rewrite in a more SIMD-friendly form as: y˙ + φ (y ˙ · y) y (56) φ φ2
a = qv × v + qwv sin(φ) When φ goes to zero, we can approximate φ with the b = qv × a series in (23) and the other singular factor as: v0 = b + b + v (45) sin(φ) 2 4 6 cos (φ) − φ 1 φ φ φ 2 = − + − + + ... (57) B. Chaining rotations φ 3 30 840 45360
Rotations q1 and q2 are chained by multiplying the two E. Spherical Linear Interpolation quaternions: q1 ⊗ q2. Spherical Linear Interpolation, SLERP, interpolates between C. Angular Derivatives two quaternions. SLERP can be understood geometrically Rotational velocity ω is related to the quaternion derivative by considering a relative orientation in the axis-angle form. as follows: Consider the relative quaternion qr between two endpoints, q1 ⊗ qr = q2, given in axis angle form (ˆur, θr). To interpolate between q and q , we apply the q(τ) = q ⊗ q (τ), where q 1 1 2 1 s s q˙ = ω ⊗ q (46) is a rotation about uˆr with angle θs varying from 0 to θr as τ 2 varies from 0 to 1. We can compute the rotation vector form ω = 2q˙ ⊗ q∗ (47) of qs from that of qr as vs = τvr. Rotational acceleration ω˙ is related to the quaternion deriva- Composing definitions for quaternion and rotation vector tive as follows: conversion and quaternion exponents:
1 t ¨q = ω˙ ⊗ q + ω ⊗ q˙ (48) (τ) = ⊗ exp τ ln ∗ ⊗ = ⊗ ∗ ⊗ (58) 2 q q1 q1 q2 q1 q1 q2 1 ∗ ∗ π 3π = ω˙ = 2 ¨q ⊗ q + q˙ ⊗ q˙ (49) To interpolate in the shorter direction, e.g., − 2 vs. + 2 , 2 ∗ scale q1 ⊗ q2 so it has a positive scalar element. A more efficient computation for SLERP [2] is: D. Axis-Angle The axis-angle form, a = (ˆu, θ) represents rotation by angle θ around unit axis uˆ. We can also normalize the representation φ = |∠(~q1, ~q2)| (59) ( by scaling the axis by the angle v = θuˆ, which is sometimes φ > π π − φ called the rotation vector form. θ = 2 (60) φ ≤ π φ Rotation vectors are related to unit quaternions through the 2 ( π sin(θ−τθ) sin(τθ) exponential and logarithm. φ > q1 − q2 q(τ) = 2 sin(θ) sin(θ) (61) φ ≤ π sin(θ−τθ) q + sin(τθ) q 2 sin(θ) 1 sin(θ) 2 θ θ θ uˆ q = H uˆ sin , cos = e 2 = 2 2 F. Integration v |v| |v| v H sin , cos = e 2 (50) |v| 2 2 Euler or Runge-Kutta integration of quaternion derivatives would not preserve the unit constraint, introducing error. We can instead integrate a constant rotational velocity with: −1 −1 θ = 2 cos (qw) = 2 tan (|qv| , qw) = 2 |ln q| (51) (θ 6= 0 qv sin θ ln q ω∆t uˆ = ( 2 ) = (52) q1 = exp ⊗ q0 (62) θ = 0 0 |ln q| 2 ∗ v = 2 ln q (53) = exp ∆tq˙ ⊗ q0 ⊗ q0 (63)
5 Dual Quaternions and Euclidean Transforms
G. Finite Difference C. Construction Based on (62), we can compute a finite difference velocity We can produce a dual quaternion for some transformation represented by the rotational quaternion q, and the translation ω∆ between two orientations: vector v as follows: