Reference · Vectors

vec.ones

signature
vec.ones(int) -> list

What it does

A list of n ints, all 1. Scaled with vec.mul it is a list of any one number, as vec.full(n, x) is directly.

Examples

print(vec.ones(3))print(vec.ones(3) | vec.mul(7))
output
[1, 1, 1][7, 7, 7]

Edge cases

print(vec.ones(0))print(vec.ones(3) == vec.full(3, 1), vec.ones(3) == vec.full(3, 1.0))
output
[]true true

Errors

A negative count, or one over ten million, is a runtime error, not an error value; so is a count that is not an int.

this one fails
print(vec.ones(20000000))
output
line 1: `vec.ones`: 20000000 is not a count from 0 to 10000000

Chaining

Equal weights for a weighted mean, and a dot product with ones as a plain sum:

let xs = [2, 4, 9]print(vec.dot(xs, vec.ones(3)))print(vec.ones(3) | vec.div(3) | vec.dot(xs))
output
155.0

See also

vec.zeros, vec.full, vec.mul, vec.dot

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

Thread 0 posts

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