A

rotation matrix is a

matrix which, when multiplied by the

vector representing a

point, has the effect of rotating that point around the

origin. A rotation around the origin is a

linear geometrical transformation.

In general, any orthogonal matrix with a determinant of 1 is a rotation matrix.

In two dimensions, the 2×2 matrix that rotates a 2D vector around the origin by an angle of θ is:

*R*(θ)
| cos(θ) -sin(θ) |
= | |
| sin(θ) cos(θ) |

(Note: the matrix should be multiplied from the right by a column vector.)

As with any other linear geometrical transformation, rotations may be composed by multiplying the matricies (see matrix multiplication) that represent them.

Rotations in 2D commute. That is, the rotation of a column vector *v* by θ_{1} and followed by the rotation by θ_{2} has the same effect as a single rotation by θ_{1}+θ_{2}. Of course, this seems obvious in 2D. In matrix form, this means that:

*R*(θ_{2})(*R*(θ_{1})**v**) = *R*(θ_{1}+θ_{2})**v**

The vectors forming the columns of the matrix are what the unit vectors along the axes are transformed to.

In three dimensions, rotation matricies are more complex. It's easiest to start with rotations around the three coordinate axes:

*R*_{x}(θ)
| 1 0 0 |
= | 0 cos(θ) -sin(θ) |
| 0 sin(θ) cos(θ) |

*R*_{y}(θ)
| cos(θ) 0 sin(θ) |
= | 0 1 0 |
| -sin(θ) 0 cos(θ) |

*R*_{z}(θ)
| cos(θ) -sin(θ) 0 |
= | sin(θ) cos(θ) 0 |
| 0 0 1 |

Here,

*R*_{x}(θ) represents a rotation of θ around the

**x** axis.

Rotation about an arbitrary axis **u** is given by the following formula:

*R*_{u}(θ)
= **u****u**^{T} + cos(θ) (*I* - **u****u**^{T}) + sin(θ) cross(**u**)

where

**u** = [

*u*_{x} *u*_{y} *u*_{z} ]

^{T} is a unit

column vector and cross(

**u**) is the

cross product operator matrix. In addition,

*I* is the 3×3

identity matrix, and the 3×3 matrix

**u****u**^{T} is a

projection matrix.

Expanding the formula into a matrix, we get

| *u*_{x}*u*_{x}*v*θ+*c*θ *u*_{x}*u*_{y}*v*θ-*u*_{z}*s*θ *u*_{x}*u*_{z}*v*θ+*u*_{y}*s*θ |
= | *u*_{y}*u*_{x}*v*θ+*u*_{z}*s*θ *u*_{y}*u*_{y}*v*θ+*c*θ *u*_{y}*u*_{z}*v*θ-*u*_{x}*s*θ |
| *u*_{z}*u*_{x}*v*θ-*u*_{y}*s*θ *u*_{z}*u*_{y}*v*θ+*u*_{x}*s*θ *u*_{z}*u*_{z}*v*θ+*c*θ |

where

*v*θ = 1-cos(θ),

*c*θ = cos(θ) and

*s*θ = sin(θ).

Given an rotation matrix, you can extract the axis *u* and angle θ of the rotation that it represents.
Suppose the rotation matrix *R* is given by

*R*
| r_{11} r_{12} r_{13} |
= | r_{21} r_{22} r_{23} |
| r_{31} r_{32} r_{33} |

Then we can calculate

cosθ = ( r_{11} + r_{22} + r_{33} - 1) / 2
sinθ = ( (r_{21}-r_{12})^{2} + (r_{13}-r_{31})^{2} + (r_{32}-r_{23})^{2} )^{1/2} / 2
*u*_{x} = (r_{21}-r_{12}) / (2sinθ)
*u*_{y} = (r_{13}-r_{31}) / (2sinθ)
*u*_{z} = (r_{32}-r_{23}) / (2sinθ)

Note that

*u* is

undefined if θ is a

multiple of 2π. That would mean that there actually is no rotation.

Rotations in 3D do not commute. For example, a 90° rotation around the **x** axis followed by a 90° rotation around the **z** axis does not have the same effect as a 90° rotation around the **z** axis followed by a 90° rotation around the **x** axis.