Linear algebra for LLMsPart 3: Matrices as transformations
Rotations
7 min
The rotation matrix
Its columns are where and land after turning by anticlockwise: goes to , and stays at 90° from it.
Write
Two angles to know
and give the quarter turn from before. For 30°, and .
Turning a vector
Same length ( both), new direction.
Rotations keep lengths and dot products
Turn two vectors by the same angle and the angle between them stays the same, so their dot product does too. In symbols: . A matrix with is called orthogonal.
Write
Angles add; the transpose turns back
Turning by and then by is one turn by . And the transpose turns back the other way, so for rotations the inverse costs nothing: just transpose.
Write
✏️ Your turn
Two quarter turns
Multiply the matrices, then say which single rotation it is and where it sends .
Work it out on paper, then go to the next card to check.
Answer: a half turn
goes to : every vector points the opposite way. The angles added: .
Only the difference in angle survives
Turn by and by . Their dot product depends only on , 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 is turned by an angle proportional to , a key at position by one proportional to , so their score depends only on the distance (Part 6).
Write
📌 Recap
Rotations in one card
- .
- : lengths and dot products are kept.
- , .
- depends only on : the idea behind RoPE.
Write