Rotations
A rotation turns vectors about the origin without changing their lengths or the angles between them. Rotations are the orthogonal matrices with determinant . 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 counter-clockwise sends to and to . Those images are the columns:
Properties: , , (2D rotations commute), and .
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 a rotation turns about an axis. Rotating about the -axis keeps fixed and rotates the -plane:
and are built the same way. In 3D, order matters: in general. In , a Givens rotation rotates the plane of two coordinates and leaves the rest alone; every rotation in is a product of Givens rotations.
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 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 by angle at position . Because , the score between a query at position and a key at position depends only on the offset :
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.Rotationconverts 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 by and then by another .
Answer
, then .
Solution
maps and . Two quarter turns make a half turn, .
Exercise 2: Rotation or reflection?
Is a rotation?
Answer
No, it is a reflection (determinant ).
Solution
Its columns are orthonormal, so it is orthogonal, but . It reflects across the line .
Exercise 3: Relative positions (code)
Check numerically that is the same for and with .
Answer
Both give the same score, since the offset is 3 in both cases.
Solution
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 satisfies…
- and
- and
Do 3D rotations commute?
- Always
- Not in general: the order matters
- Only for rotations by 90°
- Only for 180° rotations
In RoPE, the attention score between positions and depends on…
- only
- nothing positional