Reference · Vectors
vec.norm
vec.norm(list, num=) -> floatWhat 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))5.07.04.0Edge cases
- The empty list, or
null, has length0.0. - Ints give a float too:
vec.norm([3, 4])is5.0. pof 0 divides by zero inside the formula and givesinf; it is not an error.nullaspmeans 2.
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))0.0 0.01.4142135623730951inf 5.0Errors
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.
print(vec.norm([1], "x"))line 1: `vec.norm` expects a number as argument 2, got strChaining
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)))5.0[0.6, 0.8]See also
vec.dot, vec.abs, vec.pow, math.hypot, math.dist
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