Skip to content

Rotations

A rotation turns space around a fixed point without stretching, squashing, or reflecting it. Rotations are exactly the orthogonal matrices with determinant +1+1 — the “rigid motions” that preserve every length and every angle. They’re used to augment training images, orient robots and point clouds, and constrain neural-network weights to stay well-behaved.

A real-life example: rotating a photo

When an app straightens a tilted photo, it applies a rotation matrix to every pixel coordinate. The picture turns, but nothing about it distorts — distances between features and the angles of lines are all preserved. That “turn without distort” is the defining property of a rotation, and it’s why rotations are a safe, label-preserving data augmentation in computer vision.

Rotations in 2-D

Rotating the plane counter-clockwise by angle θ\theta sends the basis vectors to

Φ(e1)=[cosθsinθ],Φ(e2)=[sinθcosθ],\Phi(\mathbf{e}_1) = \begin{bmatrix}\cos\theta\\\sin\theta\end{bmatrix},\qquad \Phi(\mathbf{e}_2) = \begin{bmatrix}-\sin\theta\\\cos\theta\end{bmatrix},

so — since the columns of a matrix are the images of the basis vectors — the rotation matrix is

R(θ)=[cosθsinθsinθcosθ].R(\theta) = \begin{bmatrix}\cos\theta & -\sin\theta\\ \sin\theta & \cos\theta\end{bmatrix}.

Watch it turn

The shape below is multiplied by R(θ)R(\theta) as θ\theta sweeps. The matrix updates live; notice that the shape’s size and proportions never change — only its orientation:

sketch A 2-D rotation R(θ) p5.js
A shape multiplied by the rotation matrix R(θ) as θ increases. The live matrix is shown; distances and angles are preserved — the shape only turns, never distorts.

Rotations in 3-D and beyond

In 3-D you rotate about an axis. The three elementary rotations fix one axis and rotate the other two:

Rx(θ)=[1000cosθsinθ0sinθcosθ],  Ry(θ)=[cosθ0sinθ010sinθ0cosθ],  Rz(θ)=[cosθsinθ0sinθcosθ0001].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}.

In nn dimensions, a Givens rotation Rij(θ)R_{ij}(\theta) is the identity matrix with a 2×22\times2 rotation block placed in rows/columns ii and jj — it rotates within one coordinate plane and leaves the other n2n-2 axes fixed. The 2-D matrix is the special case n=2n = 2.

Properties

diagram Diagram mermaid

Rotations are orthogonal matrices, so R1=RR^{-1} = R^\top (rotating back is just the transpose), and they preserve distances and angles. A subtlety that bites in robotics and graphics: in 3-D and higher, rotations do not commute — rotating about xx then yy differs from yy then xx. Only in 2-D (all rotations about the same origin) do they commute.

NumPy

rotations.py
import numpy as np
 
def R2(theta):
    c, s = np.cos(theta), np.sin(theta)
    return np.array([[c, -s], [s, c]])
 
x = np.array([3.0, 4.0])
R = R2(np.pi / 2)                      # 90° counter-clockwise
print("rotated:", np.round(R @ x, 4))  # [-4, 3]
 
# rotations preserve length and are orthogonal
print("length preserved:", np.allclose(np.linalg.norm(R @ x), np.linalg.norm(x)))
print("RᵀR = I:", np.allclose(R.T @ R, np.eye(2)))
print("det(R) = +1:", np.round(np.linalg.det(R), 6))
 
# 3-D rotations do NOT commute
def Rz(t): c,s=np.cos(t),np.sin(t); return np.array([[c,-s,0],[s,c,0],[0,0,1]])
def Rx(t): c,s=np.cos(t),np.sin(t); return np.array([[1,0,0],[0,c,-s],[0,s,c]])
a, b = Rz(0.5) @ Rx(0.5), Rx(0.5) @ Rz(0.5)
print("Rz·Rx == Rx·Rz ?", np.allclose(a, b))   # False
rotations.py
import numpy as np
 
def R2(theta):
    c, s = np.cos(theta), np.sin(theta)
    return np.array([[c, -s], [s, c]])
 
x = np.array([3.0, 4.0])
R = R2(np.pi / 2)                      # 90° counter-clockwise
print("rotated:", np.round(R @ x, 4))  # [-4, 3]
 
# rotations preserve length and are orthogonal
print("length preserved:", np.allclose(np.linalg.norm(R @ x), np.linalg.norm(x)))
print("RᵀR = I:", np.allclose(R.T @ R, np.eye(2)))
print("det(R) = +1:", np.round(np.linalg.det(R), 6))
 
# 3-D rotations do NOT commute
def Rz(t): c,s=np.cos(t),np.sin(t); return np.array([[c,-s,0],[s,c,0],[0,0,1]])
def Rx(t): c,s=np.cos(t),np.sin(t); return np.array([[1,0,0],[0,c,-s],[0,s,c]])
a, b = Rz(0.5) @ Rx(0.5), Rx(0.5) @ Rz(0.5)
print("Rz·Rx == Rx·Rz ?", np.allclose(a, b))   # False
text
rotated: [-4.  3.]
length preserved: True
RᵀR = I: True
det(R) = +1: 1.0
Rz·Rx == Rx·Rz ? False
text
rotated: [-4.  3.]
length preserved: True
RᵀR = I: True
det(R) = +1: 1.0
Rz·Rx == Rx·Rz ? False

Why this matters for ML

  • Data augmentation: rotating images (and 3-D point clouds) generates new, correctly-labeled training examples, improving model robustness.
  • Orthogonal weight constraints / initializations keep signal norms stable through deep networks (rotations don’t amplify or shrink).
  • PCA and SVD decompose transformations into rotations and scalings; the rotation part reorients data onto its principal axes.

🧪 Try It Yourself

Exercise 1 – Rotate a vector 90°

Exercise 2 – Rotations preserve length

Exercise 3 – 3-D rotations don’t commute

Recap

  • A rotation turns space rigidly: R(θ)=[cosθsinθsinθcosθ]R(\theta) = \left[\begin{smallmatrix}\cos\theta & -\sin\theta\\ \sin\theta & \cos\theta\end{smallmatrix}\right] in 2-D, axis rotations in 3-D, Givens rotations in nn-D.
  • Rotations are orthogonal (R1=RR^{-1} = R^\top) with det=+1\det = +1; they preserve lengths and angles.
  • 2-D rotations commute; 3-D+ rotations do not — order matters.
  • In ML they drive data augmentation, stable weight constraints, and the rotational part of PCA/SVD.

🎓 Chapter 3 complete

You’ve built the full geometric toolkit on top of Chapter 2’s algebra: norms → inner products → lengths & distances → angles & orthogonality → orthonormal bases → orthogonal complements → function inner products → projections → rotations. Similarity, distance, orthogonality, and projection are now concrete operations you can compute and picture — and they’re the direct mechanics behind PCA, regression, SVMs, and embeddings.

Head back to the Analytic Geometry Overview for the full map, or revisit Chapter 2 — Linear Algebra.

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did