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

[parent] example of quaternions and direction cosine matrices

(Example)

Quaternions and Direction Cosine Matrices: Examples, Exercises, and Solutions

This entry is the self study companion to the PhysicsLibrary article quaternions and direction cosine matrices. All exercises are stated first. Complete worked solutions follow afterward.

The canonical PhysicsLibrary attitude representation is passive. A unit quaternion Bq A maps coordinates from frame A into frame B:

Bv = Bq   Av(Bq  )∗.
        A       A
(1)

The matching direction cosine matrix satisfies

Bv =  BCA Av,
(2)

with

BCA  = C (BqA ).
(3)

For a positive frame rotation through angle 𝜃 about unit axis u,

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

1 Formula summary

For a unit quaternion

q = qw + qxi + qyj + qzk,

the PhysicsLibrary quaternion to DCM formula is

        ⌊                                             ⌋
         1 − 2(q2 + q2)  2(qxqy − qwqz)  2(qxqz + qwqy )
C (q) = ⌈2(q q  y+ q qz)  1 − 2(q2 + q2)  2(q q − q  q )⌉ .
            x y    w z          x    z      y z 2 w  x2
         2(qxqz − qwqy)  2(qyqz + qwqx)  1 − 2(qx + qy)
(5)

For a proper DCM

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

if qw is safely away from zero,

       ∘ --------------------
qw =  1- 1 + C11 + C22 + C33,
      2
(6)

q =  C32-−-C23,     q  = C13-−-C31-,    q  = C21-−-C12-.
 x      4qw          y      4qw          z      4qw
(7)

The sign pair q and −q represents the same DCM.

2 Exercises

  1. Passive 90∘ frame rotation about +z  .

    Frame B is obtained from frame A by a positive 90∘ frame rotation about +z.

    1. Construct Bq A.
    2. Convert it to BC A.
    3. Apply the DCM to
            ⌊  ⌋
       1
Av  = ⌈0 ⌉.

       0
  2. Passive 90∘ frame rotation about +x  .

    Construct the passive quaternion and DCM for a positive 90∘ frame rotation about +x.

    Use the DCM to transform

          ⌊  ⌋
A      0
 v  = ⌈1 ⌉.
       0
  3. Quaternion sign ambiguity.

    Let

    q = 1-(1 − i − j − k) .
    2

    Compute C(q) and C(−q) and show that they are identical.

  4. Conjugate and transpose.

    For the quaternion

        √ --  √ --
    --2-  --2-
q =  2  −  2  k,

    compute C(q) and C(q∗).

    Verify

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

    Interpret the two matrices as opposite passive frame maps.

  5. Recover a quaternion from a simple DCM.

    Given

         ⌊ 0   1  0⌋
     ⌈         ⌉
C =   − 1  0  0  ,
       0   0  1
    (8)

    recover a unit quaternion using the trace formulas.

    State the positive frame axis and angle represented under the PhysicsLibrary passive convention.

  6. Recover a    ∘
180 rotation.

    Consider

         ⌊           ⌋
      1   0    0
C =  ⌈0  − 1   0 ⌉ .

      0   0   − 1
    (9)

    Explain why the simple trace formula based on division by qw is unsuitable. Recover an appropriate unit quaternion using the largest component idea.

    Interpret the result geometrically.

  7. Nontrivial axis angle to DCM.

    Frame B is obtained from frame A by a positive 60∘ rotation about

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

    Construct Bq A and then compute BC A.

  8. Nontrivial DCM round trip.

    Using the DCM from Exercise 7, recover the quaternion using the trace method. Show that the recovered quaternion agrees with the original up to overall sign.

  9. direction cosine columns.

    For the passive 90∘ +z DCM

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

    interpret all three columns geometrically.

    What are the B coordinate representations of the A frame basis directions xA, yA, and zA?

  10. Quaternion and DCM frame chains.

    Suppose

    Bq  =  q ,    Cq  =  q ,
  A     x       B     y

    where

    q  =  1√ −-i,    q  = 1√−-j.
 x      2        y      2

    1. Compute Cq A.
    2. Compute C(qx) and C(qy).
    3. Verify
        C       C    B
C(  qA) =  CB   CA.
  11. Orthogonality and determinant.

    For

    q =  1(1 − i − j − k),
     2

    compute C(q).

    Verify

    C (q)TC (q) = I

    and

    det C (q ) = +1.
  12. Active versus passive convention diagnostic.

    Two software libraries are tested with a declared positive 90∘ rotation about +z.

    Library A returns

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

    while Library B returns

          ⌊ 0  − 1 0 ⌋
      ⌈          ⌉
CB  =   1   0  0  .
        0   0  1

    Under the PhysicsLibrary interpretation, identify which matrix is the passive A → B coordinate map and explain what the other matrix represents.

  13. Check whether a matrix is a DCM.

    Consider

          ⌊                 ⌋
       1     0       0
M  =  ⌈0   0.999   0.040⌉ .

       0  − 0.039  0.998

    Describe the two principal numerical tests that should be performed before treating M as a DCM and converting it to a quaternion.

    Why is component extraction alone not sufficient?

  14. scalar first versus scalar last storage.

    A passive quaternion is displayed by PhysicsLibrary as

        ⌊            ⌋
      0.9238795
    ||      0     ||
q = ⌈      0     ⌉ .
     − 0.3826834

    1. Write the same quaternion in scalar last array storage.
    2. Determine the corresponding positive passive frame rotation.
    3. Explain whether changing storage order changes the DCM formula semantically.
  15. Navigation frame chain.

    Let I denote an inertial frame, N a navigation frame, and B a body frame.

    Suppose

    N
 qI

    and

    Bq
  N

    are known.

    1. Write Bq I.
    2. Write the matching DCM chain BC I.
    3. Write the reverse DCM IC B.
    4. If Ig is a vector known in inertial coordinates, write one DCM expression for Bg.
    5. Write the equivalent quaternion sandwich.

3 Solutions

Solution 1: passive 90∘ frame rotation about +z

The passive quaternion is

       √ --   √ --
B        2      2
  qA = ----−  ---k.
        2      2
(10)

Thus

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

Substitution into the quaternion to DCM formula gives

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

Now

Bv =  BC  Av.
         A

Therefore

      ⌊  0   1  0⌋ ⌊1⌋    ⌊ 0 ⌋
B     ⌈          ⌉ ⌈ ⌉    ⌈   ⌉
  v =   − 1  0  0   0  =   − 1  .
         0   0  1   0       0

Hence

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

The physical vector is fixed; only its coordinate description changed.

Solution 2: passive   ∘
90 frame rotation about +x

The passive quaternion is

       √ --  √ --
Bq  =  --2−  --2i.
  A     2     2
(13)

The corresponding DCM is

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

Apply it to

      ⌊0 ⌋
A     ⌈  ⌉
 v  =  1  .
       0

Then

      ⌊   ⌋
        0
Bv  = ⌈ 0 ⌉ .
        − 1
(15)

Thus a fixed vector originally along +yA has coordinates along −zB after the positive x frame rotation.

Solution 3: quaternion sign ambiguity

For

q =  1(1 − i − j − k),
     2

we have

qw =  1,    qx = qy = qz = − 1-.
      2                      2

The DCM is

        ⌊       ⌋
         0  1  0
C (q) = ⌈0  0  1⌉ .
         1  0  0
(16)

Every term in the DCM formula is quadratic in the quaternion components. Replacing each component by its negative therefore leaves every matrix entry unchanged.

Hence

C (− q) = C(q).
(17)

The two quaternions represent the same orientation.

Solution 4: conjugate and transpose

For

    √ --  √ --
      2     2
q = ----− ----k,
     2     2

we already found

        ⌊ 0   1  0⌋
        ⌈         ⌉
C (q) =  − 1  0  0  .
          0   0  1

The conjugate is

     √ --   √ --
 ∗     2      2
q  = ----+  ---k.
      2      2

Its matrix is

        ⌊ 0  − 1  0⌋
    ∗   ⌈          ⌉
C (q) =   1   0   0  .
          0   0   1
(18)

Clearly,

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

If q = Bq A, then

q∗ = AqB.

Thus C(q) maps A coordinates into B coordinates, while C(q∗) maps B coordinates back into A coordinates.

Solution 5: recover a quaternion from a simple DCM

The DCM is

     ⌊ 0   1  0⌋
     ⌈         ⌉
C =   − 1  0  0  .
       0   0  1

Its trace is

tr(C ) = 1.

Therefore

                 √ --
      1√ -----     2
qw =  -- 1 + 1 = ---.
      2           2

Now

     C32-−-C23-
qx =    4q     =  0,
          w

     C13-−-C31-
qy =    4q     =  0,
          w

and

     C21 − C12
qz = ---4q-----
          w
   = -−√1 −-1-
     4(  2∕2 )
       √ --
   = − --2.
        2

Thus

    √ --  √ --
    --2-  --2-
q =  2  −  2  k.
(20)

This is the passive quaternion for a positive 90∘ frame rotation about +z.

The equivalent quaternion −q represents the same DCM.

Solution 6: recover a 180∘ rotation

The trace is

1 − 1 − 1 = − 1.

The scalar component would therefore satisfy

q  =  1√1--−-1-= 0.
 w    2

The simple off diagonal formulas divide by 4qw, so they are unusable in this case.

The largest diagonal expression is associated with qx:

1 + C11 − C22 − C33 =  1 + 1 − (− 1) − (− 1 ) = 4.

Thus

qx = ±1,

while

qw = qy = qz = 0.

One valid quaternion is

q = − i.
(21)

Under the PhysicsLibrary passive convention,

q =  cos π-− isin π-=  − i.
        2        2

It represents a positive 180∘ frame rotation about +x.

The equivalent quaternion +i produces the same DCM because a 180∘ rotation about +x is also equivalent to a 180∘ rotation about −x.

Solution 7: nontrivial axis angle to DCM

The axis is unit because

12 + 22 + 22
------------ = 1.
      9

For

𝜃 = 60∘,

the half angle is 30∘. Therefore

         √3--               1
cos30∘ = ----,    sin30∘ =  -.
          2                 2

The passive quaternion is

       √ --
BqA  = --3-− 1-i − 1j − 1k.
        2    6     3    3
(22)

Substitution into the DCM formula gives

       ⌊    5     1   √3- 1   √3-⌋
B      | 1  9√3   9 +133  92 − √33-|
 CA  = ⌈ 9 − √3-    18√-  9 + -6-⌉ .
         1 + -3-  2−  -3-    13-
         9    3   9   6      18
(23)

Solution 8: nontrivial DCM round trip

For the DCM in Exercise 7,

tr(C ) = 5-+  13-+ 13-=  2.
        9    18   18

Therefore

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

Using the off diagonal differences,

q =  C32-−-C23-=  − 1,
 x      4qw         6

     C13 − C31      1
qy = ----------=  − -,
        4qw         3

     C21 − C12      1
qz = ----------=  − -.
        4qw         3

Hence the recovered quaternion is

    √ --
q = --3-− 1i − 1-j − 1-k,
     2    6    3    3
(24)

which is exactly the original quaternion.

The negative of this quaternion is an equally valid round trip result.

Solution 9: direction cosine columns

The DCM is

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

Its first column is

⌊    ⌋
   0
⌈ − 1 ⌉,
   0

so

B^x  =  − ^y .
  A       B

Its second column is

⌊ 1⌋
⌈  ⌉
  0  ,
  0

so

B^yA =  +^xB.

Its third column is

⌊  ⌋
  0
⌈ 0⌉ ,

  1

so

B^zA =  +^zB.

Therefore

       [                ]
BCA  =  B^xA   B^yA   B^zA  .
(25)

Solution 10: quaternion and DCM frame chains

The quaternion chain is

C
 qA =  qyqx.

From the preceding composition article,

CqA =  1(1 − i − j − k ).
       2
(26)

The individual DCMs are

         ⌊         ⌋
          1   0   0
C (qx) = ⌈0   0   1⌉
          0  − 1  0
(27)

and

        ⌊ 0  0  − 1⌋
        ⌈          ⌉
C (qy) =   0  1   0   .
          1  0   0
(28)

Multiply:

              ⌊          ⌋ ⌊         ⌋
                0  0  − 1   1   0   0
C (q )C (q ) = ⌈ 0 1   0 ⌉ ⌈0   0   1⌉
    y     x
              ⌊ 1  0   0⌋    0  − 1  0
                0  1  0
            = ⌈ 0  0  1⌉ .

                1  0  0

Direct conversion of

       1
qyqx = --(1 − i − j − k)
       2

gives the same matrix:

          ⌊        ⌋
            0  1  0
C (qyqx) = ⌈ 0  0  1⌉ .
            1  0  0
(29)

Thus

C(qyqx) = C (qy)C(qx).
(30)

Solution 11: orthogonality and determinant

From Solution 3,

        ⌊0  1  0⌋
        ⌈       ⌉
C (q) =  0  0  1  .
         1  0  0

Its transpose is

        ⌊        ⌋
          0  0  1
C(q)T = ⌈ 1  0  0⌉ .
          0  1  0

Then

     T
C (q) C (q ) = I.
(31)

This particular matrix is a cyclic permutation matrix. Its determinant is

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

Therefore it is a proper direction cosine matrix.

Solution 12: active versus passive convention diagnostic

PhysicsLibrary defines the positive 90∘ frame rotation about +z by

       √ --   √ --
B      --2-   --2-
  qA =  2  −   2 k.

Its passive A → B DCM is

⌊          ⌋
   0  1  0
⌈ − 1 0  0 ⌉.
   0  0  1

Therefore Library A matches the PhysicsLibrary passive coordinate convention.

Library B returns its transpose:

CB =  CTA .

That matrix can be interpreted as the reverse passive map

AC
   B

or as the positive active vector rotation matrix for the same +90∘ physical geometry.

The numbers alone do not determine the semantic interpretation; the declared map direction is required.

Solution 13: check whether a matrix is a DCM

The first test is orthogonality:

M  TM  ≈ I.
(33)

The second test is proper handedness:

det M  ≈ +1.
(34)

The tolerance should be chosen according to the expected numerical error in the application.

Quaternion extraction formulas assume that the matrix already represents a proper orientation. A matrix with significant scaling, shear, or reflection can still produce real numbers when substituted into component formulas, but those numbers need not represent the intended physical attitude.

Therefore matrix validity should be checked before conversion.

Solution 14: scalar first versus scalar last storage

PhysicsLibrary scalar first storage is

⌊            ⌋
  0.9238795
||      0     ||
⌈      0     ⌉ .

 − 0.3826834

Scalar last storage of the same quaternion is

⌊            ⌋
       0
||      0     || .
⌈− 0.3826834 ⌉
  0.9238795
(35)

Since

                    ∘
0.9238795 ≈  cos22.5

and

0.3826834 ≈  sin 22.5∘,

the quaternion is approximately

q = cos22.5∘ − k sin 22.5∘.

Therefore it represents a positive passive frame rotation of

  ∘
45
(36)

about +z.

Changing storage order does not change the quaternion algebra or the physical map. Software must merely place the correct semantic components into the correct indices before applying its DCM conversion routine.

Solution 15: navigation frame chain

The quaternion chain is

Bq  =  Bq  Nq .
   I     N   I
(37)

The matching DCM chain is

B      B    N
  CI =   CN   CI.
(38)

The reverse DCM is

I       B    T
 CB  = (  CI) .
(39)

If Ig is known, then the body coordinates are

B     B   I
  g =  CI  g.
(40)

The equivalent quaternion expression is

B     B   I  B   ∗
  g =  qI  g( qI) .
(41)

Substituting the frame chain explicitly gives

B    (B    N  )I  (B   N   )∗
 g =    qN  qI  g   qN   qI  .
(42)

Quaternion and DCM chains therefore preserve the same frame ordering.

4 Compact verification table



Test PhysicsLibrary passive result


Positive 90∘ frame rotation about +z  q = (1 − k)∕√ --
  2


Corresponding DCM ⌊          ⌋
   0   1  0
⌈ − 1  0  0⌉

   0   0  1


A  -frame + ^x  coordinates B  -frame − ^y  coordinates


Reverse map C(q∗) = C(q)T


Quaternion sign C(−q) = C(q)


Frame composition C(pq) = C(p)C(q)


Valid DCM CT C = I, det C = +1


5 Sources and exercise provenance

The exercises and solutions in this companion are newly written or rewritten for PhysicsLibrary under the passive frame convention.

Sommer and coauthors provide a modern convention analysis that is especially useful for distinguishing Hamilton multiplication from active and passive matrix assignments. Moore provides an openly licensed treatment of reference frames and direction cosine matrices. Henderson provides an important historical engineering reference for explicit transformations 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.


"example of 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


This object's parent.

Cross-references: Euler Angles, composition, vector, scalar, determinant, direction cosine, trace, matrices, formula, direction cosine matrix, quaternion, unit, representation, quaternions and direction cosine matrices
There is 1 reference to this object.

This is version 2 of example of quaternions and direction cosine matrices, born on 2026-08-24, modified 2026-08-27.
Object id is 1102, canonical name is ExampleOfQuaternionsAndDirectionCosineMatrices.
Accessed 250 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