Physics Library
 An open source physics library
Encyclopedia | Forums | Docs | Random | Template Test |  
Login
create new user
Username:
Password:
forget your password?
Main Menu
Sections

Meta

Talkback

Downloads

Information
quaternions and direction cosine matrices (Topic)

Quaternions and Direction Cosine Matrices

Unit quaternions and Direction Cosine matrices are two standard ways to encode three-dimensional orientation. The quaternion form is compact and avoids the singularities of Euler angles; the matrix form acts directly on vectors, exposes the geometry of the rotated basis, and interfaces naturally with rigid-body kinematics and linear algebra software. This entry derives the matrix associated with a unit quaternion, explains the relation between active and passive usage, and shows how to move between the two representations.

Convention declaration

This article uses the PhysicsLibrary house convention:

  1. right-handed orthonormal frames;
  2. Hamilton multiplication, so $\mathbf i\mathbf j=\mathbf k$;
  3. scalar-first display notation,
    $\displaystyle q=q_w+q_x\mathbf i+q_y\mathbf j+q_z\mathbf k$ (1)
    with vector part
    $\displaystyle \mathbf q = \begin{bmatrix} q_x\\ q_y\\ q_z \end{bmatrix};$ (2)
  4. active vector rotation by a unit quaternion,
    $\displaystyle \boxed{\mathbf v' = q\mathbf v q^*.}$ (3)
The associated active rotation matrix is denoted by $R(q)$, so that
$\displaystyle \boxed{\mathbf v' = R(q)\mathbf v.}$ (4)
The same quaternion written in scalar-last storage order $[q_x,q_y,q_z,q_w]^T$ represents the same physical rotation.

From the quaternion sandwich to a matrix

Write the unit quaternion as

$\displaystyle q=q_w+\mathbf q,$ (5)
where $q_w$ is the scalar part and $\mathbf q$ is the pure-vector part. From Q07, the quaternion vector-rotation formula is
$\displaystyle \mathbf v'= (q_w^2-\mathbf q\cdot\mathbf q)\mathbf v +2\mathbf q(\mathbf q\cdot\mathbf v) +2q_w(\mathbf q\times\mathbf v).$ (6)
This is already linear in $\mathbf v$, so it must have the form $\mathbf v' = R(q)\mathbf v$. The three terms may be written as matrix actions:
$\displaystyle (q_w^2-\mathbf q\cdot\mathbf q)\mathbf v = (q_w^2-\mathbf q^T\mathbf q)I_3\,\mathbf v,$ (7)
$\displaystyle 2\mathbf q(\mathbf q\cdot\mathbf v) =2\mathbf q\mathbf q^T\mathbf v,$ (8)
and
$\displaystyle 2q_w(\mathbf q\times\mathbf v)=2q_w[\mathbf q]_\times\mathbf v,$ (9)
where
$\displaystyle [\mathbf q]_\times= \begin{bmatrix} 0 & -q_z & q_y\ q_z & 0 & -q_x\ -q_y & q_x & 0 \end{bmatrix}$ (10)
is the skew-symmetric matrix for the cross product. Therefore,
$\displaystyle \boxed{ R(q)= (q_w^2-\mathbf q^T\mathbf q)I_3 +2\mathbf q\mathbf q^T +2q_w[\mathbf q]_\times. }$ (11)
This compact matrix formula is often the cleanest way to derive properties.

Component form

Expanding equation (10) gives the familiar scalar-first component formula

$\displaystyle \boxed{ R(q)= \begin{bmatrix} 1-2(q_y^2+q_z^2) & 2(q_xq_y-q_wq_z)... ...q_x)\ 2(q_xq_z-q_wq_y) & 2(q_yq_z+q_wq_x) & 1-2(q_x^2+q_y^2) \end{bmatrix}. }$ (12)
If $q$ is unit, then is orthogonal with determinant $+1$, so it is a proper rotation matrix.

What the columns mean

The columns of are the images of the basis vectors under the active rotation. If $\mathbf e_1$, $\mathbf e_2$, and $\mathbf e_3$ are the standard basis vectors, then

$\displaystyle R(q)= \begin{bmatrix} R(q)\mathbf e_1 & R(q)\mathbf e_2 & R(q)\mathbf e_3 \end{bmatrix}.$ (13)
Figure [*] shows this geometry for a representative quaternion.
Figure: A unit quaternion and its direction cosine matrix. The original basis vectors are the $x$, $y$, and $z$ axes. The columns of are the rotated basis vectors $R(q)\mathbf e_1$, $R(q)\mathbf e_2$, and $R(q)\mathbf e_3$. The same matrix is produced by and $-q$.
Image Q09_quaternion_dcm_geometry

Example: quarter-turn about the axis

Let

$\displaystyle q=\cos\frac{\pi}{4}+\mathbf k\sin\frac{\pi}{4} =\frac{1+\mathbf k}{\sqrt2}.$ (14)
Then $q_w=q_z=1/\sqrt2$ and $q_x=q_y=0$, so equation (11) becomes
$\displaystyle R(q)= \begin{bmatrix} 0 & -1 & 0\ 1 & 0 & 0\ 0 & 0 & 1 \end{bmatrix}.$ (15)
This is exactly the matrix for a $+90^\circ$ active rotation about the axis:
$\displaystyle R(q) \begin{bmatrix}1\\ 0\\ 0\end{bmatrix}= \begin{bmatrix}0\\ 1\... ... \begin{bmatrix}0\\ 1\\ 0\end{bmatrix}= \begin{bmatrix}-1\\ 0\\ 0\end{bmatrix}.$ (16)

Orthogonality, inverse, and conjugation

For a unit quaternion,

$\displaystyle \Vert q\Vert=1, \qquad q^{-1}=q^*.$ (17)
The inverse active rotation is therefore
$\displaystyle \mathbf v = q^*\mathbf v' q.$ (18)
At the matrix level this means
$\displaystyle \boxed{R(q^*)=R(q)^{-1}=R(q)^T.}$ (19)
So quaternion conjugation corresponds to matrix transposition for unit quaternions.

The sign ambiguity: and

Because equation (10) contains either quadratic terms in the quaternion components or the product $q_w\mathbf q$, replacing by leaves the matrix unchanged. Therefore,

$\displaystyle \boxed{R(-q)=R(q).}$ (20)
This is the matrix version of the familiar fact that and represent the same physical orientation.

Passive coordinate transformations and DCM notation

In mechanics and aerospace work, the phrase direction cosine matrix often refers to a passive coordinate transformation matrix. If ${}^{A}\mathbf v$ and ${}^{B}\mathbf v$ denote the coordinates of the same geometric vector in frames $A$ and $B$, then the passive mapping is written

$\displaystyle {}^{A}\mathbf v = C^A_B\,{}^{B}\mathbf v.$ (21)
With the PhysicsLibrary frame quaternion ${}^{A}q_B$, the associated matrix is
$\displaystyle \boxed{C^A_B = R({}^{A}q_B).}$ (22)
The frame-chain rule is therefore
$\displaystyle \boxed{C^N_B=C^N_A C^A_B,}$ (23)
which matches the quaternion chain ${}^{N}q_B={}^{N}q_A{}^{A}q_B$.

Recovering a quaternion from a rotation matrix

Suppose $R=[r_{ij}]$ is a proper rotation matrix. If the trace is not too close to $-1$, a convenient formula is

$\displaystyle \boxed{ q_w = \frac{1}{2}\sqrt{1+r_{11}+r_{22}+r_{33}}. }$ (24)
Once is known, the vector components follow from
$\displaystyle q_x = \frac{r_{32}-r_{23}}{4q_w}, \qquad q_y = \frac{r_{13}-r_{31}}{4q_w}, \qquad q_z = \frac{r_{21}-r_{12}}{4q_w}.$ (25)
These formulas are simple but become numerically fragile when is small, for example near a $180^\circ$ rotation. In numerical work one then switches to branch formulas based on whichever of $q_w^2$, $q_x^2$, $q_y^2$, or $q_z^2$ is largest.

A useful special case

For a unit quaternion written in axis-angle form,

$\displaystyle q=\cos\frac{\theta}{2}+\widehat{\mathbf u}\sin\frac{\theta}{2},$ (26)
substituting into equation (10) yields the matrix Rodrigues formula
$\displaystyle \boxed{R(q)=I_3\cos\theta +(1-\cos\theta)\widehat{\mathbf u}\widehat{\mathbf u}^T + [\widehat{\mathbf u}]_\times\sin\theta.}$ (27)
Thus the quaternion-to-matrix map reproduces the standard axis-angle rotation matrix automatically.

Common pitfalls

The most common mistakes are:

  1. using the wrong component order when copying a formula from a source that assumes scalar-last storage;
  2. mixing active and passive interpretations without taking the transpose or inverse;
  3. assuming that the rows of the active matrix are the rotated basis vectors rather than the columns;
  4. forgetting that and give the same matrix;
  5. recovering a quaternion from a matrix using the trace formula near without using a numerically safer branch.

What comes next

The next entry connects quaternions to Euler angles and shows how the familiar 3-2-1 yaw-pitch-roll formulas fit into the quaternion framework.

Sources and historical notes

The matrix formula follows directly from the quaternion rotation law and the scalar-vector product rules. The active/passive distinction and frame-chain interpretation are standard in multibody dynamics, spacecraft attitude work, and inertial navigation. The compact form in equation (10) is especially useful for proofs, while the expanded component form in equation (11) is common in implementations.

License

This article is an original synthesis prepared for PhysicsLibrary and intended for release under CC BY-SA 4.0.



"quaternions and direction cosine matrices" is owned by bloftin.
(view preamble)
View style:
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: trace, direction cosine matrix, work, mechanics, conjugation, determinant, cross product, skew-symmetric matrix, formula, scalar, representations, relation, kinematics, vectors, matrix, quaternion
There is 1 reference to this object.

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

Classification:
Physics Classification02.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
Style: Expand: Order:

No messages.

Interact
rate | post | correct | update request | add example | add (any)