std.array
type Array = { data: List[Float], shape: List[Length], strides: List[I64], offset: I64,} where len(it.shape) == len(it.strides)An n-dimensional array of 64-bit floats (Float), NumPy’s float64: a
view of a flat list. The arithmetic takes bounds on the elements’
magnitudes (within), so that every float result is finite.
- Element
(i0, i1, …)is `data[offset + i0 * strides[0] + i1 *
strides[1] + …]. Shapes, strides, offsets and indices are I64`s.
shapehas one length per axis. A 0-axis array holds one value.transpose,slice,rowandreshapeof a row-major array are
views: O(number of axes), the data is shared.
- Arithmetic, reductions and
matmulread row-major data. A view that
isn’t row-major is copied once first (compact, O(n)).
- The constructors (
Array.zeros,Array.full,Array.arange,
Array.from_list, Array.from_rows) keep strides and offset inside
data.
- Changing an element (
mut a.set) copies the data first when another
array shares it.
Length
Section titled “Length”type Length = I64 where it >= 0A length, an index or a count of elements: a 64-bit integer that isn’t negative.
ArrayI64
Section titled “ArrayI64”type ArrayI64 = { data: List[I64], shape: List[Length], strides: List[I64], offset: I64,} where len(it.shape) == len(it.strides)An n-dimensional array of 64-bit integers (I64), NumPy’s int64: a view
of a flat list. Arithmetic wraps modulo 2^64, as NumPy’s does.
- Element
(i0, i1, …)is `data[offset + i0 * strides[0] + i1 *
strides[1] + …]. Shapes, strides, offsets and indices are I64`s.
shapehas one length per axis. A 0-axis array holds one value.transpose,slice,rowandreshapeof a row-major array are
views: O(number of axes), the data is shared.
- Arithmetic, reductions and
matmulread row-major data. A view that
isn’t row-major is copied once first (compact, O(n)).
- The constructors (
ArrayI64.zeros,ArrayI64.full,ArrayI64.arange,
ArrayI64.from_list, ArrayI64.from_rows) keep strides and offset inside
data.
- Changing an element (
mut a.set) copies the data first when another
array shares it.
ArrayI32
Section titled “ArrayI32”type ArrayI32 = { data: List[I32], shape: List[Length], strides: List[I64], offset: I64,} where len(it.shape) == len(it.strides)An n-dimensional array of 32-bit integers (I32), NumPy’s int32: a view
of a flat list. Arithmetic wraps modulo 2^32, as NumPy’s does. Sums are
64-bit (I64, an ArrayI64 along an axis), as NumPy’s sum of int32
is.
- Element
(i0, i1, …)is `data[offset + i0 * strides[0] + i1 *
strides[1] + …]. Shapes, strides, offsets and indices are I64`s.
shapehas one length per axis. A 0-axis array holds one value.transpose,slice,rowandreshapeof a row-major array are
views: O(number of axes), the data is shared.
- Arithmetic, reductions and
matmulread row-major data. A view that
isn’t row-major is copied once first (compact, O(n)).
- The constructors (
ArrayI32.zeros,ArrayI32.full,ArrayI32.arange,
ArrayI32.from_list, ArrayI32.from_rows) keep strides and offset inside
data.
- Changing an element (
mut a.set) copies the data first when another
array shares it.
Methods
Section titled “Methods”Array.full
Section titled “Array.full”fn full(shape: List[Length], x: Float) -> Array ensures it.shape == shapeAn array of shape with every element x.
Cost: O(n).
Array.zeros
Section titled “Array.zeros”fn zeros(shape: List[Length]) -> Array ensures it.shape == shapeAn array of shape filled with 0.0.
Cost: O(n).
Array.ones
Section titled “Array.ones”fn ones(shape: List[Length]) -> Array ensures it.shape == shapeAn array of shape filled with 1.0.
Cost: O(n).
Array.arange
Section titled “Array.arange”fn arange(n: Length) -> Array ensures it.shape == [n]0.0, 1.0, …, up to n - 1: one axis of length n.
Cost: O(n).
Array.from_list
Section titled “Array.from_list”fn from_list(xs: List[Float]) -> ArrayOne axis holding xs.
Cost: O(1): the array shares the list.
Array.from_rows
Section titled “Array.from_rows”fn from_rows(rows: List[List[Float]]) -> Option[Array]Two axes, one row per list. None when the rows differ in length.
Cost: O(n).
Array.dense
Section titled “Array.dense”fn dense(data: List[Float], shape: List[Length]) -> Array ensures it.shape == shapeA row-major array of shape over data.
Array.ndim
Section titled “Array.ndim”fn ndim(self) -> LengthThe number of axes. Cost: O(1).
Array.size
Section titled “Array.size”fn size(self) -> Length ensures it == count(...self)The number of elements. Cost: O(number of axes).
Array.within
Section titled “Array.within”fn within(self, b: Float) -> Bool ensures it == all(self.to_list(), x -> abs(x) <= b)Whether every element is at most b in magnitude. The float arithmetic’s contracts use it, so that no result overflows.
Cost: O(n), plus the copy to_list makes of a view that isn’t row-major.
Array.at_least
Section titled “Array.at_least”fn at_least(self, m: Float) -> Bool ensures it == all(self.to_list(), x -> abs(x) >= m)Whether every element is at least m in magnitude: div’s divisor is at least the inverse of factor_bound.
Cost: O(n), as within.
Array.is_compact
Section titled “Array.is_compact”fn is_compact(self) -> BoolWhether the array reads its data row-major from its start to its end, so the data is its elements in order. Cost: O(number of axes).
Array.to_list
Section titled “Array.to_list”fn to_list(self) -> List[Float]The elements in row-major order.
Cost: O(1) when is_compact, else O(n): a copy.
Array.compact
Section titled “Array.compact”fn compact(self) -> Array ensures it.shape == self.shapeThe same elements, row-major.
Cost: O(1) when is_compact, else O(n): a copy.
Array.get
Section titled “Array.get”fn get(self, idx: List[I64]) -> Option[Float]The element at idx, one index per axis. None when idx is out of bounds.
Cost: O(number of axes).
Array.set
Section titled “Array.set”fn set(mut self, idx: List[I64], x: Float) -> BoolElement idx set to x. Returns false, and changes nothing, when idx is out of bounds.
Cost: O(number of axes) when nothing else holds the data, else O(n): a copy.
Array.reshape
Section titled “Array.reshape”fn reshape(self, shape: List[Length]) -> Array requires count(shape) == count(...self) ensures it.shape == shapeThe same elements in shape, which must hold as many.
Cost: O(number of axes) when is_compact (a view), else O(n): a copy.
Array.transpose
Section titled “Array.transpose”fn transpose(self) -> Array ensures it.shape == self.shape.reversed()The axes in reverse order: element (i, j) of the result is element (j, i) of a matrix.
Cost: O(number of axes): a view.
Array.slice
Section titled “Array.slice”fn slice(self, axis: Length, start: Length, stop: Length) -> Array requires axis.to_int() < len(self.shape) ensures len(it.shape) == len(self.shape)Indices start to below stop of axis axis, both clamped to the axis.
Cost: O(number of axes): a view.
Array.row
Section titled “Array.row”fn row(self, i: Length) -> Array requires len(self.shape) > 0 && i < self.shape[0] ensures len(it.shape) == len(self.shape) - 1Index i of axis 0, one axis fewer: a matrix’s row i.
Cost: O(number of axes): a view.
Array.map
Section titled “Array.map”fn map(self, f: (Float) -> Float) -> Array ensures it.shape == self.shapef of each element.
Cost: O(n) calls of f.
Array.pair
Section titled “Array.pair”fn pair(self, other: Array) -> (List[Float], List[Float], List[Length])The elements of self and other in row-major order of the shape they broadcast to.
Array.zip_with
Section titled “Array.zip_with”fn zip_with(self, other: Array, f: (Float, Float) -> Float) -> Array requires broadcasts(self.shape, other.shape)f of each pair of elements, broadcast: the shapes must broadcast together.
Cost: O(n) calls of f, n the size of the broadcast shape.
Array.add
Section titled “Array.add”fn add(self, other: Array) -> Array requires broadcasts(self.shape, other.shape) requires self.within(add_bound()) && other.within(add_bound())Element by element, broadcast. Both within(add_bound()), so no sum overflows.
Cost: O(n).
Array.sub
Section titled “Array.sub”fn sub(self, other: Array) -> Array requires broadcasts(self.shape, other.shape) requires self.within(add_bound()) && other.within(add_bound())Element by element, broadcast. Both within(add_bound()), so no difference overflows.
Cost: O(n).
Array.mul
Section titled “Array.mul”fn mul(self, other: Array) -> Array requires broadcasts(self.shape, other.shape) requires self.within(factor_bound()) && other.within(factor_bound())Element by element, broadcast. Both within(factor_bound()), so no product overflows.
Cost: O(n).
Array.div
Section titled “Array.div”fn div(self, other: Array) -> Array requires broadcasts(self.shape, other.shape) requires self.within(factor_bound()) && other.at_least(1.0 / factor_bound())Element by element, broadcast. self within(factor_bound()) and other at_least its inverse (so no element of other is 0.0): no quotient overflows.
Cost: O(n).
Array.neg
Section titled “Array.neg”fn neg(self) -> Array ensures it.shape == self.shapeEach element negated. Cost: O(n).
Array.scale
Section titled “Array.scale”fn scale(self, k: Float) -> Array requires self.within(product_bound() / std.array.max(abs(k), float(1))) ensures it.shape == self.shapeEach element times k. Every element times abs(k) (or 1.0, if larger) at most product_bound in magnitude.
Cost: O(n).
Array.shift
Section titled “Array.shift”fn shift(self, k: Float) -> Array requires self.within(add_bound()) && abs(k) <= add_bound() ensures it.shape == self.shapek added to each element. Every element and k at most add_bound in magnitude.
Cost: O(n).
Array.sum
Section titled “Array.sum”fn sum(self) -> Float requires self.size() < 1 << 40 && self.within(sum_bound())The sum of the elements; 0.0 when there are none. They are added in row-major order into four interleaved partial sums, then those four, so the result can differ from a left-to-right sum in the last bits. At most 2^40 elements, within(sum_bound()), so no partial sum overflows.
Cost: O(n).
Array.max
Section titled “Array.max”fn max(self) -> Option[Float]The largest element, None when there are none.
Cost: O(n).
Array.min
Section titled “Array.min”fn min(self) -> Option[Float]The smallest element, None when there are none.
Cost: O(n).
Array.argmax
Section titled “Array.argmax”fn argmax(self) -> Option[Length]Where the largest element is, in row-major order (the first, for ties); None when there are none.
Cost: O(n).
Array.sum_axis
Section titled “Array.sum_axis”fn sum_axis(self, axis: Length) -> Array requires axis.to_int() < len(self.shape) requires self.shape[axis.to_int()] < 1 << 40 && self.within(sum_bound()) ensures it.shape == without_axis(self.shape, axis)The sums along axis axis: the result has the other axes. A matrix’s sum_axis(0) sums each column. The axis at most 2^40 long and the array within(sum_bound()), as sum requires.
Cost: O(n).
Array.max_axis
Section titled “Array.max_axis”fn max_axis(self, axis: Length) -> Array requires axis.to_int() < len(self.shape) && self.shape[axis.to_int()] > 0 ensures it.shape == without_axis(self.shape, axis)The largest elements along axis axis, which must not be empty.
Cost: O(n).
Array.argmax_axis
Section titled “Array.argmax_axis”fn argmax_axis(self, axis: Length) -> ArrayI64 requires axis.to_int() < len(self.shape) && self.shape[axis.to_int()] > 0 ensures it.shape == without_axis(self.shape, axis)Where the largest element is along axis axis (the first, for ties), for each position of the other axes, in row-major order of those. The axis must not be empty.
Cost: O(n).
Array.dot
Section titled “Array.dot”fn dot(self, other: Array) -> Float requires len(self.shape) == 1 && self.shape == other.shape requires self.size() < 1 << 40 && self.within(dot_bound()) && other.within(dot_bound())The dot product of two arrays of one axis and the same length, added as sum adds. At most 2^40 elements, both within(dot_bound()), so no partial sum overflows.
Cost: O(n).
Array.matmul
Section titled “Array.matmul”fn matmul(self, other: Array) -> Array requires len(self.shape) == 2 && len(other.shape) == 2 && self.shape[1] == other.shape[0] requires self.shape[1] < 1 << 40 && self.within(dot_bound()) && other.within(dot_bound()) ensures it.shape == [self.shape[0], other.shape[1]]The matrix product of an m×k and a k×n matrix: an m×n matrix, added as sum adds. At most 2^40 columns of self, both within(dot_bound()), as dot requires.
Cost: O(m·k·n) multiply-adds, one dot product of a row of self and a column of other per element, after a row-major copy of other’s transpose (O(k·n)) so both are read in order.
Array.mean
Section titled “Array.mean”fn mean(self) -> Float requires count(...self) > 0 requires self.size() < 1 << 40 && self.within(sum_bound())The mean of the elements. The array must not be empty, and bounded as sum requires.
Cost: O(n).
Array.mean_axis
Section titled “Array.mean_axis”fn mean_axis(self, axis: Length) -> Array requires axis.to_int() < len(self.shape) && self.shape[axis.to_int()] > 0 requires self.shape[axis.to_int()] < 1 << 40 && self.within(sum_bound()) ensures it.shape == without_axis(self.shape, axis)The means along axis axis, which must not be empty, and bounded as sum_axis requires.
Cost: O(n).
ArrayI64.full
Section titled “ArrayI64.full”fn full(shape: List[Length], x: I64) -> ArrayI64 ensures it.shape == shapeAn array of shape with every element x.
Cost: O(n).
ArrayI64.zeros
Section titled “ArrayI64.zeros”fn zeros(shape: List[Length]) -> ArrayI64 ensures it.shape == shapeAn array of shape filled with 0.
Cost: O(n).
ArrayI64.ones
Section titled “ArrayI64.ones”fn ones(shape: List[Length]) -> ArrayI64 ensures it.shape == shapeAn array of shape filled with 1.
Cost: O(n).
ArrayI64.arange
Section titled “ArrayI64.arange”fn arange(n: Length) -> ArrayI64 ensures it.shape == [n]0, 1, …, up to n - 1: one axis of length n.
Cost: O(n).
ArrayI64.from_list
Section titled “ArrayI64.from_list”fn from_list(xs: List[I64]) -> ArrayI64One axis holding xs.
Cost: O(1): the array shares the list.
ArrayI64.from_rows
Section titled “ArrayI64.from_rows”fn from_rows(rows: List[List[I64]]) -> Option[ArrayI64]Two axes, one row per list. None when the rows differ in length.
Cost: O(n).
ArrayI64.dense
Section titled “ArrayI64.dense”fn dense(data: List[I64], shape: List[Length]) -> ArrayI64 ensures it.shape == shapeA row-major array of shape over data.
ArrayI64.ndim
Section titled “ArrayI64.ndim”fn ndim(self) -> LengthThe number of axes. Cost: O(1).
ArrayI64.size
Section titled “ArrayI64.size”fn size(self) -> LengthThe number of elements. Cost: O(number of axes).
ArrayI64.is_compact
Section titled “ArrayI64.is_compact”fn is_compact(self) -> BoolWhether the array reads its data row-major from its start to its end, so the data is its elements in order. Cost: O(number of axes).
ArrayI64.to_list
Section titled “ArrayI64.to_list”fn to_list(self) -> List[I64]The elements in row-major order.
Cost: O(1) when is_compact, else O(n): a copy.
ArrayI64.compact
Section titled “ArrayI64.compact”fn compact(self) -> ArrayI64 ensures it.shape == self.shapeThe same elements, row-major.
Cost: O(1) when is_compact, else O(n): a copy.
ArrayI64.get
Section titled “ArrayI64.get”fn get(self, idx: List[I64]) -> Option[I64]The element at idx, one index per axis. None when idx is out of bounds.
Cost: O(number of axes).
ArrayI64.set
Section titled “ArrayI64.set”fn set(mut self, idx: List[I64], x: I64) -> BoolElement idx set to x. Returns false, and changes nothing, when idx is out of bounds.
Cost: O(number of axes) when nothing else holds the data, else O(n): a copy.
ArrayI64.reshape
Section titled “ArrayI64.reshape”fn reshape(self, shape: List[Length]) -> ArrayI64 requires count(shape) == count(...self) ensures it.shape == shapeThe same elements in shape, which must hold as many.
Cost: O(number of axes) when is_compact (a view), else O(n): a copy.
ArrayI64.transpose
Section titled “ArrayI64.transpose”fn transpose(self) -> ArrayI64 ensures it.shape == self.shape.reversed()The axes in reverse order: element (i, j) of the result is element (j, i) of a matrix.
Cost: O(number of axes): a view.
ArrayI64.slice
Section titled “ArrayI64.slice”fn slice(self, axis: Length, start: Length, stop: Length) -> ArrayI64 requires axis.to_int() < len(self.shape) ensures len(it.shape) == len(self.shape)Indices start to below stop of axis axis, both clamped to the axis.
Cost: O(number of axes): a view.
ArrayI64.row
Section titled “ArrayI64.row”fn row(self, i: Length) -> ArrayI64 requires len(self.shape) > 0 && i < self.shape[0] ensures len(it.shape) == len(self.shape) - 1Index i of axis 0, one axis fewer: a matrix’s row i.
Cost: O(number of axes): a view.
ArrayI64.map
Section titled “ArrayI64.map”fn map(self, f: (I64) -> I64) -> ArrayI64 ensures it.shape == self.shapef of each element.
Cost: O(n) calls of f.
ArrayI64.pair
Section titled “ArrayI64.pair”fn pair(self, other: ArrayI64) -> (List[I64], List[I64], List[Length])The elements of self and other in row-major order of the shape they broadcast to.
ArrayI64.zip_with
Section titled “ArrayI64.zip_with”fn zip_with(self, other: ArrayI64, f: (I64, I64) -> I64) -> ArrayI64 requires broadcasts(self.shape, other.shape)f of each pair of elements, broadcast: the shapes must broadcast together.
Cost: O(n) calls of f, n the size of the broadcast shape.
ArrayI64.add
Section titled “ArrayI64.add”fn add(self, other: ArrayI64) -> ArrayI64 requires broadcasts(self.shape, other.shape)Element by element, broadcast, modulo 2^64. Cost: O(n).
ArrayI64.sub
Section titled “ArrayI64.sub”fn sub(self, other: ArrayI64) -> ArrayI64 requires broadcasts(self.shape, other.shape)Element by element, broadcast, modulo 2^64. Cost: O(n).
ArrayI64.mul
Section titled “ArrayI64.mul”fn mul(self, other: ArrayI64) -> ArrayI64 requires broadcasts(self.shape, other.shape)Element by element, broadcast, modulo 2^64. Cost: O(n).
ArrayI64.neg
Section titled “ArrayI64.neg”fn neg(self) -> ArrayI64 ensures it.shape == self.shapeEach element negated, modulo 2^64. Cost: O(n).
ArrayI64.scale
Section titled “ArrayI64.scale”fn scale(self, k: I64) -> ArrayI64 ensures it.shape == self.shapeEach element times k, modulo 2^64.
Cost: O(n).
ArrayI64.shift
Section titled “ArrayI64.shift”fn shift(self, k: I64) -> ArrayI64 ensures it.shape == self.shapek added to each element, modulo 2^64.
Cost: O(n).
ArrayI64.sum
Section titled “ArrayI64.sum”fn sum(self) -> I64The sum of the elements, modulo 2^64; 0 when there are none. Cost: O(n).
ArrayI64.max
Section titled “ArrayI64.max”fn max(self) -> Option[I64]The largest element, None when there are none.
Cost: O(n).
ArrayI64.min
Section titled “ArrayI64.min”fn min(self) -> Option[I64]The smallest element, None when there are none.
Cost: O(n).
ArrayI64.argmax
Section titled “ArrayI64.argmax”fn argmax(self) -> Option[Length]Where the largest element is, in row-major order (the first, for ties); None when there are none.
Cost: O(n).
ArrayI64.sum_axis
Section titled “ArrayI64.sum_axis”fn sum_axis(self, axis: Length) -> ArrayI64 requires axis.to_int() < len(self.shape) ensures it.shape == without_axis(self.shape, axis)The sums along axis axis: the result has the other axes. A matrix’s sum_axis(0) sums each column.
Cost: O(n).
ArrayI64.max_axis
Section titled “ArrayI64.max_axis”fn max_axis(self, axis: Length) -> ArrayI64 requires axis.to_int() < len(self.shape) && self.shape[axis.to_int()] > 0 ensures it.shape == without_axis(self.shape, axis)The largest elements along axis axis, which must not be empty.
Cost: O(n).
ArrayI64.argmax_axis
Section titled “ArrayI64.argmax_axis”fn argmax_axis(self, axis: Length) -> ArrayI64 requires axis.to_int() < len(self.shape) && self.shape[axis.to_int()] > 0 ensures it.shape == without_axis(self.shape, axis)Where the largest element is along axis axis (the first, for ties), for each position of the other axes, in row-major order of those. The axis must not be empty.
Cost: O(n).
ArrayI64.dot
Section titled “ArrayI64.dot”fn dot(self, other: ArrayI64) -> I64 requires len(self.shape) == 1 && self.shape == other.shapeThe dot product of two arrays of one axis and the same length, modulo 2^64. Cost: O(n).
ArrayI64.matmul
Section titled “ArrayI64.matmul”fn matmul(self, other: ArrayI64) -> ArrayI64 requires len(self.shape) == 2 && len(other.shape) == 2 && self.shape[1] == other.shape[0] ensures it.shape == [self.shape[0], other.shape[1]]The matrix product of an m×k and a k×n matrix: an m×n matrix, modulo 2^64.
Cost: O(m·k·n) multiply-adds, one dot product of a row of self and a column of other per element, after a row-major copy of other’s transpose (O(k·n)) so both are read in order.
ArrayI64.to_float
Section titled “ArrayI64.to_float”fn to_float(self) -> Array ensures it.shape == self.shapeThe elements as floats (Array), each the nearest Float.
Cost: O(n).
ArrayI32.full
Section titled “ArrayI32.full”fn full(shape: List[Length], x: I32) -> ArrayI32 ensures it.shape == shapeAn array of shape with every element x.
Cost: O(n).
ArrayI32.zeros
Section titled “ArrayI32.zeros”fn zeros(shape: List[Length]) -> ArrayI32 ensures it.shape == shapeAn array of shape filled with 0.
Cost: O(n).
ArrayI32.ones
Section titled “ArrayI32.ones”fn ones(shape: List[Length]) -> ArrayI32 ensures it.shape == shapeAn array of shape filled with 1.
Cost: O(n).
ArrayI32.arange
Section titled “ArrayI32.arange”fn arange(n: Length) -> ArrayI32 ensures it.shape == [n]0, 1, …, up to n - 1 (modulo 2^32): one axis of length n.
Cost: O(n).
ArrayI32.from_list
Section titled “ArrayI32.from_list”fn from_list(xs: List[I32]) -> ArrayI32One axis holding xs.
Cost: O(1): the array shares the list.
ArrayI32.from_rows
Section titled “ArrayI32.from_rows”fn from_rows(rows: List[List[I32]]) -> Option[ArrayI32]Two axes, one row per list. None when the rows differ in length.
Cost: O(n).
ArrayI32.dense
Section titled “ArrayI32.dense”fn dense(data: List[I32], shape: List[Length]) -> ArrayI32 ensures it.shape == shapeA row-major array of shape over data.
ArrayI32.ndim
Section titled “ArrayI32.ndim”fn ndim(self) -> LengthThe number of axes. Cost: O(1).
ArrayI32.size
Section titled “ArrayI32.size”fn size(self) -> LengthThe number of elements. Cost: O(number of axes).
ArrayI32.is_compact
Section titled “ArrayI32.is_compact”fn is_compact(self) -> BoolWhether the array reads its data row-major from its start to its end, so the data is its elements in order. Cost: O(number of axes).
ArrayI32.to_list
Section titled “ArrayI32.to_list”fn to_list(self) -> List[I32]The elements in row-major order.
Cost: O(1) when is_compact, else O(n): a copy.
ArrayI32.compact
Section titled “ArrayI32.compact”fn compact(self) -> ArrayI32 ensures it.shape == self.shapeThe same elements, row-major.
Cost: O(1) when is_compact, else O(n): a copy.
ArrayI32.get
Section titled “ArrayI32.get”fn get(self, idx: List[I64]) -> Option[I32]The element at idx, one index per axis. None when idx is out of bounds.
Cost: O(number of axes).
ArrayI32.set
Section titled “ArrayI32.set”fn set(mut self, idx: List[I64], x: I32) -> BoolElement idx set to x. Returns false, and changes nothing, when idx is out of bounds.
Cost: O(number of axes) when nothing else holds the data, else O(n): a copy.
ArrayI32.reshape
Section titled “ArrayI32.reshape”fn reshape(self, shape: List[Length]) -> ArrayI32 requires count(shape) == count(...self) ensures it.shape == shapeThe same elements in shape, which must hold as many.
Cost: O(number of axes) when is_compact (a view), else O(n): a copy.
ArrayI32.transpose
Section titled “ArrayI32.transpose”fn transpose(self) -> ArrayI32 ensures it.shape == self.shape.reversed()The axes in reverse order: element (i, j) of the result is element (j, i) of a matrix.
Cost: O(number of axes): a view.
ArrayI32.slice
Section titled “ArrayI32.slice”fn slice(self, axis: Length, start: Length, stop: Length) -> ArrayI32 requires axis.to_int() < len(self.shape) ensures len(it.shape) == len(self.shape)Indices start to below stop of axis axis, both clamped to the axis.
Cost: O(number of axes): a view.
ArrayI32.row
Section titled “ArrayI32.row”fn row(self, i: Length) -> ArrayI32 requires len(self.shape) > 0 && i < self.shape[0] ensures len(it.shape) == len(self.shape) - 1Index i of axis 0, one axis fewer: a matrix’s row i.
Cost: O(number of axes): a view.
ArrayI32.map
Section titled “ArrayI32.map”fn map(self, f: (I32) -> I32) -> ArrayI32 ensures it.shape == self.shapef of each element.
Cost: O(n) calls of f.
ArrayI32.pair
Section titled “ArrayI32.pair”fn pair(self, other: ArrayI32) -> (List[I32], List[I32], List[Length])The elements of self and other in row-major order of the shape they broadcast to.
ArrayI32.zip_with
Section titled “ArrayI32.zip_with”fn zip_with(self, other: ArrayI32, f: (I32, I32) -> I32) -> ArrayI32 requires broadcasts(self.shape, other.shape)f of each pair of elements, broadcast: the shapes must broadcast together.
Cost: O(n) calls of f, n the size of the broadcast shape.
ArrayI32.add
Section titled “ArrayI32.add”fn add(self, other: ArrayI32) -> ArrayI32 requires broadcasts(self.shape, other.shape)Element by element, broadcast, modulo 2^32. Cost: O(n).
ArrayI32.sub
Section titled “ArrayI32.sub”fn sub(self, other: ArrayI32) -> ArrayI32 requires broadcasts(self.shape, other.shape)Element by element, broadcast, modulo 2^32. Cost: O(n).
ArrayI32.mul
Section titled “ArrayI32.mul”fn mul(self, other: ArrayI32) -> ArrayI32 requires broadcasts(self.shape, other.shape)Element by element, broadcast, modulo 2^32. Cost: O(n).
ArrayI32.neg
Section titled “ArrayI32.neg”fn neg(self) -> ArrayI32 ensures it.shape == self.shapeEach element negated, modulo 2^32. Cost: O(n).
ArrayI32.scale
Section titled “ArrayI32.scale”fn scale(self, k: I32) -> ArrayI32 ensures it.shape == self.shapeEach element times k, modulo 2^32.
Cost: O(n).
ArrayI32.shift
Section titled “ArrayI32.shift”fn shift(self, k: I32) -> ArrayI32 ensures it.shape == self.shapek added to each element, modulo 2^32.
Cost: O(n).
ArrayI32.sum
Section titled “ArrayI32.sum”fn sum(self) -> I64The sum of the elements as an I64, modulo 2^64; 0 when there are none.
Cost: O(n).
ArrayI32.max
Section titled “ArrayI32.max”fn max(self) -> Option[I32]The largest element, None when there are none.
Cost: O(n).
ArrayI32.min
Section titled “ArrayI32.min”fn min(self) -> Option[I32]The smallest element, None when there are none.
Cost: O(n).
ArrayI32.argmax
Section titled “ArrayI32.argmax”fn argmax(self) -> Option[Length]Where the largest element is, in row-major order (the first, for ties); None when there are none.
Cost: O(n).
ArrayI32.sum_axis
Section titled “ArrayI32.sum_axis”fn sum_axis(self, axis: Length) -> ArrayI64 requires axis.to_int() < len(self.shape) ensures it.shape == without_axis(self.shape, axis)The sums along axis axis: the result has the other axes. A matrix’s sum_axis(0) sums each column.
Cost: O(n).
ArrayI32.max_axis
Section titled “ArrayI32.max_axis”fn max_axis(self, axis: Length) -> ArrayI32 requires axis.to_int() < len(self.shape) && self.shape[axis.to_int()] > 0 ensures it.shape == without_axis(self.shape, axis)The largest elements along axis axis, which must not be empty.
Cost: O(n).
ArrayI32.argmax_axis
Section titled “ArrayI32.argmax_axis”fn argmax_axis(self, axis: Length) -> ArrayI64 requires axis.to_int() < len(self.shape) && self.shape[axis.to_int()] > 0 ensures it.shape == without_axis(self.shape, axis)Where the largest element is along axis axis (the first, for ties), for each position of the other axes, in row-major order of those. The axis must not be empty.
Cost: O(n).
ArrayI32.dot
Section titled “ArrayI32.dot”fn dot(self, other: ArrayI32) -> I32 requires len(self.shape) == 1 && self.shape == other.shapeThe dot product of two arrays of one axis and the same length, modulo 2^32. Cost: O(n).
ArrayI32.matmul
Section titled “ArrayI32.matmul”fn matmul(self, other: ArrayI32) -> ArrayI32 requires len(self.shape) == 2 && len(other.shape) == 2 && self.shape[1] == other.shape[0] ensures it.shape == [self.shape[0], other.shape[1]]The matrix product of an m×k and a k×n matrix: an m×n matrix, modulo 2^32.
Cost: O(m·k·n) multiply-adds, one dot product of a row of self and a column of other per element, after a row-major copy of other’s transpose (O(k·n)) so both are read in order.
ArrayI32.to_float
Section titled “ArrayI32.to_float”fn to_float(self) -> Array ensures it.shape == self.shapeThe elements as floats (Array), each the nearest Float.
Cost: O(n).
ArrayI32.to_i64
Section titled “ArrayI32.to_i64”fn to_i64(self) -> ArrayI64 ensures it.shape == self.shapeThe elements as 64-bit integers (ArrayI64).
Cost: O(n).
Functions
Section titled “Functions”fn ones(shape: List[Length]) -> Array ensures it.shape == shapeAn array of 64-bit floats of shape filled with 1.0.
Cost: O(n).
fn full(shape: List[Length], x: Float) -> Array ensures it.shape == shapeAn array of 64-bit floats of shape with every element x.
Cost: O(n).
arange
Section titled “arange”fn arange(n: Length) -> Array ensures it.shape == [n]0.0, 1.0, …, up to n - 1: one axis of length n.
Cost: O(n).
from_list
Section titled “from_list”fn from_list(xs: List[Float]) -> ArrayOne axis holding xs.
Cost: O(1): the array shares the list.
from_rows
Section titled “from_rows”fn from_rows(rows: List[List[Float]]) -> Option[Array]Two axes, one row per list. None when the rows differ in length.
Cost: O(n).
fn count(shape: List[Length]) -> LengthThe number of elements of an array of shape shape: the product of its
lengths, 1 for no axes. A product past 2^62 gives 2^62, more than any list
holds.
Cost: O(number of axes).
row_major
Section titled “row_major”fn row_major(shape: List[Length]) -> List[I64] ensures len(it) == len(shape)The strides of a row-major array of shape shape: the last axis is 1,
each axis before it the product of the lengths after it.
Cost: O(number of axes).
in_bounds
Section titled “in_bounds”fn in_bounds(shape: List[Length], idx: List[I64]) -> BoolWhether idx names an element of an array of shape: one index per axis,
each from 0 to below its length.
broadcast_shape
Section titled “broadcast_shape”fn broadcast_shape(a: List[Length], b: List[Length]) -> Option[List[Length]]The shape two arrays broadcast to, NumPy’s rule: lengths are compared from
the last axis, and a length of 1 stretches to the other’s. None when two
lengths differ and neither is 1.
Cost: O(number of axes).
broadcasts
Section titled “broadcasts”fn broadcasts(a: List[Length], b: List[Length]) -> BoolWhether a and b broadcast together (broadcast_shape is not None).
add_bound
Section titled “add_bound”fn add_bound() -> Float2^1022, about 4.5e307: the largest magnitude of the operands of add,
sub and shift, so their sums stay finite.
factor_bound
Section titled “factor_bound”fn factor_bound() -> Float2^511, about 6.7e153: the largest magnitude of the operands of mul, and
of div’s dividend; a divisor’s are at least its inverse.
product_bound
Section titled “product_bound”fn product_bound() -> Float2^1023, about 9.0e307: the most scale’s products can reach.
sum_bound
Section titled “sum_bound”fn sum_bound() -> Float2^960, about 9.7e288: the largest magnitude sum, mean and the
reductions along an axis take, so that 2^40 elements add up to a finite
number.
without_axis
Section titled “without_axis”fn without_axis(shape: List[Length], axis: Length) -> List[Length]shape without axis axis.
Cost: O(number of axes).
fn zeros(shape: List[Length]) -> Array ensures it.shape == shapeAn array of 64-bit floats of shape filled with 0.0.
Cost: O(n).
dot_bound
Section titled “dot_bound”fn dot_bound() -> Float2^480, about 3.1e144: the largest magnitude of the operands of dot and
matmul, so that 2^40 products add up to a finite number.