|
A unit quaternion and a proper direction cosine matrix are two representations of the same three dimensional orientation. Quaternions are compact and convenient for composition, interpolation, estimation, and numerical propagation. Direction cosine matrices act directly on coordinate columns and make frame transformations visually explicit.
PhysicsLibrary uses passive coordinate transformations as the canonical attitude interpretation. If
maps coordinates from frame into frame , then
 |
(1) |
The corresponding direction cosine matrix is denoted
and satisfies
 |
(2) |
The quaternion to matrix map is defined so that
 |
(3) |
Under the PhysicsLibrary convention, quaternion and DCM frame chains use the same written order:
 |
(4) |
 |
(5) |
This article derives the matrix from the quaternion sandwich, develops the inverse matrix to quaternion conversion, explains the transpose and conjugate relationship, and gives numerical checks for implementation.
PhysicsLibrary uses Hamilton multiplication,
with reversed products changing sign.
A quaternion is displayed scalar first:
 |
(6) |
For a positive frame rotation through angle about unit axis
, the passive quaternion is
 |
(7) |
The reverse frame quaternion is
 |
(8) |
The corresponding DCMs satisfy
 |
(9) |
These equations define the signs used throughout the article.
Let
and
be right handed orthonormal bases.
The passive direction cosine matrix that maps coordinates into coordinates has entries
![$\displaystyle \left[ {}^{B}C_A \right]_{ij} = \widehat{\mathbf e}_i^B \cdot \widehat{\mathbf e}_j^A.$ $\displaystyle \left[ {}^{B}C_A \right]_{ij} = \widehat{\mathbf e}_i^B \cdot \widehat{\mathbf e}_j^A.$](https://images.physicslibrary.org/cache/objects/1101/l2h/img21.png) |
(10) |
Each entry is therefore the cosine of the angle between one basis vector and one basis vector.
The matrix maps coordinate columns according to
Because both bases are orthonormal,
 |
(11) |
and a proper frame orientation also satisfies
 |
(12) |
Thus
Write a unit quaternion as
where
For a pure vector quaternion
, the Hamilton sandwich can be written
 |
(13) |
Use the Vector Triple Product identity
Then
Since is unit,
Therefore
 |
(14) |
Equation (15) is linear in , so it can be represented by a matrix.
Define
![$\displaystyle [\mathbf q]_{\times} = \begin{bmatrix} 0&-q_z&q_y\ q_z&0&-q_x\ -q_y&q_x&0 \end{bmatrix},$ $\displaystyle [\mathbf q]_{\times} = \begin{bmatrix} 0&-q_z&q_y\ q_z&0&-q_x\ -q_y&q_x&0 \end{bmatrix},$](https://images.physicslibrary.org/cache/objects/1101/l2h/img39.png) |
(15) |
so that
![$\displaystyle [\mathbf q]_{\times}\mathbf v = \mathbf q\times\mathbf v.$ $\displaystyle [\mathbf q]_{\times}\mathbf v = \mathbf q\times\mathbf v.$](https://images.physicslibrary.org/cache/objects/1101/l2h/img40.png) |
(16) |
Also,
Therefore equation (15) becomes
![$\displaystyle \mathbf v' = \left[ \left( q_w^2-\mathbf q^T\mathbf q \right)I + 2\mathbf q\mathbf q^T + 2q_w[\mathbf q]_{\times} \right] \mathbf v.$ $\displaystyle \mathbf v' = \left[ \left( q_w^2-\mathbf q^T\mathbf q \right)I + 2\mathbf q\mathbf q^T + 2q_w[\mathbf q]_{\times} \right] \mathbf v.$](https://images.physicslibrary.org/cache/objects/1101/l2h/img42.png) |
(17) |
Hence the quaternion generated matrix is
![$\displaystyle C(q) = \left( q_w^2-\mathbf q^T\mathbf q \right)I + 2\mathbf q\mathbf q^T + 2q_w[\mathbf q]_{\times}.$ $\displaystyle C(q) = \left( q_w^2-\mathbf q^T\mathbf q \right)I + 2\mathbf q\mathbf q^T + 2q_w[\mathbf q]_{\times}.$](https://images.physicslibrary.org/cache/objects/1101/l2h/img43.png) |
(18) |
For the canonical passive frame quaternion,
this matrix is exactly .
Let
Expanding equation (19) gives
 |
(19) |
Using unit norm, the diagonal terms may also be written
 |
(20) |
Equations (20) and (21) are algebraically identical for unit quaternions.
For a positive frame rotation,
and
Substitute these into equation (19).
Using
and
we obtain
![$\displaystyle {}^{B}C_A = I\cos\theta + (1-\cos\theta) \widehat{\mathbf u}\widehat{\mathbf u}^T - [\widehat{\mathbf u}]_{\times}\sin\theta.$ $\displaystyle {}^{B}C_A = I\cos\theta + (1-\cos\theta) \widehat{\mathbf u}\widehat{\mathbf u}^T - [\widehat{\mathbf u}]_{\times}\sin\theta.$](https://images.physicslibrary.org/cache/objects/1101/l2h/img54.png) |
(21) |
This is the passive Rodrigues matrix.
The negative sine term is expected because the frame rotates positively while the coordinates of a fixed physical vector rotate in the inverse sense.
For
the passive quaternion is
 |
(22) |
Thus
Equation (21) gives
 |
(23) |
Apply it to the fixed vector with coordinates
Then
 |
(24) |
This agrees with the quaternion sandwich developed in the preceding PhysicsLibrary article.
Because
the columns of have an immediate geometric interpretation.
Let the standard basis columns in frame be
Applying the passive coordinate transformation gives
 |
(25) |
Thus:
- the first column is the
coordinate representation of the frame basis direction;
- the second column is the
coordinate representation of the frame basis direction;
- the third column is the
coordinate representation of the frame basis direction.
For example,
is simply the first column of .
The following illustration shows this column interpretation for a representative unit quaternion and its passive direction cosine matrix.
Figure. A unit quaternion and its passive direction cosine matrix. The original frame basis directions are shown together with their coordinate representations in frame . The three columns of are
,
, and
.
This interpretation is also a useful convention check. For the positive passive frame rotation about ,
Its first column is
which agrees with the passive coordinate result
The quaternion conjugate reverses the frame map:
The corresponding matrix relation is
 |
(26) |
Therefore
 |
(27) |
Since a proper DCM is orthogonal,
 |
(28) |
Thus quaternion conjugation, quaternion inversion, matrix transposition, and matrix inversion all describe the same reversal of a unit frame transformation.
Suppose
maps coordinates into coordinates, and
maps coordinates into coordinates.
The quaternion chain is
Apply the quaternion to matrix map:
Equation (19) is chosen so that Hamilton multiplication is homomorphic:
 |
(29) |
Therefore
Hence
 |
(30) |
The quaternion and DCM frame chains have identical written order.
A different source may use the same Hamilton quaternion coefficients but assign the quaternion to the opposite frame map or to an active vector rotation.
For the same positive geometric rotation, the active rotor is
Its active rotation matrix is
 |
(31) |
Therefore two sources may display matrix formulas that are transposes of one another while describing the same physical orientation from inverse viewpoints.
A quaternion to DCM formula should never be copied without its frame direction and sandwich convention.
The matrices generated by and are identical.
Every term in equations (20) and (21) is quadratic in the quaternion components. Replacing
by their negatives leaves every matrix entry unchanged.
Therefore
 |
(32) |
The map from unit quaternions to proper rotation matrices is two to one.
This is not a numerical defect. It is an intrinsic property of the quaternion representation of three dimensional orientation.
Let
be a proper DCM that obeys the PhysicsLibrary passive mapping convention.
From equation (20),
Therefore
 |
(33) |
When is safely away from zero, select one quaternion sign and compute
 |
(34) |
The off diagonal differences give
 |
(35) |
 |
(36) |
 |
(37) |
These signs are consistent with the PhysicsLibrary passive DCM formula in equation (20).
For a frame rotation near ,
is near zero.
The formulas in equations (36) through (38) then divide by a small number. The mathematics remains valid, but the numerical calculation becomes poorly conditioned.
A robust implementation instead chooses the quaternion component associated with the largest diagonal expression.
Let
If , define
 |
(38) |
Then
 |
(39) |
 |
(40) |
If is the largest diagonal term, define
 |
(41) |
Then
 |
(42) |
 |
(43) |
If is the largest diagonal term, define
 |
(44) |
Then
 |
(45) |
 |
(46) |
Otherwise use the branch:
 |
(47) |
Then
 |
(48) |
 |
(49) |
Each branch returns one member of the equivalent pair and .
After extraction, numerical software should normally normalize the quaternion to remove roundoff error.
Consider a positive frame rotation of about
The passive quaternion is
 |
(50) |
Substitution into equation (20) gives
 |
(51) |
The trace is
Therefore equation (35) gives
The vector components recover as
Thus the DCM to quaternion conversion returns the original passive quaternion.
Choosing the opposite overall quaternion sign would produce the same DCM.
For a quaternion generated matrix,
For unit ,
Using the homomorphism property,
Therefore
 |
(52) |
This gives a compact quaternion proof of DCM orthogonality.
An orthogonal matrix has determinant or .
At the identity quaternion,
we have
and therefore
Unit quaternions vary continuously, and the determinant of an orthogonal matrix cannot change continuously from to without passing through zero. An orthogonal matrix never has zero determinant.
Therefore every unit quaternion generates a proper rotation matrix:
 |
(53) |
A numerical matrix intended to represent orientation should satisfy
 |
(54) |
and
 |
(55) |
Measured or numerically propagated matrices may drift slightly away from these conditions.
Converting a badly nonorthogonal matrix directly to a quaternion can produce misleading results. If the deviation is more than roundoff level, the matrix should first be examined or projected back onto using a suitable orthogonalization method.
The quaternion extraction formulas assume that the input is already a proper rotation matrix.
For software validation, perform both conversion directions.
Starting with a normalized quaternion :
- compute
- recover
- compare orientation rather than raw component sign.
Because and represent the same orientation, a useful scalar check is
 |
(56) |
Alternatively compare
with
Starting with a DCM , perform the reverse round trip
and verify that the matrices agree to numerical tolerance.
The positive frame rotation about provides a compact convention test.
PhysicsLibrary expects
 |
(57) |
and
 |
(58) |
Therefore
 |
(59) |
If a library instead produces
it is producing the inverse matrix for this declared passive frame transformation, or equivalently the positive active rotation matrix.
This one case detects many transpose and sign mistakes immediately.
- Copying a quaternion to DCM formula without its frame direction.
Two valid formulas may be transposes of one another because they represent inverse maps.
- Using an active quaternion with the passive matrix formula without conjugating it.
For the same positive physical geometry, the active rotor is the conjugate of the canonical passive frame quaternion.
- Changing Hamilton multiplication to make matrix composition work.
PhysicsLibrary keeps Hamilton multiplication and uses the quaternion to DCM assignment in equation (19), for which
- Assuming
and should produce different matrices.
They must produce exactly the same DCM.
- Using the trace extraction formula near
.
When is small, use the largest component branch instead.
- Forgetting to normalize an extracted quaternion.
Small floating point errors can move the result slightly away from unit norm.
- Extracting a quaternion from a matrix that is not close to
.
The conversion formulas assume an orthogonal matrix with determinant .
- Comparing round trip quaternion components without accounting for sign.
A recovered quaternion may equal and still represent the identical orientation.
- Inferring semantic convention from scalar first or scalar last storage.
Array layout does not determine frame direction, active or passive meaning, or the multiplication law.
The preceding article, composition of rotations and quaternion order, establishes the passive frame chain
The present article derives the matrix representation whose frame chain uses the same written order.
A separate companion entry, Quaternions and Direction Cosine Matrices: Examples, Exercises, and Solutions, provides the Q09E self study problem bank.
The next main article, quaternions and Euler angles, uses the quaternion and DCM formulas developed here to derive the intrinsic - - yaw, pitch, roll conversion under the same passive convention.
The quaternion to matrix relation follows directly from the Hamilton sandwich product. The principal engineering difficulty is convention management: different sources may use inverse frame maps, active vector rotations, transposed DCMs, scalar last storage, or flipped quaternion multiplication.
Sommer and coauthors provide a modern discussion of the Hamilton and flipped multiplication conventions and explain how quaternion to matrix assignments interact with passive frame mappings. Moore provides an openly licensed engineering treatment of reference frames and direction cosine matrices. Henderson's Shuttle era memorandum is a useful historical engineering reference for explicit relationships among Euler Angles, quaternions, and transformation matrices.
- 1
- H. Sommer, I. Gilitschenski, M. Bloesch, S. Weiss, R. Siegwart, and J. Nieto, “Why and How to Avoid the Flipped Quaternion Multiplication,” Aerospace, vol. 5, no. 3, article 72, 2018. Published under CC BY 4.0. Publisher article https://www.mdpi.com/2226-4310/5/3/72
- 2
- J. K. Moore, Learn Multibody Dynamics, chapter “Orientation of Reference Frames.” Distributed under CC BY 4.0. Learn Multibody Dynamics https://moorepants.github.io/learn-multibody-dynamics/orientation.html
- 3
- D. M. Henderson, Euler Angles, Quaternions, and Transformation Matrices: Working Relationships, JSC-12960, NASA Johnson Space Center, Mission Planning and Analysis Division, 1977. Engineering reference. NASA Technical Reports Server search https://ntrs.nasa.gov/search?q=JSC-12960
- 4
- W. R. Hamilton, Elements of Quaternions, 2nd ed., edited by C. J. Joly, Longmans, Green, and Co., 1899. Public domain historical source. Internet Archive scan https://archive.org/details/elementsofquater01hamiuoft
Unless otherwise noted, this PhysicsLibrary entry is intended for release under the Creative Commons Attribution ShareAlike 4.0 International license.
|