borch

Foundations · 1

Vectors & dot products

A vector is a list of numbers with a direction and a length. Everything below runs in this page — edit any block and press Run, or ⌘/Ctrl + Enter.

A vector is a 1-D tensor

One axis, a handful of numbers. Its length — the L2 norm — is the square root of the sum of squares.

Adding and scaling

Add two vectors component by component; scale one by a number. In borch.ts an operator takes a tensor, so a scalar is written Tensor.full([], k).

The dot product

a.dot(b) multiplies matching entries and sums them — one number. Geometrically it is |a||b|cos θ, so it reads the angle between two directions.

Seeing alignment: the Gram matrix

Stack a few vectors as rows and multiply by the transpose — cell (i, j) is the dot product of vector i and vector j. Brighter means more aligned; the diagonal is each vector's squared length.

Your turn: make them perpendicular

Two vectors are perpendicular exactly when their dot product is zero. Change b below so that a · b = 0 — then the page says you have it.

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.