Reference · Vectors

vec.norm

signature
vec.norm(list, num=) -> float

What it does

The length of a list of numbers as a vector: without p, or with p of 2, the square root of the sum of the squares. With p it is the p-norm, the sum of |x|^p to the power 1/p: p of 1 is the sum of the absolute values, and p of math.inf is the largest absolute value. The result is always a float.

Examples

print(vec.norm([3, 4]))print(vec.norm([3, 4], 1))print(vec.norm([3, -4], math.inf))
output
5.07.04.0

Edge cases

print(vec.norm([]), vec.norm(null))print(vec.norm([1, 1, 1, 1], 4))print(vec.norm([1, 2, 3], 0), vec.norm([3, 4], null))
output
0.0 0.01.4142135623730951inf 5.0

Errors

A list with an item that is not an int or a float, a p that is not a number, or a first argument that is not a list are runtime errors, not error values.

this one fails
print(vec.norm([1], "x"))
output
line 1: `vec.norm` expects a number as argument 2, got str

Chaining

The distance between two points is the norm of their difference, and dividing a list by its norm gives a unit vector:

print(vec.sub([1, 1], [4, 5]) | vec.norm)let v = [3, 4]print(v | vec.div(vec.norm(v)))
output
5.0[0.6, 0.8]

See also

vec.dot, vec.abs, vec.pow, math.hypot, math.dist

Thread
7Useful? Like it, or add to its thread below.

Thread 0 posts

On vec.norm(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo