Reference · Vectors
vec.mul
vec.mul(list | num, list | num) -> anyWhat 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]))[4, 10, 18][10, 20, 30][5.0, 10.0]Edge cases
- Each pair is typed on its own:
[1.5, 2]times 2 is[3.0, 4]. - Scaling by 0 gives zeros, ints for ints and
0.0for floats. - The empty list gives the empty list.
print(vec.mul([1.5, 2], 2))print(vec.mul([1, 2.5], 0))print(vec.mul([], 3))[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.
print(vec.mul([9223372036854775807], 2))line 1: integer overflowprint(vec.mul([1, 2], [1]))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))[0, 25, 50, 75, 100][7, 7, 7]See also
vec.dot, vec.div, vec.pow, vec.cumprod, math.prod
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