borch

Foundations · 4

Norms, distance & projection

A norm turns a vector into a single length. From it come distance (the norm of a difference), unit vectors (divide by the norm), and projection (how much of one vector lies along another).

The L2 norm is length

v.norm() is the square root of the sum of squares — the straight-line length of the vector from the origin.

Distance is the norm of the difference

The distance between two points is ‖a − b‖. Same operation, one subtraction first.

Projection

The scalar projection of a onto b is a·b / ‖b‖ — how far along b's direction a reaches. Against a unit axis it is just that coordinate.

Seeing distance: the pairwise matrix

Take four points and measure every point-to-point distance — ‖P_i − P_j‖, built with broadcasting. The diagonal is dark (a point is distance 0 from itself); the far-apart pairs are bright.

Your turn: make a unit vector

A unit vector points the same way but has length 1. Divide v by its own norm so that ‖u‖ = 1.

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.