Physics Library
 An open source physics library
Encyclopedia | Forums | Docs | Random  

quaternions and direction cosine matrices

(Topic)

Quaternions and Direction Cosine Matrices

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

B
 qA

maps coordinates from frame A into frame B, then

B    B    A  B    ∗
 v =   qA  v(  qA) .
(1)

The corresponding direction cosine matrix is denoted

B
  CA,

and satisfies

B     B   A
 v =   CA  v.
(2)

The quaternion to matrix map is defined so that

BCA  = C (BqA ).
(3)

Under the PhysicsLibrary convention, quaternion and DCM frame chains use the same written order:

C      C   B
 qA =   qB  qA,
(4)

C CA = C CB BCA.
(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.

1 Convention declaration

PhysicsLibrary uses Hamilton multiplication,

ij = k,    jk =  i,     ki = j,

with reversed products changing sign.

A quaternion is displayed scalar first:

                                 ⌊ qw⌋
                                 |   |
q = qw + qxi + qyj + qzk  ← →    | qx| .
                                 ⌈ qy⌉
                                   qz
(6)

For a positive frame rotation through angle 𝜃 about unit axis u, the passive quaternion is

B         𝜃-       𝜃-
 qA =  cos2 − ^u sin2 .
(7)

The reverse frame quaternion is

Aq  = (Bq  )∗.
  B       A
(8)

The corresponding DCMs satisfy

A       B    T
 CB  = (  CA)  .
(9)

These equations define the signs used throughout the article.

2 Definition of a direction cosine matrix

Let

{^eA,^eA ,^eA}
  1   2  3

and

{  B   B  B}
  ^e1 ,^e 2 ,^e3

be right handed orthonormal bases.

The passive direction cosine matrix that maps A coordinates into B coordinates has entries

[BC  ]  = ^eB ⋅^eA .
    A ij    i    j
(10)

Each entry is therefore the cosine of the angle between one B basis vector and one A basis vector.

The matrix maps coordinate columns according to

B     B   A
 v =   CA  v.

Because both bases are orthonormal,

(BCA  )T BCA  = I,
(11)

and a proper frame orientation also satisfies

det(BCA ) = +1.
(12)

Thus

BCA  ∈ SO (3).

3 Quaternion sandwich in vector form

Write a unit quaternion as

q = q  + q,
     w

where

     ⌊q ⌋
     ⌈ x⌉
q =   qy  .
      qz

For a pure vector quaternion v = (0,v), the Hamilton sandwich can be written

v′ = v + 2qw(q × v ) + 2q × (q ×  v).
(13)

Use the Vector Triple Product identity

q × (q × v ) = q(q ⋅ v) − (q ⋅ q)v.

Then

v ′ = v + 2q (q × v)
            w
      + 2q (q ⋅ v ) − 2(q ⋅ q)v.

Since q is unit,

q2w + q ⋅ q = 1.

Therefore

v′ = (q2 − q ⋅ q) v + 2q(q ⋅ v ) + 2q (q × v).
       w                          w
(14)

Equation (15) is linear in v, so it can be represented by a 3 × 3 matrix.

4 Cross product matrix

Define

       ⌊               ⌋
         0    − qz   qy
[q ]× =  ⌈ qz    0   − qx⌉ ,
        − qy   qx    0
(15)

so that

[q]×v =  q × v.
(16)

Also,

              T
q (q ⋅ v) = qq v.

Therefore equation (15) becomes

     [(         )                   ]
v′ =   q2−  qTq  I + 2qqT  + 2qw[q]×  v.
        w
(17)

Hence the quaternion generated matrix is

C (q) = (q2 − qT q)I + 2qqT  + 2q [q] .
          w                      w   ×
(18)

For the canonical passive frame quaternion,

q = Bq  ,
       A

this matrix is exactly BC A.

5 Explicit quaternion to DCM formula

Let

q = q  + q i + q j + q k.
     w    x    y     z

Expanding equation (19) gives

        ⌊q2 + q2 − q2−  q2   2(qxqy − qwqz)     2(qxqz + qwqy) ⌋
        ⌈ w    x    y    z   2    2    2   2                   ⌉
C (q) =    2(qxqy + qwqz)   qw − qx + qy − qz  22(qyqz2 − qw2qx) 2  .
           2(qxqz − qwqy)     2(qyqz + qwqx)   qw − qx − qy + qz
(19)

Using unit norm, the diagonal terms may also be written

        ⌊                                             ⌋
         1 − 2(q2y + q2z)  2(qxqy − qwqz)  2(qxqz + qwqy )
C (q) = ⌈2(qxqy + qwqz)  1 − 2(q2x + q2z)  2(qyqz − qwqx )⌉ .
         2(qxqz − qwqy)  2(qyqz + qwqx)  1 − 2(q2+  q2)
                                                x   y
(20)

Equations (20) and (21) are algebraically identical for unit quaternions.

6 Passive axis angle matrix

For a positive frame rotation,

         𝜃-
qw =  cos2

and

           𝜃-
q = − ^u sin 2 .

Substitute these into equation (19).

Using

q2 − qT q = cos𝜃,
 w

2qqT  = (1 − cos𝜃)^u ^uT,

and

2qw[q]× = − sin 𝜃[^u ]×,

we obtain

BCA  =  I cos 𝜃 + (1 − cos 𝜃)^u^uT − [^u ]× sin 𝜃.
(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.

7 Check: positive 90∘ frame rotation about +z

For

𝜃 = 90∘,     ^u = ^z,

the passive quaternion is

         --     --
       √ 2    √ 2
BqA  = ----−  ---k.
        2      2
(22)

Thus

     √2--                             √2-
qw = ----,    qx = qy = 0,     qz = − ---.
      2                               2

Equation (21) gives

        ⌊ 0   1  0⌋
B       ⌈         ⌉
  CA =   − 1  0  0  .
          0   0  1
(23)

Apply it to the fixed vector with A coordinates

      ⌊  ⌋
       1
Av  = ⌈0 ⌉.

       0

Then

                ⌊   ⌋
B     B   A       0
  v =  CA   v = ⌈ − 1⌉ .
                  0
(24)

This agrees with the quaternion sandwich developed in the preceding PhysicsLibrary article.

8 What the columns mean

Because

Bv =  BCA Av,

the columns of BC A have an immediate geometric interpretation.

Let the standard basis columns in frame A be

       ⌊ ⌋             ⌊  ⌋             ⌊ ⌋
        1                0               0
Ae  =  ⌈0⌉ ,     Ae  = ⌈ 1⌉ ,    Ae  =  ⌈0⌉ .
   1               2                3
        0                0               1

Applying the passive coordinate transformation gives

        [              ]
BCA  =   Be1  Be2  Be3  .
(25)

Thus:

  • the first column is the B coordinate representation of the A frame x basis direction;
  • the second column is the B coordinate representation of the A frame y basis direction;
  • the third column is the B coordinate representation of the A frame z basis direction.

For example,

           ⌊  ⌋
B     B      1
 e1 =   CA ⌈ 0⌉
             0

is simply the first column of BC A.

The following illustration shows this column interpretation for a representative unit quaternion and its passive direction cosine matrix.

PIC

Figure. A unit quaternion and its passive direction cosine matrix. The original A frame basis directions are shown together with their coordinate representations in frame B. The three columns of BC A are Be 1, Be 2, and Be 3.

This interpretation is also a useful convention check. For the positive 90∘ passive frame rotation about +z,

        ⌊         ⌋
          0   1  0
BC   =  ⌈− 1  0  0⌉ .
   A
          0   0  1

Its first column is

⌊    ⌋
   0
⌈ − 1 ⌉,
   0

which agrees with the passive coordinate result

A       B
  ^x −→   (− ^y).

9 Conjugate and transpose

The quaternion conjugate reverses the frame map:

A      B    ∗
 qB = (  qA) .

The corresponding matrix relation is

C (q∗) = C(q)T.
(26)

Therefore

ACB  =  C (AqB ) = C ((BqA )∗) = (BCA )T.
(27)

Since a proper DCM is orthogonal,

(BCA  )− 1 = (BCA  )T = ACB.
(28)

Thus quaternion conjugation, quaternion inversion, matrix transposition, and matrix inversion all describe the same reversal of a unit frame transformation.

10 Quaternion and DCM composition

Suppose

BqA

maps A coordinates into B coordinates, and

Cq
  B

maps B coordinates into C coordinates.

The quaternion chain is

C      C   B
 qA =   qB  qA.

Apply the quaternion to matrix map:

C(C qA) = C(C qB BqA ).

Equation (19) is chosen so that Hamilton multiplication is homomorphic:

C (pq) = C (p)C(q).
(29)

Therefore

C         C
 CA  = C ( qA)
     = C (Cq )C (Bq  )
            B      A
     = CCBBCA.

Hence

C      C    B
  CA =   CB   CA.
(30)

The quaternion and DCM frame chains have identical written order.

11 Why some sources show the transpose formula

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

         B    ∗
qactive = (  qA) .

Its active rotation matrix is

Ractive = C((BqA )∗) = (BCA  )T.
(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.

12 The q  versus − q  ambiguity

The matrices generated by q and −q are identical.

Every term in equations (20) and (21) is quadratic in the quaternion components. Replacing

qw,qx,qy,qz

by their negatives leaves every matrix entry unchanged.

Therefore

C (− q) = C(q).
(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.

13 Recovering a quaternion from a DCM

Let

     ⌊C11  C12   C13⌋
     ⌈              ⌉
C =   C21  C22   C23
      C31  C32   C33

be a proper DCM that obeys the PhysicsLibrary passive mapping convention.

From equation (20),

tr(C ) = 4q2w − 1.

Therefore

 2   1-+-C11-+-C22-+-C33-
qw =          4          .
(33)

When qw is safely away from zero, select one quaternion sign and compute

q  =  1∘1--+-C---+-C---+-C---.
 w    2        11     22    33
(34)

The off diagonal differences give

     C32 − C23
qx = ---4q-----,
          w
(35)

q =  C13-−-C31-,
 y      4qw
(36)

     C21-−-C12-
qz =    4q     .
          w
(37)

These signs are consistent with the PhysicsLibrary passive DCM formula in equation (20).

14 Why the simple trace formula can become ill conditioned

For a frame rotation near 180∘,

q  =  cos 𝜃
 w       2

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.

15 Robust DCM to quaternion algorithm

Let

t = C11 + C22 + C33.

If t > 0, define

      √ -----
S =  2  1 + t.
(38)

Then

     S-
qw = 4 ,
(39)

q =  C32-−-C23,     q  = C13-−-C31-,    q  = C21-−-C12-.
 x       S           y       S           z       S
(40)

If C11 is the largest diagonal term, define

     ∘  --------------------
S = 2   1 + C11 − C22 − C33.
(41)

Then

      C32-−-C23-          S-
qw =      S     ,    qx =  4,
(42)

     C12 + C21           C13 + C31
qy = ----------,    qz = ----------.
         S                   S
(43)

If C22 is the largest diagonal term, define

     ∘  --------------------
S = 2   1 − C11 + C22 − C33.
(44)

Then

     C13 − C31           C12 +  C21
qw = ----------,    qx = ----------,
         S                   S
(45)

     S           C   + C
qy = --,    qz = --23----32.
     4               S
(46)

Otherwise use the qz branch:

     ∘  --------------------
S = 2   1 − C11 − C22 + C33.
(47)

Then

     C21-−-C12-          C13-+--C31
qw =     S     ,    qx =     S     ,
(48)

q =  C23-+-C32-,    q  = S-.
 y       S           z   4
(49)

Each branch returns one member of the equivalent pair q and −q.

After extraction, numerical software should normally normalize the quaternion to remove roundoff error.

16 Round trip example with a nontrivial axis

Consider a positive frame rotation of 60∘ about

      ⌊ 1⌋
    1-⌈  ⌉
^u = 3   2  .
        2

The passive quaternion is

       √ --
B      --3-  1-    1-   1-
  qA =  2  − 6 i − 3j − 3k.
(50)

Substitution into equation (20) gives

       ⌊    5     1   √3- 1   √3-⌋
       |    9√-   9 + 3   9 − √3-|
BCA  = ⌈ 19 − -33-    1138 -  29 + -63⌉ .
         1 + √3-  2−  √3-    13-
         9    3   9   6      18
(51)

The trace is

   B
tr( CA ) = 2.

Therefore equation (35) gives

       √ --  √ --
qw =  1- 3 = --3.
      2       2

The vector components recover as

qx = − 1-,    qy = − 1-,    qz = − 1.
       6             3             3

Thus the DCM to quaternion conversion returns the original passive quaternion.

Choosing the opposite overall quaternion sign would produce the same DCM.

17 Orthogonality check

For a quaternion generated matrix,

    ∗        T
C (q ) = C(q) .

For unit q,

q∗q = 1.

Using the homomorphism property,

C (q )T C(q) = C (q∗)C (q)
                 ∗
           = C (q q)
           = C (1)

           = I.

Therefore

C (q)TC (q ) = I.
(52)

This gives a compact quaternion proof of DCM orthogonality.

18 Determinant

An orthogonal matrix has determinant +1 or −1.

At the identity quaternion,

q = 1,

we have

C (1) = I

and therefore

det C (1 ) = +1.

Unit quaternions vary continuously, and the determinant of an orthogonal matrix cannot change continuously from +1 to −1 without passing through zero. An orthogonal matrix never has zero determinant.

Therefore every unit quaternion generates a proper rotation matrix:

det C (q ) = +1.
(53)

19 Checking whether a measured matrix is a valid DCM

A numerical matrix intended to represent orientation should satisfy

CT C  ≈ I
(54)

and

detC  ≈ +1.
(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 SO(3) using a suitable orthogonalization method.

The quaternion extraction formulas assume that the input is already a proper rotation matrix.

20 Numerical round trip checks

For software validation, perform both conversion directions.

Starting with a normalized quaternion q:

  1. compute
    C = C (q);
  2. recover
    ^q = C −q1uat(C );
  3. compare orientation rather than raw component sign.

Because q and −q represent the same orientation, a useful scalar check is

| T |
|q ^q| ≈ 1.
(56)

Alternatively compare

C (q)

with

C (^q).

Starting with a DCM C, perform the reverse round trip

C − →  q − → C(q)

and verify that the matrices agree to numerical tolerance.

21 Implementation sign diagnostic

The positive 90∘ frame rotation about +z provides a compact convention test.

PhysicsLibrary expects

       ⌊ √ --  ⌋
           2∕2
Bq   = ||   0   ||
   A   ⌈  √0-- ⌉
        −   2∕2
(57)

and

        ⌊         ⌋
          0   1  0
BCA  =  ⌈− 1  0  0⌉ .
          0   0  1
(58)

Therefore

⌊ 1⌋       ⌊ 0 ⌋
⌈  ⌉       ⌈   ⌉
  0   − →   − 1   .
  0 A        0   B
(59)

If a library instead produces

⌊ 0  − 1 0 ⌋
⌈          ⌉
  1   0  0  ,
  0   0  1

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.

22 Common pitfalls

  1. Copying a quaternion to DCM formula without its frame direction.

    Two valid formulas may be transposes of one another because they represent inverse maps.

  2. 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.

  3. Changing Hamilton multiplication to make matrix composition work.

    PhysicsLibrary keeps Hamilton multiplication and uses the quaternion to DCM assignment in equation (19), for which

    C (pq) = C (p)C(q).
  4. Assuming q  and − q  should produce different matrices.

    They must produce exactly the same DCM.

  5. Using the trace extraction formula near 180∘ .

    When qw is small, use the largest component branch instead.

  6. Forgetting to normalize an extracted quaternion.

    Small floating point errors can move the result slightly away from unit norm.

  7. Extracting a quaternion from a matrix that is not close to SO  (3 )  .

    The conversion formulas assume an orthogonal matrix with determinant +1.

  8. Comparing round trip quaternion components without accounting for sign.

    A recovered quaternion may equal −q and still represent the identical orientation.

  9. 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.

23 Relationship to adjacent PhysicsLibrary entries

The preceding article, composition of rotations and quaternion order, establishes the passive frame chain

C      C   B
 qA =   qB  qA.

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 3-2-1 yaw, pitch, roll conversion under the same passive convention.

24 Sources and convention notes

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.

References

[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

[2]   J. K. Moore, Learn Multibody Dynamics, chapter “Orientation of Reference Frames.” Distributed under CC BY 4.0. Learn Multibody Dynamics

[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

[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

License

Unless otherwise noted, this PhysicsLibrary entry is intended for release under the Creative Commons Attribution ShareAlike 4.0 International license.


"quaternions and direction cosine matrices" is owned by bloftin.
(view preamble)
View style:
See Also: quaternion series overview and article guide, Quaternions for Physics and Engineering: Orientation, Notation, and Conventions, quaternion definition and basic algebra, example of quaternion definition and basic algebra, quaternion product, example of quaternion product, quaternion conjugate, example of quaternion conjugate, quaternion norm, example of quaternion norm, quaternion inverse, example of quaternion inverse

Keywords:  quaternion, direction cosine matrix, DCM, rotation matrix, active rotation, passive rotation, frame transformation, attitude matrix

Attachments:
example of quaternions and direction cosine matrices (Example) by bloftin

Cross-references: Euler Angles, quaternion multiplication, quaternions and Euler angles, composition of rotations and quaternion order, work, detects, determinant, trace, formulas, conjugation, relation, quaternion conjugate, norm, identity, Vector Triple Product, vector, scalar, matrix, composition, representations, direction cosine matrix, quaternion, unit
There are 4 references to this object.

This is version 3 of quaternions and direction cosine matrices, born on 2026-08-24, modified 2026-08-27.
Object id is 1101, canonical name is QuaternionsAndDirectionCosineMatrices.
Accessed 296 times total.

Classification:
Physics Classification: 02.40.Yy (Geometric mechanics )
 02.10.Hh (Rings and algebras)
 45.40.-f (Dynamics and kinematics of rigid bodies)

Pending Errata and Addenda

None.

Discussion

No messages.

Interact