Reference · Vectors

vec.mul

signature
vec.mul(list | num, list | num) -> any

What it does

Multiplies item by item: two lists of the same length give a list of their pairwise products, and a list and a number give the list with each item scaled by the number. Ints stay ints and floats stay floats, as with the * operator. Two numbers give their product. For the sum of the products use vec.dot.

Examples

print(vec.mul([1, 2, 3], [4, 5, 6]))print(vec.mul([1, 2, 3], 10))print(vec.mul(0.5, [10, 20]))
output
[4, 10, 18][10, 20, 30][5.0, 10.0]

Edge cases

print(vec.mul([1.5, 2], 2))print(vec.mul([1, 2.5], 0))print(vec.mul([], 3))
output
[3.0, 4][0, 0.0][]

Errors

Lists of different lengths, an item that is not an int or a float, or an argument that is neither a list nor a number are runtime errors, not error values. An int product past 64 bits is an integer overflow runtime error; use floats for numbers that large.

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

Chaining

xs | vec.mul(k) scales a list in a pipeline, often before rounding:

print(vec.linspace(0, 1, 5) | vec.mul(100) | vec.round)print(vec.ones(3) | vec.mul(7))
output
[0, 25, 50, 75, 100][7, 7, 7]

See also

vec.dot, vec.div, vec.pow, vec.cumprod, math.prod

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

Thread 0 posts

On vec.mul(): 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