Reference · Vectors

vec.sub

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

What it does

Subtracts item by item: two lists of the same length give a list of their pairwise differences, a list and a number give the list with the number taken from each item, and a number and a list give the number less each item. Ints stay ints, floats stay floats, as with the - operator. Two numbers give their difference.

Examples

print(vec.sub([10, 20, 30], [1, 2, 3]))print(vec.sub([10, 20, 30], 5))print(vec.sub(100, [10, 20, 30]))print(vec.sub([1, 2], [0.5, 0.5]))
output
[9, 18, 27][5, 15, 25][90, 80, 70][0.5, 1.5]

Edge cases

print(vec.sub(5, [1, 2]), vec.sub([1, 2], 5))print(vec.sub([], 1))let sales = [3, 5, 4, 6, 8]print(vec.sub(sales, stats.mean(sales)))
output
[4, 3] [-4, -3][][-2.2, -0.20000000000000018, -1.2000000000000002, 0.7999999999999998, 2.8]

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 result past 64 bits is an integer overflow runtime error.

this one fails
print(vec.sub([1, 2], [1, 2, 3]))
output
line 1: `vec.sub` needs lists of the same length (2 and 3)
this one fails
print(vec.sub([1, "2"], 1))
output
line 1: `vec.sub` expects numbers, got str and int

Chaining

The left side is what is subtracted from: xs | vec.sub(1) takes 1 from each item.

let sales = [3, 5, 4, 6, 8]print(sales | vec.sub(stats.mean(sales)) | vec.round(2))print(vec.norm(vec.sub([1, 1], [4, 5])))
output
[-2.2, -0.2, -1.2, 0.8, 2.8]5.0

See also

vec.add, vec.diff, vec.abs, stats.zscore, math.dist

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

Thread 0 posts

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