Reference · Vectors

vec.dot

signature
vec.dot(list, list) -> num

What 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]))
output
327.0

Edge cases

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)
output
0 00.6 0.6000000000000001

Errors

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.

this one fails
print(vec.dot([1, 2], [1]))
output
line 1: `vec.dot` needs lists of the same length (2 and 1)
this one fails
print(vec.dot([9223372036854775807, 1], [2, 1]))
output
line 1: integer overflow

Chaining

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)))
output
110.9838699100999074

See also

vec.mul, vec.norm, vec.matmul, math.sumprod, sum

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

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