Lecture 8 Quaternions

24
Lecture 8 Quaternions Overview, motivation Background Definition and properties Rotation using unit quaternions Intuition Using quaternions to represent rotations Why we love quaternions. Lecture 8 Quaternions Matthew T. Mason Mechanics of Manipulation

Transcript of Lecture 8 Quaternions

Page 1: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

Lecture 8Quaternions

Matthew T. Mason

Mechanics of Manipulation

Page 2: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

Today’s outline

Overview, motivation

Background

Definition and properties

Rotation using unit quaternions

Intuition

Using quaternions to represent rotations

Why we love quaternions.

Page 3: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

Motivation

Motivation

I Quaternions have nice geometrical interpretation.I Quaternions have advantages in representing

rotation.I Quaternions are cool. Even if you don’t want to use

them, you might need to defend yourself fromquaternion fanatics.

Page 4: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

Why can’t we invert vectors in R3?

I We can invert reals. x × 1x = 1.

I We can invert elements of R2 using complexnumbers. z × z∗/|z|2 = 1, where ∗ is complexconjugate.

I Can we invert v ∈ R3?I No.

I How about v ∈ R4?I Yes! Hamilton’s quaternions are to R4 what complex

numbers are to R2.

Page 5: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

Complex numbers versus quaternionsDefinition (Complex numbers)

I Define basis elements 1 and i ;I Define complex numbers as a vector space over

reals: elements have the form x + iy ;I One more axiom required: i2 = −1.

Definition (Quaternions)

I Define basis elements 1, i , j , k ;I Define quaternions as a vector space over reals:

elements have the form q0 + q1i + q2j + q3k ;I One more axiom:

i2 = j2 = k2 = ijk = −1

Page 6: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

Basis element multiplication

From that one axiom, we can derive other products:

ijk = −1i(ijk) = i(−1)−jk = −i

jk = i

Writing them all down:

ij = k , ji = −kjk = i , kj = −iki = j , ik = −j

Quaternion products of i , j , k behave like cross product.

Page 7: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

Quaternion notation

We can write a quaternion several ways:

q = q0 + q1i + q2j + q3kq = (q0,q1,q2,q3)

q = q0 + q

Definition (Scalar part; vector part)

For quaternion q0 + q, q0 is the scalar part and q is thevector part

Page 8: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

Quaternion product

We can write a quaternion product several ways:

pq = (p0 + p1i + p2j + p3k)(q0 + q1i + q2j + q3k)= (p0q0 − p1q1 − p2q2 − p3q3) + . . . i + . . . j + . . . k

pq = (p0 + p)(q0 + q)= (p0q0 + p0q + q0p + pq)

The last product includes many different kinds of product:product of two reals, scalar product of vectors. But whatis pq? Cross product? Dot product? Both! Cross productminus dot product!

pq = (p0q0 − p · q + p0q + q0p + p× q)

Page 9: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

Conjugate, lengthDefinition (Conjugate)

q∗ = q0 − q1i − q2j − q3k

Note that

qq∗ = (q0 + q)(q0 − q)

= q20 + q0q− q0q− qq

= q20 + q · q− q× q

= q20 + q2

1 + q22 + q2

3

Definition (Length)

|q| =√

qq∗ =√

q20 + q2

1 + q22 + q2

3

Page 10: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

Quaternion inverse

Every quaternion except 0 has an inverse:

q−1 =q∗

|q|2

Without commutativity, quaternions are a division ring, ora non-commutative field, or a skew field.Just as complex numbers are an extension of the reals,quaternions are an extension of the complex numbers(and of the reals).If 1D numbers are the reals, and 2D numbers are thecomplex numbers, then 4D numbers are quaternions, andthat’s all there is. (Frobenius) (Octonions are notassociative.)

Page 11: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

Rotation using unit quaternions

I Let q be a unit quaternion, i.e. |q| = 1.I It can be expressed as

q = cosθ

2+ sin

θ

2n̂

I Let x = 0 + x be a “pure vector”.I Let x ′ = qxq∗.I Then x ′ is the pure vector rot(θ, n̂)x!!!

Page 12: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

Proof that unit quaternions work

I Expand the product qxq∗;I Apply half angle formulas;I Simplify;I Compare with Rodrigues’s formula.

Sadly, not all proofs confer insight.

Page 13: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

Why θ/2? Why qxq∗ instead of qx?

In analogy with complex numbers, why not use

p = cos θ + n̂ sin θx′ = px

To explore that idea, define a map Lp(q) = pq. Note thatLp(q) can be written:

Lp(q) =

p0 −p1 −p2 −p3p1 p0 −p3 p2p2 p3 p0 −p1p3 −p2 p1 p0

q0q1q2q3

Note that the matrix above is orthonormal. Lp is a rotationof Euclidean 4 space!

Page 14: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

Geometrical explanation

Although Lp(q) rotates the 4D space of quaternions, it isnot a rotation of the 3D subspace of pure vectors. Someof the 3D subspace leaks into the fourth dimension.

Consider an example using p = i . Is it a rotation about iof π/2?

norotation

i

1

j

k

j j

1 1

i i

k k

1- i plane

j- k plane

Li q iq Ri q qi iqi Li Ri q

2 2

2 2

Page 15: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

What do we do with a representation?

Rotate a point: qxq∗.

Compose two rotations:

q(pxp∗)q∗ = (qp)x(qp)∗

Convert to other representations:I From axis-angle to quaternion:

q = cosθ

2+ sin

θ

2n̂

I From quaternion to axis-angle:

θ = 2 tan−1(|q|,q0)

n̂ = q/|q|

assuming θ is nonzero.

Page 16: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

From quaternion to rotation matrix

Just expand the product

qxq∗ = q20 + q2

1 − q22 − q2

3 2(q1q2 − q0q3) 2(q1q3 + q0q2)2(q1q2 + q0q3) q2

0 − q21 + q2

2 − q23 2(q2q3 − q0q1)

2(q1q3 − q0q2) 2(q2q3 + q0q1) q20 − q2

1 − q22 + q2

3

x

Page 17: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

From rotation matrix to quaternion

Given R = (rij), solve expression on previous slide forquaternion elements qi

Linear combinations of diagonal elements seem to solvethe problem:

q20 =

14(1 + r11 + r22 + r33)

q21 =

14(1 + r11 − r22 − r33)

q22 =

14(1− r11 + r22 − r33)

q23 =

14(1− r11 − r22 + r33)

so take four square roots and you’re done? You have tofigure the signs out. There is a better way . . .

Page 18: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

Look at the off-diagonal elements

I

q0q1 =14(r32 − r23)

q0q2 =14(r13 − r31)

q0q3 =14(r21 − r12)

q1q2 =14(r12 + r21)

q1q3 =14(r13 + r31)

q2q3 =14(r23 + r32)

I Given any one qi , could solve the above for the otherthree.

Page 19: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

The procedure

1. Use first four equations to find the largest q2i . Take its

square root, with either sign.2. Use the last six equations (well, three of them

anyway) to solve for the other qi .

I That way, only have to worry about getting one signright.

I Actually q and −q represent the same rotation, so noworries about signs.

I Taking the largest square root avoids division bysmall numbers.

Page 20: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

Properties of unit quaternions

I Unit quaternions live on the unit sphere in R4.I Quaternions q and −q represent the same rotation.I Inverse of rotation q is the conjugate q∗.I Null rotation, the identity, is the quaternion 1.

Page 21: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

Metric on SO(3)

What is the right metric between two spatial rotations?I.e. between two points in SO(3)?

I provided an answer in Lecture 2! SO(3) is aconfiguration space. Let R1 and R2 be two displacementsof the sphere S2. Let x by a point on the sphere. Let α bethe usual metric on the sphere—the length of the shortestgreat circle arc joining two points. Defined(R1,R2) = maxx∈S2 α(R1(x),R2(x)). That is equivalentto taking the rotation angle of R−1

1 R2. In brief, the angleof the rotation that takes you from R1 to R2.

Page 22: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

Metric on sphere of unit quaternionsI Consider unit quaternion

q = cosθ

2+ sin

θ

2n̂

Shortest path on the unit sphere joining ±q with 1has length θ/2.

I What is the shortest distance on the unit quaternionsphere S3 from ±p to ±q?

I Multiplication by a unit quaternion is a rotation of theunit quaternion sphere. I.e. distance-preserving. Sothe distance from ±p to ±q is the same as thedistance from ±pq∗ to 1. I.e. α/2, where α is therotation angle required from p to q.

I So usual metric on the sphere, applied to unitquaternions, is the right metric for SO(3). (Factor oftwo is irrelevant.)

Page 23: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

Topology of SO(3)

The right metric matters. Our representation is smoothand one-to-one, apart from the fact that antipodesrepresent the same rotation. That means the unitquaternions, with antipodes identified, have the sametopology as SO(3). That’s how we know the topology ofSO(3) is Projective 3-space, P3.

Page 24: Lecture 8 Quaternions

Lecture 8Quaternions

Overview,motivation

Background

Definition andproperties

Rotation using unitquaternions

Intuition

Using quaternionsto representrotations

Why we lovequaternions.

Measure, probabilities, samplingI Likewise we can turn our attention to measure.I Easiest way is to think in terms of probabilities.

Suppose you are given some probability densityfunction on SO(3). How can you decide whether it isunbiased?

I Pick some subset of SO(3), integrate the probabilityof that subset. Now rigidly rotate that subset. If theprobability is invariant with respect to rotation, thePDF is unbiased. A measure with this property is theHaar measure, and it is uniquely determined up tosome scale factor.

I Look at a uniform density function on the unitquaternion sphere S3. It has the right property — it isinvariant with respect to rotations. The quaternionrepresentation gives you the right measure, and is aneasy way to correctly sample SO(3).