Reference · Vectors

vec.pow

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

What it does

Each item raised to a power. The result is always a list of floats, even for ints to a whole power: vec.pow([2, 3], 2) is [4.0, 9.0]. A fractional power takes a root, so vec.pow(xs, 0.5) is vec.sqrt(xs).

Examples

print(vec.pow([1, 2, 3], 2))print(vec.pow([4, 9], 0.5))print(vec.pow([2, 3], 3))
output
[1.0, 4.0, 9.0][2.0, 3.0][8.0, 27.0]

Edge cases

print(vec.pow([-8], 1 / 3), vec.pow([-2], 0.5))print(vec.pow([0, 5], 0), vec.pow([2, 4], -1))print(vec.pow([], 2))print(vec.pow([10], 400))
output
[NaN] [NaN][1.0, 1.0] [0.5, 0.25][][inf]

Errors

A list with an item that is not an int or a float, a missing or non-numeric power, or a first argument that is not a list are runtime errors, not error values.

this one fails
print(vec.pow([1, "a"], 2))
output
line 1: `vec.pow` expects a list of numbers, but item 1 is str
this one fails
print(vec.pow([1, 2]))
output
line 1: `vec.pow` expects a number as argument 2, got nothing

Chaining

xs | vec.pow(2) squares a list; summed, it is the squared length:

print(vec.arange(1, 4) | vec.pow(2) | sum)print([3, 4] | vec.pow(2) | sum | math.sqrt)
output
14.05.0

See also

vec.sqrt, vec.exp, vec.mul, vec.norm, math.pow

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

Thread 0 posts

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