<?xml version="1.0" encoding="UTF-8"?>

<record version="1" id="1101">
 <title>quaternions and direction cosine matrices</title>
 <name>QuaternionsAndDirectionCosineMatrices</name>
 <created>2026-08-24 00:20:09</created>
 <modified>2026-08-24 00:20:09</modified>
 <type>Topic</type>
 <creator id="1" name="bloftin"/>
 <modifier id="1" name="bloftin"/>
 <author id="1" name="bloftin"/>
 <classification>
	<category scheme="pacs" code="02.40.Yy"/>
	<category scheme="pacs" code="02.10.Hh"/>
	<category scheme="pacs" code="45.40.-f"/>
 </classification>
 <keywords>
	<term>quaternion</term>
	<term>direction cosine matrix</term>
	<term>DCM</term>
	<term>rotation matrix</term>
	<term>active rotation</term>
	<term>passive rotation</term>
	<term>frame transformation</term>
	<term>attitude matrix</term>
 </keywords>
 <preamble>% this is the default PhysicsLibrary preamble.  as your knowledge
% of TeX increases, you will probably want to edit this, but
% it should be fine as is for beginners.

% almost certainly you want these
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsfonts}

% used for TeXing text within eps files
%\usepackage{psfrag}
% need this for including graphics (\includegraphics).
% If you use \includegraphics{figure} with no file extension, PhysicsLibrary
% will prefer figure.png before falling back to figure.eps.
\usepackage{graphicx}
\DeclareGraphicsExtensions{.png,.jpg,.jpeg,.pdf,.eps}
% for neatly defining theorems and propositions
%\usepackage{amsthm}
% making logically defined graphics
%\usepackage{xypic}

% there are many more packages, add them here as you need them

% define commands here</preamble>
 <content>\section*{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.

\section{Convention declaration}

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

\section{From the quaternion sandwich to a matrix}

Write the unit quaternion as
\begin{equation}
q=q_w+\mathbf q,
\end{equation}
where $q_w$ is the scalar part and $\mathbf q$ is the pure-vector part.  From
Q07, the quaternion vector-rotation formula is
\begin{equation}
\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).
\end{equation}
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:
\begin{equation}
(q_w^2-\mathbf q\cdot\mathbf q)\mathbf v
= (q_w^2-\mathbf q^T\mathbf q)I_3\,\mathbf v,
\end{equation}
\begin{equation}
2\mathbf q(\mathbf q\cdot\mathbf v)
=2\mathbf q\mathbf q^T\mathbf v,
\end{equation}
and
\begin{equation}
2q_w(\mathbf q\times\mathbf v)=2q_w[\mathbf q]_\times\mathbf v,
\end{equation}
where
\begin{equation}
[\mathbf q]_\times=
\begin{bmatrix}
0 &amp; -q_z &amp; q_y\\
q_z &amp; 0 &amp; -q_x\\
-q_y &amp; q_x &amp; 0
\end{bmatrix}
\end{equation}
is the skew-symmetric matrix for the cross product.  Therefore,
\begin{equation}
\boxed{
R(q)=
(q_w^2-\mathbf q^T\mathbf q)I_3
+2\mathbf q\mathbf q^T
+2q_w[\mathbf q]_\times.
}
\end{equation}
This compact matrix formula is often the cleanest way to derive properties.

\section{Component form}

Expanding equation (10) gives the familiar scalar-first component formula
\begin{equation}
\boxed{
R(q)=
\begin{bmatrix}
1-2(q_y^2+q_z^2) &amp; 2(q_xq_y-q_wq_z) &amp; 2(q_xq_z+q_wq_y)\\
2(q_xq_y+q_wq_z) &amp; 1-2(q_x^2+q_z^2) &amp; 2(q_yq_z-q_wq_x)\\
2(q_xq_z-q_wq_y) &amp; 2(q_yq_z+q_wq_x) &amp; 1-2(q_x^2+q_y^2)
\end{bmatrix}.
}
\end{equation}
If $q$ is unit, then $R(q)$ is orthogonal with determinant $+1$, so it is a
proper rotation matrix.

\section{What the columns mean}

The columns of $R(q)$ 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
\begin{equation}
R(q)=
\begin{bmatrix}
R(q)\mathbf e_1 &amp; R(q)\mathbf e_2 &amp; R(q)\mathbf e_3
\end{bmatrix}.
\end{equation}
Figure~\ref{fig:q09-dcm} shows this geometry for a representative quaternion.

\begin{figure}[h]
\centering
\includegraphics[width=0.87\textwidth]{Q09_quaternion_dcm_geometry.png}
\caption{A unit quaternion and its direction cosine matrix.  The original basis
vectors are the $x$, $y$, and $z$ axes.  The columns of $R(q)$ 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 $q$ and $-q$.}
\label{fig:q09-dcm}
\end{figure}

\section{Example: quarter-turn about the $z$ axis}

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

\section{Orthogonality, inverse, and conjugation}

For a unit quaternion,
\begin{equation}
\|q\|=1,
\qquad
q^{-1}=q^*.
\end{equation}
The inverse active rotation is therefore
\begin{equation}
\mathbf v = q^*\mathbf v' q.
\end{equation}
At the matrix level this means
\begin{equation}
\boxed{R(q^*)=R(q)^{-1}=R(q)^T.}
\end{equation}
So quaternion conjugation corresponds to matrix transposition for unit
quaternions.

\section{The sign ambiguity: $q$ and $-q$}

Because equation (10) contains either quadratic terms in the quaternion
components or the product $q_w\mathbf q$, replacing $q$ by $-q$ leaves the
matrix unchanged.  Therefore,
\begin{equation}
\boxed{R(-q)=R(q).}
\end{equation}
This is the matrix version of the familiar fact that $q$ and $-q$ represent the
same physical orientation.

\section{Passive coordinate transformations and DCM notation}

In mechanics and aerospace work, the phrase \emph{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
\begin{equation}
{}^{A}\mathbf v = C^A_B\,{}^{B}\mathbf v.
\end{equation}
With the PhysicsLibrary frame quaternion ${}^{A}q_B$, the associated matrix is
\begin{equation}
\boxed{C^A_B = R({}^{A}q_B).}
\end{equation}
The frame-chain rule is therefore
\begin{equation}
\boxed{C^N_B=C^N_A C^A_B,}
\end{equation}
which matches the quaternion chain
${}^{N}q_B={}^{N}q_A{}^{A}q_B$.

\section{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
\begin{equation}
\boxed{
q_w = \frac{1}{2}\sqrt{1+r_{11}+r_{22}+r_{33}}.
}
\end{equation}
Once $q_w$ is known, the vector components follow from
\begin{equation}
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}.
\end{equation}
These formulas are simple but become numerically fragile when $q_w$ 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.

\section{A useful special case}

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

\section{Common pitfalls}

The most common mistakes are:
\begin{enumerate}
\item using the wrong component order when copying a formula from a source that
assumes scalar-last storage;
\item mixing active and passive interpretations without taking the transpose or
inverse;
\item assuming that the rows of the active matrix are the rotated basis vectors
rather than the columns;
\item forgetting that $q$ and $-q$ give the same matrix;
\item recovering a quaternion from a matrix using the trace formula near
$180^\circ$ without using a numerically safer branch.
\end{enumerate}

\section{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.

\section{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.

\section*{License}

This article is an original synthesis prepared for PhysicsLibrary and intended
for release under CC BY-SA 4.0.</content>
</record>
