Reference · Vectors
vec.dot
vec.dot(list, list) -> numWhat it does
The dot product of two lists of the same length: the sum of the pairwise products. Two lists of ints give an int, computed exactly; any float gives a float, summed with compensation so that the result is what exact arithmetic rounded once would give.
Examples
print(vec.dot([1, 2, 3], [4, 5, 6]))print(vec.dot([1.5, 2], [2, 2]))327.0Edge cases
- Two empty lists give
0. nullcounts as the empty list.- The float sum is compensated:
vec.dot([0.1, 0.2, 0.3], [1, 1, 1])is0.6, where adding the three by hand gives0.6000000000000001.
print(vec.dot([], []), vec.dot(null, null))print(vec.dot([0.1, 0.2, 0.3], [1, 1, 1]), 0.1 + 0.2 + 0.3)0 00.6 0.6000000000000001Errors
Lists of different lengths, an item that is not an int or a float, or an argument that
is not a list are runtime errors, not error values. An int result past 64 bits is an
integer overflow runtime error; convert to floats for numbers that large.
print(vec.dot([1, 2], [1]))line 1: `vec.dot` needs lists of the same length (2 and 1)print(vec.dot([9223372036854775807, 1], [2, 1]))line 1: integer overflowChaining
xs | vec.dot(ys) reads naturally; with vec.norm it gives the cosine of the angle
between two lists:
let a = [1, 2]let b = [3, 4]print(a | vec.dot(b))print(vec.dot(a, b) / (vec.norm(a) * vec.norm(b)))110.9838699100999074See also
vec.mul, vec.norm, vec.matmul, math.sumprod, sum
Thread 0 posts
On vec.dot(): 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