TExcelTAD

Rotations

A rotation turns vectors about the origin without changing their lengths or the angles between them. Rotations are the orthogonal matrices with determinant +1+1. They appear in ML whenever the coordinate system is arbitrary: PCA rotates data into its principal axes, data augmentation rotates images, and rotary position embeddings rotate query and key vectors in transformers.

Rotations in the plane

Rotating by angle θ\theta counter-clockwise sends e1=(1,0)\mathbf{e}_1 = (1, 0) to (cos⁡θ,sin⁡θ)(\cos\theta, \sin\theta) and e2=(0,1)\mathbf{e}_2 = (0, 1) to (−sin⁡θ,cos⁡θ)(-\sin\theta, \cos\theta). Those images are the columns:

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

Properties: R(θ)TR(θ)=IR(\theta)^T R(\theta) = I, det⁡R(θ)=cos⁡2θ+sin⁡2θ=1\det R(\theta) = \cos^2\theta + \sin^2\theta = 1, R(α)R(β)=R(α+β)R(\alpha)R(\beta) = R(\alpha + \beta) (2D rotations commute), and R(θ)−1=R(−θ)=R(θ)TR(\theta)^{-1} = R(-\theta) = R(\theta)^T.

python · try it yourself
import numpy as np

def R(t): return np.array([[np.cos(t), -np.sin(t)], [np.sin(t), np.cos(t)]])
x, y = np.array([3.0, 1.0]), np.array([-1.0, 2.0])
Q = R(np.pi / 6)
print("lengths before:", np.round([np.linalg.norm(x), np.linalg.norm(y)], 4),
      " after:", np.round([np.linalg.norm(Q @ x), np.linalg.norm(Q @ y)], 4))
print("dot before:", x @ y, " after:", round(float((Q @ x) @ (Q @ y)), 10))
print("R(a)R(b) = R(a+b):", np.allclose(R(0.3) @ R(0.5), R(0.8)), " det:", round(np.linalg.det(Q), 10))
lengths before: [3.1623 2.2361]  after: [3.1623 2.2361]
dot before: -1.0  after: -1.0
R(a)R(b) = R(a+b): True  det: 1.0

Rotations in three and more dimensions

In R3\mathbb{R}^3 a rotation turns about an axis. Rotating about the zz-axis keeps e3\mathbf{e}_3 fixed and rotates the xyxy-plane:

Rz(θ)=[cos⁡θ−sin⁡θ0sin⁡θcos⁡θ0001]R_z(\theta) = \begin{bmatrix} \cos\theta & -\sin\theta & 0 \\ \sin\theta & \cos\theta & 0 \\ 0 & 0 & 1 \end{bmatrix}

RxR_x and RyR_y are built the same way. In 3D, order matters: RxRz≠RzRxR_xR_z \ne R_zR_x in general. In Rn\mathbb{R}^n, a Givens rotation rotates the plane of two coordinates i,ji, j and leaves the rest alone; every rotation in Rn\mathbb{R}^n is a product of Givens rotations.

python · try it yourself
import numpy as np

def Rx(t):
    c, s = np.cos(t), np.sin(t); return np.array([[1, 0, 0], [0, c, -s], [0, s, c]])
def Rz(t):
    c, s = np.cos(t), np.sin(t); return np.array([[c, -s, 0], [s, c, 0], [0, 0, 1]])
v = np.array([1.0, 0.0, 0.0])
a = Rx(np.pi / 2) @ Rz(np.pi / 2) @ v     # z first, then x
b = Rz(np.pi / 2) @ Rx(np.pi / 2) @ v     # x first, then z
print("z then x:", np.round(a, 10) + 0, "  x then z:", np.round(b, 10) + 0)
z then x: [0. 0. 1.]   x then z: [0. 1. 0.]

Rotations versus reflections

Orthogonal matrices with det⁡Q=−1\det Q = -1 are reflections (possibly combined with rotations): they also keep lengths and angles but flip orientation, like a mirror. Numerical routines sometimes return a reflection where you wanted a rotation; the fix is to flip the sign of one column.

Rotary position embeddings

Transformers need to know token positions. RoPE splits a query vector into 2D pairs and rotates pair ii by angle m θim\,\theta_i at position mm. Because R(α)TR(β)=R(β−α)R(\alpha)^T R(\beta) = R(\beta - \alpha), the score between a query at position mm and a key at position nn depends only on the offset n−mn - m:

(R(mθ)q)T(R(nθ)k)=qTR((n−m)θ) k(R(m\theta)\mathbf{q})^T(R(n\theta)\mathbf{k}) = \mathbf{q}^T R((n - m)\theta)\,\mathbf{k}

See Positional Encodings for the full scheme.

In software

  • PCA's principal axes form an orthogonal matrix, so projecting onto all of them is a rotation (or reflection) of the data.
  • scipy.spatial.transform.Rotation converts between matrices, quaternions and Euler angles; games prefer quaternions for interpolation.
  • Image augmentation rotates training images by small random angles to make classifiers rotation-robust.

Exercises

Exercise 1: Rotate by hand

Rotate (2,0)(2, 0) by 90∘90^\circ and then by another 90∘90^\circ.

Answer

(0,2)(0, 2), then (−2,0)(-2, 0).

Solution

R(90∘)=[0−110]R(90^\circ) = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix} maps (2,0)↦(0,2)(2, 0) \mapsto (0, 2) and (0,2)↦(−2,0)(0, 2) \mapsto (-2, 0). Two quarter turns make a half turn, R(180∘)=−IR(180^\circ) = -I.

Exercise 2: Rotation or reflection?

Is [0110]\begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} a rotation?

Answer

No, it is a reflection (determinant −1-1).

Solution

Its columns are orthonormal, so it is orthogonal, but det⁡=0⋅0−1⋅1=−1\det = 0 \cdot 0 - 1 \cdot 1 = -1. It reflects across the line y=xy = x.

Exercise 3: Relative positions (code)

Check numerically that (R(mθ)q)T(R(nθ)k)(R(m\theta)\mathbf{q})^T(R(n\theta)\mathbf{k}) is the same for (m,n)=(2,5)(m, n) = (2, 5) and (m,n)=(10,13)(m, n) = (10, 13) with θ=0.4\theta = 0.4.

Answer

Both give the same score, since the offset is 3 in both cases.

Solution
python · try it yourself
import numpy as np
def R(t): return np.array([[np.cos(t), -np.sin(t)], [np.sin(t), np.cos(t)]])
q, k, th = np.array([1.0, 2.0]), np.array([0.5, -1.0]), 0.4
s1 = (R(2 * th) @ q) @ (R(5 * th) @ k)
s2 = (R(10 * th) @ q) @ (R(13 * th) @ k)
print(round(float(s1), 10), round(float(s2), 10))
1.3205415402 1.3205415402

Test yourself

A rotation matrix RR satisfies…

  1. RTR=IR^TR = I and det⁡R=1\det R = 1
  2. RTR=IR^TR = I and det⁡R=−1\det R = -1
  3. R2=RR^2 = R
  4. R=RTR = R^T

Do 3D rotations commute?

  1. Always
  2. Not in general: the order matters
  3. Only for rotations by 90°
  4. Only for 180° rotations

In RoPE, the attention score between positions mm and nn depends on…

  1. m+nm + n
  2. only n−mn - m
  3. m⋅nm \cdot n
  4. nothing positional