borch

Foundations · 2

Matrices & matrix multiply

A matrix is a grid of numbers that acts on vectors — it stretches, rotates, and mixes their components. Matrix multiply is how you apply and compose those actions.

A matrix times a vector

M.mv(v) sends a vector to a new one — each output entry is a dot product of a row of M with v.

Matrix times matrix is composition

A.matmul(B) is the single map that does B first, then A. Order matters — A@B and B@A are usually different.

Shapes have to line up

[m, k] @ [k, n] → [m, n]. The inner sizes must match; the outer two become the result's shape.

A matrix as motion: rotation traces a circle

A rotation matrix turns a vector by a fixed angle. Apply it over and over and the vector walks around a circle — plotted here as its x-coordinate, a clean cosine.

Your turn: get the order right

Applying B then A to a vector is the same as multiplying by A@B first. The block below composes them in the wrong order — fix it so the composed map matches the step-by-step one.

What to remember

If a block above errors, that is worth knowing. Every example on this page runs against the same library the tests run against — nothing here is a screenshot. Press Reset to get the original code back.