Rotation matrix (source code)

= Rotation matrix
{wiki}

A rotation matrix is an orthogonal matrix with determinant $1$.

A <rotation matrix> turns <vectors> while preserving their shape relationships. For column <coordinate vectors> in an <orthonormal basis>, its defining conditions are
$$
R^TR=I,\qquad \det R=1,
$$
where $R^T$ is the <transpose> and $I$ the <identity matrix>. In the usual <Cartesian basis> of the <plane>, an active counterclockwise <planar rotation> through an <angle> $\theta$ uses
$$
R_\theta=\begin{pmatrix}\cos\theta&-\sin\theta\\\sin\theta&\cos\theta\end{pmatrix},\qquad v'=R_\theta v.
$$
Here the <vector> moves and the <basis> stays fixed: $R_{\pi/2}(1,0)^T=(0,1)^T$. Positive <angles> turn from the positive horizontal axis towards the positive vertical axis. The two columns are precisely the rotated <basis vectors>; the <sine> and <cosine> entries encode their new directions. Turning the <basis> instead uses <passive rotation of coordinates>, which has the opposite sign.

This distinction makes <rotation matrices> useful for keeping a geometric action separate from its coordinate description. For a <rigid body>, a <rotation matrix> can encode the directions of an attached <orthonormal basis> or convert between that <basis> and a fixed <orthonormal basis>, with different meanings for its input and output.<Modern Robotics: Mechanics, Planning, and Control by Kevin M. Lynch and Frank C. Park>[Modern Robotics, §3.2.1.2, preprint p. 69: three uses of a rotation matrix]{ref}

The <orthogonal matrix> condition preserves every <dot product>:
$$
(Ru)\cdot(Rv)=(Ru)^T(Rv)=u^TR^TRv=u\cdot v.
$$
Consequently <Euclidean norms> and <angles> between nonzero <vectors> are preserved, as is the <Euclidean norm> of the difference between any two endpoints. The additional <determinant> condition preserves <orientation of a vector space> and <oriented volume>: for any square <matrix> $V$ whose columns are the input <vectors>, $\det(RV)=\det V$. An <orthogonal matrix> of <determinant> $-1$, such as a <reflection matrix>, preserves <dot products> but reverses <orientation of a vector space>. The <matrix inverse> is especially simple: $R^{-1}=R^T$.<Modern Robotics: Mechanics, Planning, and Control by Kevin M. Lynch and Frank C. Park>[Modern Robotics, §3.2.1, definition 3.1 and propositions 3.3–3.6, preprint pp. 67–69]{ref}