Rotation matrix

In matrix theory, a rotation matrix is a real square matrix whose transpose is its inverse and whose determinant is +1 (i.e. it is a real special orthogonal matrix)
 * $$Q^{T} = Q^{-1}$$
 * $$\det Q = +1$$

The matrix is so-called because it geometrically corresponds to a linear map that sends vectors to a corresponding vector rotated about the origin by a fixed angle.

Rotation matrices can be generalized up to n-dimensional Euclidean space and can operate over any commutative scalar Field, however for real applications it is typical just to consider rotation matrices in 2 or 3 dimensions and with real components.

The 2-dimensional rotation matrix
$$ R_\theta = \begin{bmatrix} \cos(\theta) & \sin(\theta) \\ -\sin(\theta) & \cos(\theta) \\ \end{bmatrix} $$

Is a left-handed 2-dimensional rotation $$\theta$$ radians clockwise about the origin.

For a counterclockwise rotation, or for a clockwise rotation of the coordinate system (keeping the point steady), the above matrix can be used, replacing $$\theta$$ with $$-\theta$$

The 3-dimensional rotation matrices
There are three 3-dimensional rotation matrices which correspond to rotations about the x, y and z axes respectively.

$$ R_{x,\theta} = \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos(\theta) & -\sin(\theta) \\ 0 & \sin(\theta) & \cos(\theta) \\ \end{bmatrix} $$

$$ R_{y,\theta} = \begin{bmatrix} \cos(\theta) & 0 & \sin(\theta) \\ 0 & 1 & 0 \\ -\sin(\theta) & 0 & \cos(\theta) \\ \end{bmatrix} $$

$$ R_{z,\theta} = \begin{bmatrix} \cos(\theta) & -\sin(\theta) & 0 \\ \sin(\theta) & \cos(\theta) & 0\\ 0 & 0 & 1\\ \end{bmatrix} $$

where $$R_{L, b} v$$ sends the vector $$v \in \mathbb{R}$$ to the vector $$v$$ rotated $$b$$ radians clockwise (when looking along the axis with the positive axis stretching out in front of you) about the axis $$L$$.

To find the rotation matrix corresponding to a rotation anti-clockwise about the axis, or the rotation of the coordinate system about an axis, replace $$\theta$$ with $$-\theta$$ in any of the matrices.

In aeronautics and flight dynamics, rotation about the x, y and z axes are named roll, pitch and yaw respectively, where the coordinate system has the x-axis along the direction of flight and the positive y-axis pointing to the right.

Properties of a rotation matrix
For any rotation matrix $$R_{axis,\theta} \in \mathbb{R}^n $$ and $$I$$, the identity in $$I \in \mathbb{R}^n$$


 * $$R_{a, (\theta + r)} = R_{a, \theta} \cdot R_{a,r}$$
 * $$ R_{a, 0} = \mathbb{I} $$
 * $$ R_{a, \theta}^T = R_{a, \theta}^{-1} $$
 * $$ det(R_{a, \theta}) = 1 $$

Examples
 The 2×2 rotation matrix
 * $$ Q = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix} $$

corresponds to a 90° planar rotation.

The transpose of the 2×2 matrix
 * $$ M = \begin{bmatrix} 0.936 & 0.352 \\ 0.352 & -0.936 \end{bmatrix} $$

is its inverse, but since its determinant is −1 this is not a rotation matrix; it is a reflection across the line 11y = 2x.

The 3×3 rotation matrix
 * $$ Q = \begin{bmatrix} 1 & 0 & 0 \\ 0 & \frac{\sqrt{3}}{2} & \frac12 \\ 0 & -\frac12 & \frac{\sqrt{3}}{2} \end{bmatrix} $$

corresponds to a −30° rotation around the x axis in three-dimensional space.

The 3×3 rotation matrix
 * $$ Q = \begin{bmatrix} 0.36 & 0.48 & -0.8 \\ -0.8 & 0.60 & 0 \\ 0.48 & 0.64 & 0.60 \end{bmatrix} $$

corresponds to a rotation of approximately 74° around the axis (−1&frasl;3,2&frasl;3,2&frasl;3) in three-dimensional space.

The 3×3 permutation matrix
 * $$ P = \begin{bmatrix} 0 & 0 & 1 \\ 1 & 0 & 0 \\ 0 & 1 & 0 \end{bmatrix} $$

is also a rotation matrix, as is the matrix of any even permutation (but never of any odd permutation). 

 The 3×3 matrix
 * $$ M = \begin{bmatrix} 3 & -4 & 1 \\ 5 & 3 & -7 \\ -9 & 2 & 6 \end{bmatrix} $$

has determinant +1, but its transpose is not its inverse, so it is not a rotation matrix.

The 4×3 matrix
 * $$ M = \begin{bmatrix} 0.5 & -0.1 & 0.7 \\ 0.1 & 0.5 & -0.5 \\ -0.7 & 0.5 & 0.5 \\ -0.5 & -0.7 & -0.1 \end{bmatrix} $$

is not square, and so cannot be a rotation matrix; yet QTQ yields a 3×3 identity matrix (the columns are orthonormal).

The 4×4 rotation matrix
 * $$ Q = \begin{bmatrix} -1 & 0 & 0 & 0 \\ 0 & -1 & 0 & 0 \\ 0 & 0 & -1 & 0 \\ 0 & 0 & 0 & -1 \end{bmatrix} $$

has no axis of rotation; it reverses the direction of every vector.

The 5×5 rotation matrix
 * $$ Q = \begin{bmatrix} 0 & -1 & 0 & 0 & 0 \\ 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & -1 & 0 & 0 \\ 0 & 0 & 0 & -1 & 0 \\ 0 & 0 & 0 & 0 & 1 \end{bmatrix} $$

rotates vectors in the plane of the first two coordinate axes 90°, rotates vectors in the plane of the next two axes 180°, and leaves the last coordinate axis unmoved. </ul>

Rotation about an arbitrary vector
Rotation about an arbitrary vector $$(l_x, l_y, l_x)$$ normalised so that $$l_x^2+l_y^2+l_z^2=1$$ by an angle $$\alpha$$ is given by a matrix
 * $$R = \begin{bmatrix}

l_x^2+(1-x^2)c  & l_xl_y(1-c)-l_zs & l_xl_z(1-c)+l_ys \\ l_xl_y(1-c)+l_zs & l_y^2+(1-l_y^2)c & l_yl_z(1-c)-l_xs \\ l_xl_z(1-c)-l_ys & l_yl_z(1-c)+l_xs & l_z^2+(1-l_z^2)c \end{bmatrix}, $$ where
 * $$c = \cos\alpha,s = \sin\alpha$$.

This representation of rotation, different from that via the Euler angles, is quite convenient for some applications.

Geometry
In Euclidean geometry, a rotation is an example of an isometry, a transformation that moves points without changing the distances between them. Rotations are distinguished from other isometries by two additional properties: they leave (at least) one point fixed, and they leave "handedness" unchanged. By contrast, a translation moves every point, a reflection exchanges left- and right-handed ordering, and a glide reflection does both.

A rotation that does not leave "handedness" unchanged is called an Improper Rotation or a Rotoinversion

If we take the fixed point as the origin of a Cartesian coordinate system, then every point can be given coordinates as a displacement from the origin. Thus we may work with the vector space of displacements instead of the points themselves. Now suppose (p1,…,pn) are the coordinates of the vector p from the origin, O, to point P. Choose an orthonormal basis for our coordinates; then the squared distance to P, by Pythagoras, is
 * $$ d^2(O,P) = \| \bold{p} \|^2 = \sum_{r=1}^n p_r^2 $$

which we can compute using the matrix multiplication
 * $$ \| \bold{p} \|^2 = \begin{bmatrix}p_1 \cdots p_n\end{bmatrix} \begin{bmatrix}p_1 \\ \vdots \\ p_n \end{bmatrix} = \bold{p}^T \bold{p} . $$

A geometric rotation transforms lines to lines, and preserves ratios of distances between points. From these properties we can show that a rotation is a linear transformation of the vectors, and thus can be written in matrix form, Qp. The fact that a rotation preserves, not just ratios, but distances themselves, we can state as
 * $$ \bold{p}^T \bold{p} = (Q \bold{p})^T (Q \bold{p}), \,\!$$

or
 * $$\begin{align}

\bold{p}^T I \bold{p}&{}= (\bold{p}^T Q^T) (Q \bold{p}) \\ &{}= \bold{p}^T (Q^T Q) \bold{p}. \end{align}$$ Because this equation holds for all vectors, p, we conclude that every rotation matrix, Q, satisfies the orthogonality condition,
 * $$ Q^T Q = I . \,\!$$

Rotations preserve handedness because they cannot change the ordering of the axes, which implies the special matrix condition,
 * $$ \det Q = +1 . \,\!$$

Equally important, we can show that any matrix satisfying these two conditions acts as a rotation.

Multiplication
The inverse of a rotation matrix is its transpose, which is also a rotation matrix:
 * $$\begin{align} (Q^T)^T (Q^T) &{}= Q Q^T = I\\ \det Q^T &{}= \det Q = +1 \end{align}$$

The product of two rotation matrices is a rotation matrix:
 * $$\begin{align}

(Q_1 Q_2)^T (Q_1 Q_2) &{}= Q_2^T (Q_1^T Q_1) Q_2 = I \\ \det (Q_1 Q_2) &{}= (\det Q_1) (\det Q_2) = +1 \end{align}$$ For n greater than 2, multiplication of n×n rotation matrices is not commutative.
 * $$\begin{align}

Q_1 &{}= \begin{bmatrix}0 & -1 & 0 \\ 1 & 0 & 0 \\ 0 & 0 & 1\end{bmatrix} & Q_2 &{}= \begin{bmatrix}0 & 0 & 1 \\ 0 & 1 & 0 \\ -1 & 0 & 0\end{bmatrix} \\ Q_1 Q_2 &{}= \begin{bmatrix}0 & -1 & 0 \\ 0 & 0 & 1 \\ -1 & 0 & 0\end{bmatrix} & Q_2 Q_1 &{}= \begin{bmatrix}0 & 0 & 1 \\ 1 & 0 & 0 \\ 0 & 1 & 0\end{bmatrix} \end{align}$$ Noting that any identity matrix is a rotation matrix, and that matrix multiplication is associative, we may summarize all these properties by saying that the n×n rotation matrices form a group, which for n &gt; 2 is non-abelian. Called a special orthogonal group, and denoted by SO(n), SO(n,R), SOn, or SOn(R), the group of n×n rotation matrices is isomorphic to the group of rotations in an n-dimensional space. This means that multiplication of rotation matrices corresponds to composition of rotations.

Ambiguities
The interpretation of a rotation matrix can be subject to many ambiguities.


 * Alias or alibi transformation
 * The change in a vector's coordinates can indicate a turn of the coordinate system (alias) or a turn of the vector (alibi).


 * Right- or left-handed coordinates
 * The matrix can be with respect to a right-handed or left-handed coordinate system.


 * World or body axes
 * The coordinate axes can be fixed or rotate with a body.


 * Vectors or forms
 * The vector space has a dual space of linear forms, and the matrix can act on either vectors or forms.

In most cases the effect of the ambiguity is to transpose or invert the matrix.

Independent planes
Consider the 3×3 rotation matrix
 * $$ Q = \begin{bmatrix} 0.36 & 0.48 & -0.8 \\ -0.8 & 0.60 & 0 \\ 0.48 & 0.64 & 0.60 \end{bmatrix} . $$

If Q acts in a certain direction, v, purely as a scaling by a factor &lambda;, then we have
 * $$ Q \bold{v} = \lambda \bold{v}, \,\!$$

so that
 * $$ \bold{0} = (\lambda I - Q) \bold{v} . \,\!$$

Thus &lambda; is a root of the characteristic polynomial for Q,
 * $$\begin{align}

0 &{}= \det (\lambda I - Q) \\ &{}= \lambda^3 - \tfrac{39}{25} \lambda^2 + \tfrac{39}{25} \lambda - 1 \\ &{}= (\lambda-1) (\lambda^2 - \tfrac{14}{25} \lambda + 1) \end{align}$$ Two features are noteworthy. First, one of the roots (or eigenvalues) is 1, which tells us that some direction is unaffected by the matrix. For rotations in three dimensions, this is the axis of the rotation (a concept that has no meaning in any other dimension). Second, the other two roots are a pair of complex conjugates, whose product is 1 (the constant term of the quadratic), and whose sum is 2 cos &theta; (the negated linear term). This factorization is of interest for 3×3 rotation matrices because the same thing occurs for all of them. (As special cases, for a null rotation the "complex conjugates" are both 1, and for a 180° rotation they are both −1.) Furthermore, a similar factorization holds for any n×n rotation matrix. If the dimension, n, is odd, there will be a "dangling" eigenvalue of 1; and for any dimension the rest of the polynomial factors into quadratic terms like the one here (with the two special cases noted). We are guaranteed that the characteristic polynomial will have degree n and thus n eigenvalues. And since a rotation matrix commutes with its transpose, it is a normal matrix, so can be diagonalized. We conclude that every rotation matrix, when expressed in a suitable coordinate system, partitions into independent rotations of two-dimensional subspaces, at most n&frasl;2 of them.

The sum of the entries on the main diagonal of a matrix is called the trace; it does not change if we reorient the coordinate system, and always equals the sum of the eigenvalues. This has the convenient implication for 2×2 and 3×3 rotation matrices that the trace reveals the angle of rotation, &theta;, in the two-dimensional (sub-)space. For a 2×2 matrix the trace is 2 cos(&theta;), and for a 3×3 matrix it is 1+2 cos(&theta;). In the three-dimensional case, the subspace consists of all vectors perpendicular to the rotation axis (the invariant direction, with eigenvalue 1). Thus we can extract from any 3×3 rotation matrix a rotation axis and an angle, and these completely determine the rotation.

Sequential angles
The constraints on a 2×2 rotation matrix imply that it must have the form
 * $$Q = \begin{bmatrix} a & -b \\ b & a \end{bmatrix}$$

with a2+b2 = 1. Therefore we may set a = cos &theta; and b = sin &theta;, for some angle &theta;. To solve for &theta; it is not enough to look at a alone or b alone; we must consider both together to place the angle in the correct quadrant, using a two-argument arctangent function.

Now consider the first column of a 3×3 rotation matrix,
 * $$\begin{bmatrix}a\\b\\c\end{bmatrix} . $$

Although a2+b2 will probably not equal 1, but some value r2 &lt; 1, we can use a slight variation of the previous computation to find a so-called Givens rotation that transforms the column to
 * $$\begin{bmatrix}r\\0\\c\end{bmatrix}, $$

zeroing b. This acts on the subspace spanned by the x and y axes. We can then repeat the process for the xz subspace to zero c. Acting on the full matrix, these two rotations produce the schematic form
 * $$Q_{xz}Q_{xy}Q = \begin{bmatrix}1&0&0\\0&\ast&\ast\\0&\ast&\ast\end{bmatrix} . $$

Shifting attention to the second column, a Givens rotation of the yz subspace can now zero the z value. This brings the full matrix to the form
 * $$Q_{yz}Q_{xz}Q_{xy}Q = \begin{bmatrix}1&0&0\\0&1&0\\0&0&1\end{bmatrix}, $$

which is an identity matrix. Thus we have decomposed Q as
 * $$Q = Q_{xy}^{-1}Q_{xz}^{-1}Q_{yz}^{-1} . $$

An n×n rotation matrix will have (n−1)+(n−2)+⋯+2+1, or
 * $$\sum_{k=1}^{n-1} k = \frac{n(n-1)}{2} \,\!$$

entries below the diagonal to zero. We can zero them by extending the same idea of stepping through the columns with a series of rotations in a fixed sequence of planes. We conclude that the set of n×n rotation matrices, each of which has n2 entries, can be parameterized by n(n−1)/2 angles.

In three dimensions this restates in matrix form an observation made by Euler, so mathematicians call the ordered sequence of three angles Euler angles. However, the situation is somewhat more complicated than we have so far indicated. Despite the small dimension, we actually have considerable freedom in the sequence of axis pairs we use; and we also have some freedom in the choice of angles. Thus we find many different conventions employed when three-dimensional rotations are parameterized for physics, or medicine, or chemistry, or other disciplines. When we include the option of world axes or body axes, 24 different sequences are possible. And while some disciplines call any sequence Euler angles, others give different names (Euler, Cardano, Tait-Byan, roll-pitch-yaw) to different sequences.

One reason for the large number of options is that, as noted previously, rotations in three dimensions (and higher) do not commute. If we reverse a given sequence of rotations, we get a different outcome. This also implies that we cannot compose two rotations by adding their corresponding angles. Thus Euler angles are not vectors, despite a similarity in appearance as a triple of numbers.

Nested dimensions
A 3×3 rotation matrix like
 * $$Q_{3 \times 3} = \begin{bmatrix}\cos \theta & -\sin \theta & {\color{CadetBlue}0} \\ \sin \theta & \cos \theta & {\color{CadetBlue}0} \\ {\color{CadetBlue}0} & {\color{CadetBlue}0} & {\color{CadetBlue}1}\end{bmatrix} $$

suggests a 2×2 rotation matrix,
 * $$Q_{2 \times 2} = \begin{bmatrix}\cos \theta & -\sin \theta \\ \sin \theta & \cos \theta\end{bmatrix}, $$

is embedded in the upper left corner:
 * $$Q_{3 \times 3} = \left[ \begin{matrix} Q_{2 \times 2} & \bold{0} \\ \bold{0}^T & 1 \end{matrix} \right] . $$

This is no illusion; not just one, but many, copies of n-dimensional rotations are found within (n+1)-dimensional rotations, as subgroups. Each embedding leaves one direction fixed, which in the case of 3×3 matrices is the rotation axis. For example, we have
 * $$Q_{\bold{x}}(\theta) = \begin{bmatrix}1 & 0 & 0 \\ 0 & \cos \theta & -\sin \theta \\ 0 & \sin \theta & \cos \theta\end{bmatrix}, $$
 * $$Q_{\bold{y}}(\theta) = \begin{bmatrix}\cos \theta & 0 & \sin \theta \\ 0 & 1 & 0 \\ -\sin \theta & 0 & \cos \theta\end{bmatrix}, $$
 * $$Q_{\bold{z}}(\theta) = \begin{bmatrix}\cos \theta & -\sin \theta & 0 \\ \sin \theta & \cos \theta & 0 \\ 0 & 0 & 1\end{bmatrix}, $$

fixing the x axis, the y axis, and the z axis, respectively. The rotation axis need not be a coordinate axis; if u = (x,y,z) is a unit vector in the desired direction, then
 * $$\begin{align}

Q_{\bold{u}}(\theta) &{}=  \begin{bmatrix} 0&-z&y\\ z&0&-x\\ -y&x&0 \end{bmatrix} \sin \theta + (I - \bold{u}\bold{u}^T) \cos \theta + \bold{u}\bold{u}^T \\ &{}= \begin{bmatrix} (1-x^2) c_{\theta} + x^2 & - z s_{\theta} - x y c_{\theta} + x y & y s_{\theta} - x z c_{\theta} + x z \\ z s_{\theta} - x y c_{\theta} + x y & (1-y^2) c_{\theta} + y^2 & -x s_{\theta} - y z c_{\theta} + y z \\ -y s_{\theta} - x z c_{\theta} + x z & x s_{\theta} - y z c_{\theta} + y z & (1-z^2) c_{\theta} + z^2 \end{bmatrix} \\ &{}= \begin{bmatrix} x^2 (1-c_{\theta}) + c_{\theta} & x y (1-c_{\theta}) - z s_{\theta} & x z (1-c_{\theta}) + y s_{\theta} \\ x y (1-c_{\theta}) + z s_{\theta} & y^2 (1-c_{\theta}) + c_{\theta} & y z (1-c_{\theta}) - x s_{\theta} \\ x z (1-c_{\theta}) - y s_{\theta} & y z (1-c_{\theta}) + x s_{\theta} & z^2 (1-c_{\theta}) + c_{\theta} \end{bmatrix} , \end{align}$$ where c&theta; = cos &theta;, s&theta; = sin &theta;, is a rotation by angle &theta; leaving axis u fixed.

A direction in (n+1)-dimensional space will be a unit magnitude vector, which we may consider a point on a generalized sphere, Sn. Thus it is natural to describe the rotation group SO(n+1) as combining SO(n) and Sn. A suitable formalism is the fiber bundle,
 * $$ SO(n) \hookrightarrow SO(n+1) \to S^n, \,\!$$

where for every direction in the "base space", Sn, the "fiber" over it in the "total space", SO(n+1), is a copy of the "fiber space", SO(n), namely the rotations that keep that direction fixed.

Thus we can build an n×n rotation matrix by starting with a 2×2 matrix, aiming its fixed axis on S2 (the ordinary sphere in three-dimensional space), aiming the resulting rotation on S3, and so on up through Sn−1. A point on Sn can be selected using n numbers, so we again have n(n−1)/2 numbers to describe any n×n rotation matrix.

In fact, we can view the sequential angle decomposition, discussed previously, as reversing this process. The composition of n−1 Givens rotations brings the first column (and row) to (1,0,…,0), so that the remainder of the matrix is a rotation matrix of dimension one less, embedded so as to leave (1,0,…,0) fixed.

Skew parameters
When an n×n rotation matrix, Q, does not include −1 as an eigenvalue, so that none of the planar rotations of which it is composed are 180° rotations, then Q+I is an invertible matrix. Most rotation matrices fit this description, and for them we can show that (Q−I)(Q+I)−1 is a skew-symmetric matrix, A. Thus AT = −A; and since the diagonal is necessarily zero, and since the upper triangle determines the lower one, A contains n(n−1)/2 independent numbers. Conveniently, I−A is invertible whenever A is skew-symmetric; thus we can recover the original matrix using the Cayley transform,
 * $$ A \mapsto (I+A)(I-A)^{-1}, \,\!$$

which maps any skew-symmetric matrix A to a rotation matrix. In fact, aside from the noted exceptions, we can produce any rotation matrix in this way. Although in practical applications we can hardly afford to ignore 180° rotations, the Cayley transform is still a potentially useful tool, giving a parameterization of most rotation matrices without trigonometric functions.

In three dimensions, for example, we have
 * $$\begin{align}

&\begin{bmatrix}0&-z&y\\z&0&-x\\-y&x&0\end{bmatrix} \mapsto {} \\ &\quad \frac{1}{1+x^2+y^2+z^2} \begin{bmatrix} 1+x^2-y^2-z^2 & 2 x y-2 z & 2 y+2 x z \\ 2 x y+2 z & 1-x^2+y^2-z^2 & 2 y z-2 x \\ 2 x z-2 y & 2 x+2 y z & 1-x^2-y^2+z^2 \end{bmatrix}. \end{align}$$ If we condense the skew entries into a vector, (x,y,z), then we produce a 90° rotation around the x axis for (1,0,0), around the y axis for (0,1,0), and around the z axis for (0,0,1). The 180° rotations are just out of reach; for, in the limit as x goes to infinity, (x,0,0) does approach a 180° rotation around the x axis, and similarly for other directions.

Lie group
We have established that n×n rotation matrices form a group, the special orthogonal group, SO(n). This algebraic structure is coupled with a topological structure, in that the operations of multiplication and taking the inverse (which here is merely transposition) are continuous functions of the matrix entries. Thus SO(n) is a classic example of a topological group. (In purely topological terms, it is a compact manifold.) Furthermore, the operations are not only continuous, but smooth, so SO(n) is a differentiable manifold and a Lie group.

Most properties of rotation matrices depend very little on the dimension, n; yet in Lie group theory we see systematic differences between even dimensions and odd dimensions. As well, there are some irregularities below n = 5; for example, SO(4) is, anomalously, not a simple Lie group, but instead isomorphic to the product of S3 and SO(3).

Lie algebra
Associated with every Lie group is a Lie algebra, a linear space equipped with a bilinear alternating product called a bracket. The algebra for SO(n) is denoted by
 * $$ \mathfrak{so}(n), \,\!$$

and consists of all skew-symmetric n×n matrices (as implied by differentiating the orthogonality condition, I = QTQ). The bracket, [A1,A2], of two skew-symmetric matrices is defined to be A1A2−A2A1, which is again a skew-symmetric matrix. This Lie algebra bracket captures the essence of the Lie group product via infinitesimals.

For 2×2 rotation matrices, the Lie algebra is a one-dimensional vector space, multiples of
 * $$J = \begin{bmatrix}0&-1\\1&0\end{bmatrix} . $$

Here the bracket always vanishes, which tells us that, in two dimensions, rotations commute. Not so in any higher dimension. For 3×3 rotation matrices, we have a three-dimensional vector space with the convenient basis (generators)

A_{\bold{x}} = \begin{bmatrix}0&0&0\\0&0&-1\\0&1&0\end{bmatrix}, \quad A_{\bold{y}} = \begin{bmatrix}0&0&1\\0&0&0\\-1&0&0\end{bmatrix}, \quad A_{\bold{z}} = \begin{bmatrix}0&-1&0\\1&0&0\\0&0&0\end{bmatrix}. $$ The essence of the bracket for these basis vectors works out to be as follows.

A_{\bold{x}} A_{\bold{y}} = A_{\bold{z}}, \quad A_{\bold{z}} A_{\bold{x}} = A_{\bold{y}}, \quad A_{\bold{y}} A_{\bold{z}} = A_{\bold{x}}. $$

We can conveniently identify any matrix in this Lie algebra with a vector in R3,
 * $$\begin{align}

\boldsymbol{\omega} &{}= (x,y,z) \\ \tilde{\boldsymbol{\omega}} &{}= x A_{\bold{x}} + y A_{\bold{y}} + z A_{\bold{z}} \\ &{}= \begin{bmatrix}0&-z&y\\z&0&-x\\-y&x&0\end{bmatrix}. \end{align}$$ Under this identification, the so(3) bracket has a memorable description; it is the vector cross product,
 * $$ [\tilde{\bold{u}},\tilde{\bold{v}}] = (\bold{u} \times \bold{v})^{\sim} . \,\!$$

The matrix identified with a vector v is also memorable, because
 * $$ \tilde{\bold{v}} \bold{u} = \bold{v} \times \bold{u} . \,\!$$

Notice this implies that v is in the null space of the skew-symmetric matrix with which it is identified, because v×v is always the zero vector.

Exponential map
Connecting the Lie algebra to the Lie group is the exponential map, which we define using the familiar power series for ex ,
 * $$\begin{align}

\exp \colon \mathfrak{so}(n) &{}\to SO(n) \\ A &{}\mapsto I + A + \tfrac{1}{2} A^2 + \tfrac{1}{6} A^3 + \cdots + \tfrac{1}{k!} A^k + \cdots \\ &{}= \sum_{k=0}^{\infty} \frac{1}{k!} A^k \end{align}$$ For any skew-symmetric A, exp(A) is always a rotation matrix.

An important practical example is the 3×3 case, where we have seen we can identify every skew-symmetric matrix with a vector &omega; = u&theta;, where u = (x,y,z) is a unit magnitude vector. Recall that u is in the null space of the matrix associated with &omega;, so that if we use a basis with u as the z axis the final column and row will be zero. Thus we know in advance that the exponential matrix must leave u fixed. It is mathematically impossible to supply a straightforward formula for such a basis as a function of u (its existence would violate the hairy ball theorem), but direct exponentiation is possible, and yields
 * $$\begin{align}

\exp( \tilde{\boldsymbol{\omega}} ) &{}= \exp \left( \begin{bmatrix} 0 & -z \theta & y \theta \\ z \theta & 0&-x \theta \\ -y \theta & x \theta & 0 \end{bmatrix} \right) \\ &{}= \begin{bmatrix} 2 (x^2 - 1) s^2 + 1 & 2 x y s^2 - 2 z c s & 2 x z s^2 + 2 y c s \\ 2 x y s^2 + 2 z c s & 2 (y^2 - 1) s^2 + 1 & 2 y z s^2 - 2 x c s \\ 2 x z s^2 - 2 y c s & 2 y z s^2 + 2 x c s & 2 (z^2 - 1) s^2 + 1 \end{bmatrix} , \end{align}$$ where c = cos &theta;&frasl;2, s = sin &theta;&frasl;2. We recognize this as our matrix for a rotation around axis u by angle &theta;. We also note that this mapping of skew-symmetric matrices is quite different from the Cayley transform discussed earlier.

In any dimension, if we choose some nonzero A and consider all its scalar multiples, exponentiation yields rotation matrices along a geodesic of the group manifold, forming a one-parameter subgroup of the Lie group. More broadly, the exponential map provides a homeomorphism between a neighborhood of the origin in the Lie algebra and a neighborhood of the identity in the Lie group. In fact, we can produce any rotation matrix as the exponential of some skew-symmetric matrix, so for these groups the exponential map is a surjection.

Baker–Campbell–Hausdorff formula
Suppose we are given A and B in the Lie algebra. Their exponentials, exp(A) and exp(B), are rotation matrices, which we can multiply. Since the exponential map is a surjection, we know that for some C in the Lie algebra, exp(A)exp(B) = exp(C), and we write
 * $$ A \ast B = C . \,\!$$

When exp(A) and exp(B) commute (which always happens for 2×2 matrices, but not higher), then C = A+B, mimicking the behavior of complex exponentiation. The general case is given by the BCH formula, a series expanded in terms of the bracket. For matrices, the bracket is the same operation as the commutator, which detects lack of commutativity in multiplication. The general formula begins as follows.
 * $$ A \ast B = A + B + \tfrac12 [A,B] + \tfrac{1}{12} [A,[A,B]] - \tfrac{1}{12} [B,[A,B]] - \cdots \,\!$$

Representation of a rotation matrix as a sequential angle decomposition, as in Euler angles, may tempt us to treat rotations as a vector space, but the higher order terms in the BCH formula reveal that to be a mistake.

We again take special interest in the 3×3 case, where [A,B] equals the cross product, A×B. If A and B are linearly independent, then A, B, and A×B can be used as a basis; if not, then A and B commute. And conveniently, in this dimension the summation in the BCH formula has a closed form as &alpha;A+&beta;B+&gamma;(A×B).

Spin group
The Lie group of n×n rotation matrices, SO(n), is a compact and path-connected manifold, and thus locally compact and connected. However, it is not simply connected, so Lie theory tells us it is a kind of "shadow" (a homomorphic image) of a universal covering group. Often the covering group, which in this case is the spin group denoted by Spin(n), is simpler and more natural to work with.

In the case of planar rotations, SO(2) is topologically a circle, S1. Its universal covering group, Spin(2), is isomorphic to the real line, R, under addition. In other words, whenever we use angles of arbitrary magnitude, which we often do, we are essentially taking advantage of the convenience of the "mother space". Every 2×2 rotation matrix is produced by a countable infinity of angles, separated by integer multiples of 2&pi;. Correspondingly, the fundamental group of SO(2) is isomorphic to the integers, Z.

In the case of spatial rotations, SO(3) is topologically equivalent to three-dimensional real projective space, RP3. Its universal covering group, Spin(3), is isomorphic to the 3-sphere, S3. Every 3×3 rotation matrix is produced by two opposite points on the sphere. Correspondingly, the fundamental group of SO(2) is isomorphic to the two-element group, Z2. We can also describe Spin(3) as isomorphic to quaternions of unit norm under multiplication, or to certain 4×4 real matrices, or to 2×2 complex special unitary matrices.

Concretely, a unit quaternion, q, with
 * $$\begin{align}

q &{}= w + \bold{i}x + \bold{j}y + \bold{k}z, \\ 1 &{}= w^2 + x^2 + y^2 + z^2 , \end{align}$$ produces the rotation matrix
 * $$ Q = \begin{bmatrix}

2 x^2 + 2 w^2 - 1 & 2 x y - 2 z w & 2 x z + 2 y w \\ 2 x y + 2 z w & 2 y^2 + 2 w^2 - 1 & 2 y z - 2 x w \\ 2 x z - 2 y w & 2 y z + 2 x w & 2 z^2 + 2 w^2 - 1 \end{bmatrix}. $$ This is our third version of this matrix, here as a rotation around non-unit axis vector (x,y,z) by angle 2&theta;, where cos &theta; = w and |sin &theta;| = ||(x,y,z)||. (The proper sign for sin &theta; is implied once the signs of the axis components are decided.)

Many features of this case are the same for higher dimensions. The coverings are all two-to-one, with SO(n), n &gt; 2, having fundamental group Z2. The natural setting for these groups is within a Clifford algebra. And the action of the rotations is produced by a kind of "sandwich", denoted by qvq&lowast;.

Infinitesimal rotations
The matrices in the Lie algebra are not themselves rotations; the skew-symmetric matrices are derivatives, proportional differences of rotations. An actual "differential rotation", or infinitesimal rotation matrix has the form
 * $$ I + A \, d\theta, \,\!$$

where d&theta; is vanishingly small. These matrices do not satisfy all the same properties as ordinary finite rotation matrices under the usual treatment of infinitesimals. To understand what this means, consider
 * $$ dA_{\bold{x}} = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & -d\theta \\ 0 & d\theta & 1 \end{bmatrix} . $$

We first test the orthogonality condition, QTQ = I. The product is
 * $$ dA_{\bold{x}}^T \, dA_{\bold{x}} = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1+d\theta^2 & 0 \\ 0 & 0 & 1+d\theta^2 \end{bmatrix}, $$

differing from an identity matrix by second order infinitesimals, which we discard. So to first order, an infinitesimal rotation matrix is an orthogonal matrix. Next we examine the square of the matrix.
 * $$ dA_{\bold{x}}^2 = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1-d\theta^2 & -2d\theta \\ 0 & 2d\theta & 1-d\theta^2 \end{bmatrix} $$

Again discarding second order effects, we see that the angle simply doubles. This hints at the most essential difference in behavior, which we can exhibit with the assistance of a second infinitesimal rotation,
 * $$ dA_{\bold{y}} = \begin{bmatrix} 1 & 0 & d\phi \\ 0 & 1 & 0 \\ -d\phi & 0 & 1 \end{bmatrix} . $$

Compare the products dAxdAy and dAydAx.
 * $$\begin{align}

dA_{\bold{x}}\,dA_{\bold{y}} &{}= \begin{bmatrix} 1 & 0 & d\phi \\ d\theta\,d\phi & 1 & -d\theta \\ -d\phi & d\theta & 1 \end{bmatrix} \\ dA_{\bold{y}}\,dA_{\bold{x}} &{}= \begin{bmatrix} 1 & d\theta\,d\phi & d\phi \\ 0 & 1 & -d\theta \\ -d\phi & d\theta & 1 \end{bmatrix} \\ \end{align}$$ Since d&theta; d&phi; is second order, we discard it; thus, to first order, multiplication of infinitesimal rotation matrices is commutative. In fact,
 * $$ dA_{\bold{x}}\,dA_{\bold{y}} = dA_{\bold{x}} + dA_{\bold{y}}, \,\!$$

again to first order.

But we must always be careful to distinguish (the first order treatment of) these infinitesimal rotation matrices from both finite rotation matrices and from derivatives of rotation matrices (namely skew-symmetric matrices). Contrast the behavior of finite rotation matrices in the BCH formula with that of infinitesimal rotation matrices, where all the commutator terms will be second order infinitesimals so we do have a vector space.

Conversions
We have seen the existence of several decompositions that apply in any dimension, namely independent planes, sequential angles, and nested dimensions. In all these cases we can either decompose a matrix or construct one. We have also given special attention to 3×3 rotation matrices, and these warrant further attention, in both directions.

Quaternion
Rewrite the 3×3 rotation matrix again, as
 * $$ Q = \begin{bmatrix}

1 - 2 y^2 - 2 z^2 & 2 x y - 2 z w & 2 x z + 2 y w \\ 2 x y + 2 z w & 1 - 2 x^2 - 2 z^2 & 2 y z - 2 x w \\ 2 x z - 2 y w & 2 y z + 2 x w & 1 - 2 x^2 - 2 y^2 \end{bmatrix}. $$ Now every quaternion component appears multiplied by two in a term of degree two, and if all such terms are zero what's left is an identity matrix. This leads to an efficient, robust conversion from any quaternion — whether unit, nonunit, or even zero — to a 3×3 rotation matrix.

Nq = w^2 + x^2 + y^2 + z^2 if Nq > 0.0 then s = 2/Nq else s = 0.0 X = x*s; Y = y*s; Z = z*s wX = w*X; wY = w*Y; wZ = w*Z xX = x*X; xY = x*Y; xZ = x*Z yY = y*Y; yZ = y*Z; zZ = z*Z [ 1.0-(yY+zZ)  xY-wZ      xZ+wY    ] [   xY+wZ   1.0-(xX+zZ)   yZ-wX    ] [   xZ-wY      yZ+wX   1.0-(xX+yY) ]

Freed from the demand for a unit quaternion, we find that nonzero quaternions act as homogeneous coordinates for 3×3 rotation matrices. The Cayley transform, discussed earlier, is obtained by scaling the quaternion so that its w component is 1. For a 180° rotation around any axis, w will be zero, which explains the Cayley limitation.

The sum of the entries along the main diagonal (the trace), plus one, equals 4−4(x2+y2+z2), which is 4w2. Thus we can write the trace itself as 2w2+2w2−1; and from the previous version of the matrix we see that the diagonal entries themselves have the same form: 2x2+2w2−1, 2y2+2w2−1, and 2z2+2w2−1. So we can easily compare the magnitudes of all four quaternion components using the matrix diagonal. We can, in fact, obtain all four magnitudes using sums and square roots, and choose consistent signs using the skew-symmetric part of the off-diagonal entries. w = 0.5*sqrt(1+Qxx+Qyy+Qzz) x = copysign(0.5*sqrt(1+Qxx-Qyy-Qzz),Qzy-Qyz) y = copysign(0.5*sqrt(1-Qxx+Qyy-Qzz),Qxz-Qzx) z = copysign(0.5*sqrt(1-Qxx-Qyy+Qzz),Qyx-Qxy) Alternatively, use a single square root and division t = Qxx+Qyy+Qzz r = sqrt(1+t) s = 0.5/r w = 0.5*r x = (Qzy-Qyz)*s y = (Qxz-Qzx)*s z = (Qyx-Qxy)*s This is numerically stable so long as the trace, <tt>t</tt>, is not negative; otherwise, we risk dividing by (nearly) zero. In that case, suppose <tt>Qxx</tt> is the largest diagonal entry, so x will have the largest magnitude (the other cases are similar); then the following is safe. r = sqrt(1+Qxx-Qyy-Qzz) s = 0.5/r w = (Qzy-Qyz)*s x = 0.5*r y = (Qxy+Qyx)*s z = (Qzx+Qxz)*s If the matrix contains significant error, such as accumulated numerical error, we may construct a symmetric 4×4 matrix,
 * $$ K = \frac13

\begin{bmatrix} Q_{xx}-Q_{yy}-Q_{zz} & Q_{yx}+Q_{xy} & Q_{zx}+Q_{xz} & Q_{yz}-Q_{zy} \\ Q_{yx}+Q_{xy} & Q_{yy}-Q_{xx}-Q_{zz} & Q_{zy}+Q_{yz} & Q_{zx}-Q_{xz} \\ Q_{zx}+Q_{xz} & Q_{zy}+Q_{yz} & Q_{zz}-Q_{xx}-Q_{yy} & Q_{xy}-Q_{yx} \\ Q_{yz}-Q_{zy} & Q_{zx}-Q_{xz} & Q_{xy}-Q_{yx} & Q_{xx}+Q_{yy}+Q_{zz} \end{bmatrix} , $$ and find the eigenvector, (x,y,z,w), of its largest magnitude eigenvalue. (If Q is truly a rotation matrix, that value will be 1.) The quaternion so obtained will correspond to the rotation matrix closest to the given matrix.

Polar decomposition
If the n×n matrix M is non-singular, its columns are linearly independent vectors; thus the Gram–Schmidt process can adjust them to be an orthonormal basis. Stated in terms of numerical linear algebra, we convert M to an orthogonal matrix, Q, using QR decomposition. However, we often prefer a Q "closest" to M, which this method does not accomplish. For that, the tool we want is the polar decomposition.

To measure closeness, we may use any matrix norm invariant under orthogonal transformations. A convenient choice is the Frobenius norm, ||Q−M||F, squared, which is the sum of the squares of the element differences. Writing this in terms of the trace, Tr, our goal is, Though written in matrix terms, the objective function is just a quadratic polynomial. We can minimize it in the usual way, by finding where its derivative is zero. For a 3×3 matrix, the orthogonality constraint implies six scalar equalities that the entries of Q must satisfy. To incorporate the constraint(s), we may employ a standard technique, Lagrange multipliers, assembled as a symmetric matrix, Y. Thus our method is: Consider a 2×2 example. Including constraints, we seek to minimize
 * Find Q minimizing Tr( (Q−M)T(Q−M) ), subject to QTQ = I.
 * Differentiate Tr( (Q−M)T(Q−M) + (QTQ−I)Y ) with respect to (the entries of) Q, and equate to zero.
 * $$\begin{align}

&\scriptstyle{ (Q_{xx}-M_{xx})^2 + (Q_{xy}-M_{xy})^2 } \\ &\scriptstyle{ {} + (Q_{yx}-M_{yx})^2 + (Q_{yy}-M_{yy})^2 } \\ &\scriptstyle{ {} + (Q_{xx}^2+Q_{yx}^2-1)Y_{xx} + (Q_{xy}^2+Q_{yy}^2-1)Y_{yy} } \\ &\scriptstyle{ {} + 2(Q_{xx} Q_{xy} + Q_{yx} Q_{yy})Y_{xy}. } \end{align}$$ Taking the derivative with respect to Qxx, Qxy, Qyx, Qyy in turn, we assemble a matrix.
 * $$\scriptstyle{ 2

\begin{bmatrix} \scriptstyle{ Q_{xx}-M_{xx} + Q_{xx} Y_{xx} + Q_{xy} Y_{xy} } & \scriptstyle{ Q_{xy}-M_{xy} + Q_{xx} Y_{xy} + Q_{xy} Y_{yy} } \\ \scriptstyle{ Q_{yx}-M_{yx} + Q_{yx} Y_{xx} + Q_{yy} Y_{xy} } & \scriptstyle{ Q_{yy}-M_{yy} + Q_{yx} Y_{xy} + Q_{yy} Y_{yy} } \end{bmatrix}}$$ In general, we obtain the equation
 * $$ 0 = 2(Q-M) + 2QY, \,\!$$

so that
 * $$ M = Q(I+Y) = QS, \,\!$$

where Q is orthogonal and S is symmetric. To ensure a minimum, the Y matrix (and hence S) must be positive definite. Linear algebra calls QS the polar decomposition of M, with S the positive square root of S2 = MTM.
 * $$ S^2 = (Q^T M)^T (Q^T M) = M^T Q Q^ T M = M^T M \,\!$$

When M is non-singular, the Q and S factors of the polar decomposition are uniquely determined. However, the determinant of S is positive because S is positive definite, so Q inherits the sign of the determinant of M. That is, Q is only guaranteed to be orthogonal, not a rotation matrix. This is unavoidable; an M with negative determinant has no uniquely-defined closest rotation matrix.

Axis and angle
To efficiently construct a rotation matrix from an angle &theta; and a unit axis u, we can take advantage of symmetry and skew-symmetry within the entries. c = cos(&theta;); s = sin(&theta;); C = 1-c xs = x*s;  ys = y*s;   zs = z*s xC = x*C;  yC = y*C;   zC = z*C xyC = x*yC; yzC = y*zC; zxC = z*xC [ x*xC+c  xyC-zs   zxC+ys ] [ xyC+zs  y*yC+c   yzC-xs ] [ zxC-ys  yzC+xs   z*zC+c ] Determining an axis and angle, like determining a quaternion, is only possible up to sign; that is, (u,&theta;) and (−u,−&theta;) correspond to the same rotation matrix, just like q and −q. As well, axis-angle extraction presents additional difficulties. The angle can be restricted to be from 0° to 180°, but angles are formally ambiguous by multiples of 360°. When the angle is zero, the axis is undefined. When the angle is 180°, the matrix becomes symmetric, which has implications in extracting the axis. Near multiples of 180°, care is needed to avoid numerical problems: in extracting the angle, a two-argument arctangent with <tt>atan2(sin &theta;,cos &theta;)</tt> equal to &theta; avoids the insensitivity of arccosine; and in computing the axis magnitude to force unit magnitude, a brute-force approach can lose accuracy through underflow.

A partial approach is as follows. x = Qzy-Qyz y = Qxz-Qzx z = Qyx-Qxy r = hypot(x,hypot(y,z)) t = Qxx+Qyy+Qzz &theta; = atan2(r,t−1) The x, y, and z components of the axis would then be divided by <tt>r</tt>. A fully robust approach will use different code when <tt>t</tt> is negative, as with quaternion extraction. When <tt>r</tt> is zero because the angle is zero, an axis must be provided from some source other than the matrix.

Euler angles
Complexity of conversion escalates with Euler angles (used here in the broad sense). The first difficulty is to establish which of the twenty-four variations of Cartesian axis order we will use. Suppose the three angles are &theta;1, &theta;2, &theta;3; physics and chemistry may interpret these as


 * $$ Q(\theta_1,\theta_2,\theta_3)= Q_{\bold{z}}(\theta_1) Q_{\bold{y}}(\theta_2) Q_{\bold{z}}(\theta_3), \,\!$$

while aircraft dynamics may use
 * $$ Q(\theta_1,\theta_2,\theta_3)= Q_{\bold{z}}(\theta_3) Q_{\bold{y}}(\theta_2) Q_{\bold{x}}(\theta_1) . \,\!$$

One systematic approach begins with choosing the right-most axis. Among all permutations of (x,y,z), only two place that axis first; one is an even permutation and the other odd. Choosing parity thus establishes the middle axis. That leaves two choices for the left-most axis, either duplicating the first or not. These three choices gives us 3×2×2 = 12 variations; we double that to 24 by choosing static or rotating axes.

This is enough to construct a matrix from angles, but triples differing in many ways can give the same rotation matrix. For example, suppose we use the zyz convention above; then we have the following equivalent pairs:
 * {| style="text-align:right"

The problem of singular alignment, the mathematical analog of physical gimbal lock, occurs when the middle rotation aligns the axes of the first and last rotations. It afflicts every axis order at either even or odd multiples of 90°, causing Euler angles to be abandoned for quaternions in many applications. Setting these unavoidable issues aside, angles for any order can be found using a concise common routine.
 * (90°,||45°,||−105°) || &equiv; || (−270°,||−315°,||255°) || &emsp; multiples of 360°
 * (72°,||0°,||0°) || &equiv; || (40°,||0°,||32°) || &emsp; singular alignment
 * (45°,||60°,||−30°) || &equiv; || (−135°,||−60°,||150°) || &emsp; bistable flip
 * }
 * (45°,||60°,||−30°) || &equiv; || (−135°,||−60°,||150°) || &emsp; bistable flip
 * }

Uniform random rotation matrices
We sometimes need to generate a uniformly distributed random rotation matrix. It seems intuitively clear in two dimensions that this means the rotation angle is uniformly distributed between 0 and 2&pi;. That intuition is correct, but does not carry over to higher dimensions. For example, if we decompose 3×3 rotation matrices in axis-angle form, the angle should not be uniformly distributed; the probability that (the magnitude of) the angle is at most &theta; should be 1&frasl;&pi;(&theta; − sin &theta;), for 0 &le; &theta; &le; &pi;.

Since SO(n) is a connected and locally compact Lie group, we have a simple standard criterion for uniformity, namely that the distribution be unchanged when composed with any arbitrary rotation (a Lie group "translation"). This definition corresponds to what is called Haar measure. show how to use the Cayley transform to generate and test matrices according to this criterion.

We can also generate a uniform distribution in any dimension using the subgroup algorithm of. This recursively exploits the nested dimensions group structure of SO(n), as follows. Generate a uniform angle and construct a 2×2 rotation matrix. To step from n to n+1, generate a vector v uniformly distributed on the n-sphere, Sn, embed the n×n matrix in the next larger size with last column (0,…,0,1), and rotate the larger matrix so the last column becomes v.

As usual, we have special alternatives for the 3×3 case. Each of these methods begins with three independent random scalars uniformly distributed on the unit interval. takes advantage of the odd dimension to change a Householder reflection to a rotation by negation, and uses that to aim the axis of a uniform planar rotation.

Another method uses unit quaternions. Multiplication of rotation matrices is homomorphic to multiplication of quaternions, and multiplication by a unit quaternion rotates the unit sphere. Since the homomorphism is a local isometry, we immediately conclude that to produce a uniform distribution on SO(3) we may use a uniform distribution on S3.

Euler angles can also be used, though not with each angle uniformly distributed.

For the axis-angle form, the axis is uniformly distributed over the unit sphere of directions, S2, while the angle has the non-uniform distribution over [0,&pi;] noted previously.