Linear algebra for LLMsPart 3: Matrices as transformations

Rotations

7 min

The rotation matrix

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

Its columns are where e1e_1 and e2e_2 land after turning by θ\theta anticlockwise: e1e_1 goes to [cos⁡θ,sin⁡θ][\cos\theta, \sin\theta], and e2e_2 stays at 90° from it.

Write

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

Two angles to know

R(90∘)=R(90^\circ) =R(30∘)≈R(30^\circ) \approx

cos⁡90∘=0\cos 90^\circ = 0 and sin⁡90∘=1\sin 90^\circ = 1 give the quarter turn from before. For 30°, cos⁡30∘≈0.866\cos 30^\circ \approx 0.866 and sin⁡30∘=0.5\sin 30^\circ = 0.5.

Turning a vector

90°vR(90°)v
R(90∘)[21]=[−12]\displaystyle R(90^\circ)\begin{bmatrix} 2 \\ 1 \end{bmatrix} = \begin{bmatrix} -1 \\ 2 \end{bmatrix}

Same length (5\sqrt{5} both), new direction.

Rotations keep lengths and dot products

∣Rx∣=∣x∣(Ra)⋅(Rb)=a⋅b\displaystyle |Rx| = |x| \qquad (Ra) \cdot (Rb) = a \cdot b
because R⊤R=I\displaystyle \text{because } R^\top R = I

Turn two vectors by the same angle and the angle between them stays the same, so their dot product does too. In symbols: (Ra)⊤(Rb)=a⊤R⊤R b=a⊤b(Ra)^\top (Rb) = a^\top R^\top R\, b = a^\top b. A matrix with R⊤R=IR^\top R = I is called orthogonal.

Write

R⊤R=IR^\top R = I, so (Ra)⋅(Rb)=a⋅b(Ra) \cdot (Rb) = a \cdot b and ∣Rx∣=∣x∣|Rx| = |x|.

Angles add; the transpose turns back

R(α) R(β)=R(α+β)\displaystyle R(\alpha)\,R(\beta) = R(\alpha + \beta)
R(θ)⊤=R(−θ)=R(θ)−1\displaystyle R(\theta)^\top = R(-\theta) = R(\theta)^{-1}

Turning by β\beta and then by α\alpha is one turn by α+β\alpha + \beta. And the transpose turns back the other way, so for rotations the inverse costs nothing: just transpose.

Write

R(α)R(β)=R(α+β)R(\alpha)R(\beta) = R(\alpha + \beta); R(θ)⊤=R(−θ)R(\theta)^\top = R(-\theta).

✏️ Your turn

Two quarter turns

R(90∘) R(90∘)=  ?\displaystyle R(90^\circ)\,R(90^\circ) = \;?

Multiply the matrices, then say which single rotation it is and where it sends [1,0][1, 0].

Work it out on paper, then go to the next card to check.

Answer: a half turn

R(90∘)2=R(90^\circ)^2 ==R(180∘)= R(180^\circ)

[1,0][1, 0] goes to [−1,0][-1, 0]: every vector points the opposite way. The angles added: 90∘+90∘=180∘90^\circ + 90^\circ = 180^\circ.

Only the difference in angle survives

(Rαq)⋅(Rβk)=q⊤Rα⊤Rβ k\displaystyle (R_\alpha q) \cdot (R_\beta k) = q^\top R_\alpha^\top R_\beta\, k
=q⊤Rβ−α k\displaystyle = q^\top R_{\beta - \alpha}\, k

Turn qq by α\alpha and kk by β\beta. Their dot product depends only on β−α\beta - \alpha, not on the two angles separately.

In a transformer

This is RoPE, the position encoding in Llama and most current LLMs. A query at position mm is turned by an angle proportional to mm, a key at position nn by one proportional to nn, so their score depends only on the distance n−mn - m (Part 6).

Write

(Rαq)⋅(Rβk)=q⊤Rβ−α k(R_\alpha q) \cdot (R_\beta k) = q^\top R_{\beta - \alpha}\, k: only the difference matters.

📌 Recap

Rotations in one card

  • R(θ)=[cos⁡θ−sin⁡θsin⁡θcos⁡θ]R(\theta) = \begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix}.
  • R⊤R=IR^\top R = I: lengths and dot products are kept.
  • R(α)R(β)=R(α+β)R(\alpha)R(\beta) = R(\alpha + \beta), R(θ)−1=R(θ)⊤=R(−θ)R(\theta)^{-1} = R(\theta)^\top = R(-\theta).
  • (Rαq)⋅(Rβk)(R_\alpha q) \cdot (R_\beta k) depends only on β−α\beta - \alpha: the idea behind RoPE.

Write

The four lines above.