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

<record version="1" id="1102">
 <title>example of quaternions and direction cosine matrices</title>
 <name>ExampleOfQuaternionsAndDirectionCosineMatrices</name>
 <created>2026-08-24 00:36:15</created>
 <modified>2026-08-24 00:36:15</modified>
 <type>Example</type>
<parent id="1101">quaternions and direction cosine matrices</parent>
 <creator id="1" name="bloftin"/>
 <modifier id="1" name="bloftin"/>
 <comment>converted to passive rotation convention</comment>
 <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>
 <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: Examples, Exercises, and Solutions}

This companion article belongs to the PhysicsLibrary entry
\emph{Quaternions and  Direction Cosine Matrices}.  All exercises are stated
first.  Complete solutions appear only after the exercise section so that the
article is self study friendly.

\section{Convention declaration}

We use right-handed frames, Hamilton multiplication, scalar-first display
notation, and the active rotation rule
\begin{equation}
\mathbf v' = q\mathbf v q^* = R(q)\mathbf v
\end{equation}
for unit quaternions.  Passive coordinate changes are represented by the
associated DCM relation
\begin{equation}
{}^{A}\mathbf v = C^A_B\,{}^{B}\mathbf v,
\qquad
C^A_B = R({}^{A}q_B).
\end{equation}

\section{Exercises}

\subsection*{Exercise 1}
Starting from the vector rotation formula
\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}
derive the compact matrix formula for $R(q)$.

\subsection*{Exercise 2}
Expand the compact formula to obtain the component matrix
\begin{equation}
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}

\subsection*{Exercise 3}
For
\begin{equation}
q=\frac{1+\mathbf k}{\sqrt2},
\end{equation}
compute $R(q)$.

\subsection*{Exercise 4}
Using the matrix from Exercise 3, compute the images of
$\mathbf e_1$, $\mathbf e_2$, and $\mathbf e_3$.

\subsection*{Exercise 5}
Show directly from the compact matrix formula that $R(-q)=R(q)$.

\subsection*{Exercise 6}
Show that for a unit quaternion,
\begin{equation}
R(q^*)=R(q)^T.
\end{equation}

\subsection*{Exercise 7}
Let
\begin{equation}
q=\cos\frac{\pi}{3}+\mathbf i\sin\frac{\pi}{3}
=\frac{1}{2}+\frac{\sqrt3}{2}\mathbf i.
\end{equation}
Find $R(q)$.

\subsection*{Exercise 8}
Let
\begin{equation}
q=\cos\frac{\pi}{3}+\frac{\mathbf i+\mathbf j+\mathbf k}{\sqrt3}\sin\frac{\pi}{3}.
\end{equation}
Compute $R(q)$ and interpret the result geometrically.

\subsection*{Exercise 9}
Show that the columns of $R(q)$ are the images of
$\mathbf e_1$, $\mathbf e_2$, and $\mathbf e_3$.

\subsection*{Exercise 10}
Write the passive DCM relation for the frame chain $B \to A \to N$ and derive
\begin{equation}
C^N_B = C^N_A C^A_B.
\end{equation}

\subsection*{Exercise 11}
Suppose
\begin{equation}
R=
\begin{bmatrix}
0 &amp; -1 &amp; 0\\
1 &amp; 0 &amp; 0\\
0 &amp; 0 &amp; 1
\end{bmatrix}.
\end{equation}
Recover a quaternion representing this rotation.

\subsection*{Exercise 12}
Given the proper rotation matrix
\begin{equation}
R=
\begin{bmatrix}
1 &amp; 0 &amp; 0\\
0 &amp; -1 &amp; 0\\
0 &amp; 0 &amp; -1
\end{bmatrix},
\end{equation}
find a quaternion representing it and explain why the trace formula using
$q_w$ is numerically delicate here.

\subsection*{Exercise 13}
Show that if $q$ is unit, then $R(q)$ is orthogonal and has determinant $+1$.

\subsection*{Exercise 14}
Explain the difference between the active matrix relation
$\mathbf v'=R(q)\mathbf v$ and the passive DCM relation
${}^{A}\mathbf v=C^A_B\,{}^{B}\mathbf v$.

\subsection*{Exercise 15}
A software package stores quaternions in scalar-last order.
A student copies the scalar-first matrix formula without changing the component
mapping.  Describe the error and how to fix it.

\section{Solutions}

\subsection*{Solution 1}
Write the three terms separately:
\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}
Adding the three contributions gives
\begin{equation}
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}

\subsection*{Solution 2}
Insert
\begin{equation}
\mathbf q=
\begin{bmatrix}q_x\\q_y\\q_z\end{bmatrix},
\qquad
[\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}
into the compact matrix formula and expand entry by entry.  Using
$q_w^2+q_x^2+q_y^2+q_z^2=1$ simplifies the diagonal terms, yielding the stated
matrix.

\subsection*{Solution 3}
For
$q_w=q_z=1/\sqrt2$ and $q_x=q_y=0$, so
\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}

\subsection*{Solution 4}
The columns are the images of the basis vectors, so
\begin{equation}
R(q)\mathbf e_1=
\begin{bmatrix}0\\1\\0\end{bmatrix},
\qquad
R(q)\mathbf e_2=
\begin{bmatrix}-1\\0\\0\end{bmatrix},
\qquad
R(q)\mathbf e_3=
\begin{bmatrix}0\\0\\1\end{bmatrix}.
\end{equation}
Thus the $x$ axis rotates into $y$, the $y$ axis rotates into $-x$, and the
$z$ axis is unchanged.

\subsection*{Solution 5}
Replacing $q$ by $-q$ changes $q_w$ to $-q_w$ and $\mathbf q$ to
$-\mathbf q$.  The terms in the matrix become
\begin{equation}
(-q_w)^2 - (-\mathbf q)^T(-\mathbf q) = q_w^2-\mathbf q^T\mathbf q,
\end{equation}
\begin{equation}
2(-\mathbf q)(-\mathbf q)^T = 2\mathbf q\mathbf q^T,
\end{equation}
and
\begin{equation}
2(-q_w)[-\mathbf q]_\times = 2q_w[\mathbf q]_\times.
\end{equation}
Hence $R(-q)=R(q)$.

\subsection*{Solution 6}
A unit quaternion satisfies $q^{-1}=q^*$.  The inverse active rotation matrix is
therefore the transpose of the forward matrix.  More directly, quaternion
conjugation corresponds to reversing the rotation, so
\begin{equation}
R(q^*)=R(q)^{-1}=R(q)^T.
\end{equation}

\subsection*{Solution 7}
Here $q_w=1/2$, $q_x=\sqrt3/2$, and $q_y=q_z=0$.  Therefore,
\begin{equation}
R(q)=
\begin{bmatrix}
1 &amp; 0 &amp; 0\\
0 &amp; -\frac12 &amp; -\frac{\sqrt3}{2}\\
0 &amp; \frac{\sqrt3}{2} &amp; -\frac12
\end{bmatrix}.
\end{equation}
This is the matrix for a $120^\circ$ active rotation about the $x$ axis.

\subsection*{Solution 8}
Here
\begin{equation}
q_w=\frac12,
\qquad
q_x=q_y=q_z=\frac12.
\end{equation}
Substituting into the component formula gives
\begin{equation}
R(q)=
\begin{bmatrix}
0 &amp; 0 &amp; 1\\
1 &amp; 0 &amp; 0\\
0 &amp; 1 &amp; 0
\end{bmatrix}.
\end{equation}
This cyclically permutes the coordinate axes:
$\mathbf e_1\mapsto\mathbf e_2$, $\mathbf e_2\mapsto\mathbf e_3$, and
$\mathbf e_3\mapsto\mathbf e_1$.

\subsection*{Solution 9}
For any matrix $A$,
\begin{equation}
A=
\begin{bmatrix}
A\mathbf e_1 &amp; A\mathbf e_2 &amp; A\mathbf e_3
\end{bmatrix}.
\end{equation}
Applying this to $A=R(q)$ shows that its columns are exactly the rotated basis
vectors.

\subsection*{Solution 10}
The passive chain is
\begin{equation}
{}^{N}\mathbf v = C^N_A\,{}^{A}\mathbf v,
\qquad
{}^{A}\mathbf v = C^A_B\,{}^{B}\mathbf v.
\end{equation}
Substituting the second relation into the first gives
\begin{equation}
{}^{N}\mathbf v = C^N_A C^A_B\,{}^{B}\mathbf v,
\end{equation}
so
\begin{equation}
C^N_B = C^N_A C^A_B.
\end{equation}

\subsection*{Solution 11}
The matrix is the $+90^\circ$ active rotation about the $z$ axis, so one choice
is
\begin{equation}
q=\frac{1+\mathbf k}{\sqrt2}.
\end{equation}
The equally valid choice $-q$ represents the same matrix.

\subsection*{Solution 12}
The matrix is a $180^\circ$ rotation about the $x$ axis, so one choice is
\begin{equation}
q=\mathbf i.
\end{equation}
The trace is $1-1-1=-1$, so the formula
\begin{equation}
q_w = \frac12\sqrt{1+\operatorname{tr}(R)}
\end{equation}
returns $q_w=0$.  Dividing by $4q_w$ is therefore numerically dangerous.  One
must instead use a branch based on the large value of $q_x$.

\subsection*{Solution 13}
For a unit quaternion the corresponding map is a proper rotation, so it
preserves dot products and orientation.  Hence the matrix is orthogonal and has
determinant $+1$.  Algebraically one can verify
\begin{equation}
R(q)^T R(q)=I_3
\end{equation}
from the compact formula or from the fact that the inverse rotation is given by
$q^*$.

\subsection*{Solution 14}
The active formula $\mathbf v'=R(q)\mathbf v$ rotates the vector itself in a
fixed basis.  The passive formula ${}^{A}\mathbf v=C^A_B\,{}^{B}\mathbf v$
changes the coordinates used to describe the same geometric vector.  The same
orientation change may be described either way, but the physical interpretation
is different.

\subsection*{Solution 15}
The student has confused storage layout with component meaning.  In the
scalar-first formula, the first symbol is the scalar component $q_w$.  If a
library stores $[q_x,q_y,q_z,q_w]^T$, the entries must be remapped before the
formula is used.  Otherwise the resulting matrix corresponds to a different
quaternion.

\section*{License}

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