Quaternion Quick Facts

This is not meant to be a treatise on quaternions, but a summary relevant for the code implementation. It also assumes familiarity with linear algebra.

See also the Matrix and Quaternions FAQ for a practical compendium of equations for implementing matrix and quaternion operations.

Quaternions

The space of quaternions is a vector space generated by the basis 1,i,j,k. In this space, quaternions are the sum of a scalar part generated by 1 and a vector component generated by i,j,k.

A multiplication operation is defined over the space such that:

  • it is distributive with respect to the usual sum

  • the identity is the element 1

  • the products of the basis elements is given by i,j,k:

(1)i2=j2=k2=1ij=ji=kjk=kj=iki=ik=j

Example: The product of two pure vectors v=v=v0i+v1j+v2k and u=u0i+u1j+u2k is:

vu=(v0u0+v1u1+v2u2)1+(v1u2v2u1)i+(v2u0v0u2)j+(v0u1v1u0)k=vu+v×u

Rotations

Most often, 3-d rotations are represented as 3×3 matrices

(2)M=[M00M01M02M10M11M12M20M21M22]

Some properties of rotations:

  • they are orthogonal: M1=MT or MTM=MMT=1

  • det(M) = 1

In what follows we use the following elemental (right-handed) rotations of α around the axes Rαz:

(3)Rαx=[1000cosαsinα0sinαcosα]Rαy=[cosα0sinα010sinα0cosα]Rαz=[cosαsinα0sinαcosα0001]

Angle-axis Representation

A 3-d rotation can be uniquely defined by a unit vector and an angle (u,ϕ).

  • there is one and only one real-valued eigenvalue: λ=1.

  • the rotation axis is given by a unit eigenvector of M with eigenvalue λ=1.

  • the rotation angle is given by the trace of M: Tr(M)=1+2cosϕ.

Equatorial Representation

Any rotation can be decomposed as a sequence of three rotations around different cartesian axes. Using the equatorial coordinates related to a fixed intertial reference frame, this can be expressed as RαzRδyRρx, where ρ, δ and α are roll, declination and right ascension respectively.

Quaternion Representation

A rotation can be represented by a quaternion operator:

(4)Lq(v)=qvq,

where q is a unit quaternion (i.e.: v2=1). The quaternion is related to the rotation axis and angle:

(5)q=cosϕ2+sinϕ2u

Switching Representations

Equatorial -> Matrix

Let’s write M=RαzRδyRρx in matrix form:

(6)M=[cosαsinα0sinαcosα0001][cosδ0sinδ010sinδ0cosδ][1000cosρsinρ0sinρcosρ]=[cosαcosδsinαcosρsinδsinρcosαsinαsinρsinδcosαcosρsinαcosδsinαsinδsinρ+cosαcosρsinαsinδcosρsinρcosαsinδsinρcosδcosδcosρ]

Matrix -> Equatorial

From Eq. (6) we can invert and get:

tanα=M10M00tanρ=M21M22sinδ=M20

Quaternion -> Matrix

Expanding Eq. (4) using the multiplication rules in Eq. (1), and then grouping factors of v, we can write this in matrix form:

(7)M=[12(q12+q22)2(q0q1q2q3)2(q0q2+q1q3)2(q0q1+q2q3)12(q02+q22)2(q1q2q0q3)2(q0q2q1q3)2(q1q2+q0q3)12(q02+q12)]

For practical reasons, some times we want to express the transform in terms of a quaternion with norm q1, only requiring that q0:

(8)M=1q2[q22(q12+q22)2(q0q1q2q3)2(q0q2+q1q3)2(q0q1+q2q3)q22(q02+q22)2(q1q2q0q3)2(q0q2q1q3)2(q1q2+q0q3)q22(q02+q12)]

Matrix -> Quaternion

This is a bit more involved. The idea is to invert from Eq. (8). From the diagonal elements we can already get the squares of the quaternion components:

(9)S=q24[1+M00M11M221M00+M11M221M00M11+M221+M00+M11+M22]=[q02q12q22q32],

The next step depends on which entry in Eq. (9) is the largest:

(10)0[S0,(M01+M10)4S0,(M02+M20)4S0,(M21M12)4S0]1[(M01+M10)4S1,S1,(M12+M21)4S1,(M02M20)4S1]2[(M20+M02)4S2,(M12+M21)4S2,S2,(M10M01)4S2]3[(M21M12)4S3,(M02M20)4S3,(M10M01)4S3,S3]

Note that the denominator is always 4Si, so we always choose the case with the largest denominator in order to minimize round-off errors.

Derivation. As an example, we derive one of the cases in Eq. (10). The rest are similar. The idea is to scale the quaternion so its largest component is equal to 1. In other words, if the i-th entry in Eq. (9) is the largest, then multiply q by a factor 1/Si to make qi=1. The magnitude of the resulting quaternion is q=1/qi.

If S1=1M00+M11M22 is the largest, we scale q by 1/S1. The matrix in Eq. (8) then takes a simpler form:

M=S1[1/S12(1+q22)2(q0q2q3)2(q0q2+q3)2(q0+q2q3)1/S12(q02+q22)2(q2q0q3)2(q0q2q3)2(q2+q0q3)1/S12(q02+1)]

and we can set:

q0=(M01+M10)4S1q1=1q2=(M12+M21)4S1q3=(M02M20)4S1q=1S1

and after normalizing:

q=[(M01+M10)4S1,S1,(M12+M21)4S1,(M02M20)4S1]

Equatorial -> Quaternion

This code first transforms from equatorial to matrix, and then from matrix to quaternion.

Quaternion -> Equatorial

Transform from quaternion to matrix, and then from matrix to equatorial.