docs

You are reading the docs for v0.1.0. Nabi 0.1.0 is no longer supported. Latest release: v0.1.3 →

core::array

On this page

Clone / Copy / Default / Debug / PartialEq / Eq / Hash for fixed arrays [T; N], element-wise, and PartialOrd / Ord, lexicographic; and Array<T, N>, the n-dimensional array.

Structs

ArraySource

pub struct Array<T, const N: usize>

An N-dimensional array of T (N is the rank): a shared element buffer seen through a shape and per-axis strides. Copies share the buffer, as with List; a basic index (a[1:, ::2], a.t(), a.reshape(..) of a contiguous array) is a view of the same elements, with no copy. Elements of a type with no GC reference sit in a buffer the collector never scans; from 8 KiB on it is 64-byte aligned and never moves.

In an Array slot a nested list literal builds one: [[1.0, 2.0], [3.0, 4.0]] is a 2 x 2 Array<f64, 2>; rows must agree in length.

  • pub const fn full(shape: [usize; N], v: T) -> Array<T, N>§

    Source

    Every element v.

  • pub const fn from_fn(shape: [usize; N], f: fn([usize; N]) -> T) -> Array<T, N>§

    Source

    Element at index idx is f(idx), filled in row-major order.

  • pub const fn from_list(xs: List<T>, shape: [usize; N]) -> Array<T, N>§

    Source

    The elements of xs in row-major order as an array of shape; panics unless the counts agree.

  • pub const fn view_list(xs: List<T>, shape: [usize; N]) -> Array<T, N>§

    Source

    The list's elements in row-major order as an array of shape, sharing its buffer: no copy. A write through either shows in the other while the list keeps that buffer; a push past its capacity moves the list to a new one, as with xs[a:b]. Panics unless the counts agree (from_list copies).

  • pub const fn as_list(self) -> List<T> | none§

    Source

    The elements as a list sharing this array's buffer, no copy, when they are contiguous from its start (a fresh array, a view_list one); else none, and to_list() copies. The list's first push moves it to a buffer of its own.

  • pub const fn shape(self) -> [usize; N]§

    Source

    The length of each axis.

  • pub const fn strides(self) -> [isize; N]§

    Source

    The step, in elements, between neighbours along each axis.

  • pub const fn ndim(self) -> usize§

    Source

    The rank N.

  • pub const fn len(self) -> usize§

    Source

    The element count (the product of the shape).

  • pub const fn is_empty(self) -> bool§

    Source

    true when some axis has length 0.

  • pub const fn is_contiguous(self) -> bool§

    Source

    true when the elements are adjacent in row-major order, as a fresh array's are.

  • pub const fn get(self, idx: [usize; N]) -> T | none§

    Source

    The element at idx, none when an index is past its axis.

  • pub const fn at(self, idx: [usize; N]) -> T§

    Source

    The element at idx; an index past its axis panics.

  • pub const fn set(self, idx: [usize; N], v: T)§

    Source

    Store v at idx; an index past its axis panics.

  • pub const fn fill(self, v: T)§

    Source

    Overwrite every element with v, in place (through a view too).

  • pub const fn iter(self) -> ArrayIter<T, N>§

    Source

    The elements in row-major order.

  • pub const fn to_list(self) -> List<T>§

    Source

    The elements in row-major order, as a new list.

  • pub const fn copy(self) -> Array<T, N>§

    Source

    A contiguous copy with its own buffer.

  • pub const fn map<U>(self, f: fn(T) -> U) -> Array<U, N>§

    Source

    f of each element, as a new array of the same shape.

  • pub const fn reshape<const M: usize>(self, shape: [usize; M]) -> Array<T, M>§

    Source

    The same elements as an array of shape: a view when this one is contiguous, else a contiguous copy first. Panics unless the element counts agree.

  • pub const fn t(self) -> Array<T, N>§

    Source

    The axes in reverse order, a view (a.t()[i, j] is a[j, i]).

  • pub const fn permute(self, axes: [usize; N]) -> Array<T, N>§

    Source

    The axes in the order axes names them, a view; axes must name each axis once.

  • pub const fn broadcast_to(self, shape: [usize; N]) -> Array<T, N>§

    Source

    This array stretched to shape without a copy: an axis of length 1 repeats (stride 0), the others must match. Panics, naming both shapes, when they do not.

  • pub unsafe fn as_ptr(self) -> ConstPtr<T>§

    Source

    The first element's address. It stays put while the array is reachable when the buffer is 8 KiB or more (64-byte aligned then); pin a smaller one. Strides are in elements.

  • pub const fn assign(self, src: Array<T, N>)§

    Source

    Copy src into this array element by element; src stretches (broadcast_to) to this shape.

  • pub const fn masked(self, mask: Array<bool, N>) -> Array<T, 1>§

    Source

    The elements where mask is true, in row-major order, as a new rank-1 array. The shapes must match.

  • pub const fn fill_masked(self, mask: Array<bool, N>, v: T)§

    Source

    Store v where mask is true. The shapes must match.

  • pub const fn assign_masked(self, mask: Array<bool, N>, vals: Array<T, 1>)§

    Source

    Store the elements of vals in order where mask is true; vals holds one per true.

  • pub const fn zeros(shape: [usize; N]) -> Array<T, N>§

    Source

    Every element 0.

  • pub const fn ones(shape: [usize; N]) -> Array<T, N>§

    Source

    Every element 1.

  • pub const fn is_close(self, other: Array<T, N>, rel: f64 | none = none, abs: f64 = 0.0, equal_nan: bool = false) -> Array<bool, N>§

    Source

    Whether each element is close to other's (broadcast to one shape): |a - b| <= max(rel * max(|a|, |b|), abs), or equal. Symmetric in a and b. rel defaults to the element type's (f64: 1e-9, as Python's math.isclose; f32: 2^-20); pass abs when values near zero must match. NaN is close to nothing unless equal_nan.

    nabi
    use core::array::Vector;
    
    let a: Vector<f64> = [1.0, 2.0, 0.0];
    let b: Vector<f64> = [1.0 + 1e-12, 2.1, 1e-20];
    assert(a.is_close(b).to_list() == [true, false, false]);
    assert(a.is_close(b, abs: 1e-12).to_list() == [true, false, true]);
    
  • pub const fn all_close(self, other: Array<T, N>, rel: f64 | none = none, abs: f64 = 0.0, equal_nan: bool = false) -> bool§

    Source

    Whether every element is close to other's (see is_close); the shapes must broadcast. NumPy: np.allclose, but symmetric and relative by default.

    nabi
    use core::array::Matrix;
    
    let m: Matrix<f64> = [[0.1, 0.2], [0.3, 0.4]];
    assert((m .* 3.0).all_close(m .+ m .+ m));
    assert(!m.all_close(m .+ 1e-6));
    
  • pub const fn cast<U: From<T>>(self) -> Array<U, N>§

    Source

    Each element converted to U without loss (i32 to f64, u8 to i64, ..), as a new array of the same shape. NumPy: a.astype(U). A lossy conversion is a map: a.map(|x: f64| -> i64 { x as i64 }).

    nabi
    use core::array::Vector;
    
    let v: Vector<i32> = [1, 2, 3];
    let f: Vector<f64> = v.cast();
    assert(f.mean() == 2.0);
    
  • pub const unsafe fn from_raw_parts(buf: GcBuf<T>, off: usize, shape: [usize; N], strides: [isize; N]) -> Array<T, N>§

    Source

    An array over buf: the element at idx is `buf[off + sum(idx[k]

    • strides[k])]. No copy and no check: every index in the shape must name a slot of buf. For kernels that keep their data in a GcBuf` and want the array methods on a run of it.
    nabi
    use bare::mem::GcBuf;
    use core::array::Array;
    
    let b: GcBuf<f64> = unsafe { GcBuf::new(4) };
    unsafe { b.set(1, 2.0); b.set(2, 3.0); }
    let v: Array<f64, 1> = unsafe { Array::from_raw_parts(b, 1, [2], [1]) };
    assert(v.sum() == 5.0);
    
  • pub const unsafe fn raw_parts(self) -> (GcBuf<T>, usize, [usize; N], [isize; N])§

    Source

    The buffer, the offset of the first element, the shape and the strides: what from_raw_parts takes.

  • pub const fn sum(self) -> T§

    Source

    The sum of every element (0 when empty), pairwise: runs of 128 summed with eight accumulators, halves added, so the float error grows with log2 of the count. NumPy: a.sum().

    nabi
    use core::array::Matrix;
    
    let m: Matrix<f64> = [[1, 2, 3], [4, 5, 6]];
    assert(m.sum() == 21.0);
    
  • pub const fn sum_along(self, axis: usize) -> Array<T, { N - 1 }>§

    Source

    The sum of each lane along axis; that axis is dropped. Panics for an axis past the rank. NumPy: a.sum(axis=k).

    nabi
    use core::array::Matrix;
    
    let m: Matrix<f64> = [[1, 2, 3], [4, 5, 6]];
    assert(m.sum_along(0).to_list() == [5.0, 7.0, 9.0]);
    assert(m.sum_along(1).to_list() == [6.0, 15.0]);
    
  • pub const fn cumsum(self) -> Array<T, 1>§

    Source

    The running sums in row-major order, as a rank-1 array (the last is sum()). NumPy: np.cumsum(a).

    nabi
    use core::array::Vector;
    
    let v: Vector<i64> = [1, 2, 3, 4];
    assert(v.cumsum().to_list() == [1, 3, 6, 10]);
    
  • pub const fn cumsum_along(self, axis: usize) -> Array<T, N>§

    Source

    The running sums of each lane along axis, same shape. NumPy: np.cumsum(a, axis=k).

    nabi
    use core::array::Matrix;
    
    let m: Matrix<i64> = [[1, 2], [3, 4]];
    assert(m.cumsum_along(0).to_list() == [1, 2, 4, 6]);
    assert(m.cumsum_along(1).to_list() == [1, 3, 3, 7]);
    
  • pub const fn prod(self) -> T§

    Source

    The product of every element (1 when empty), in row-major order. NumPy: a.prod().

    nabi
    use core::array::Vector;
    
    let v: Vector<i64> = [1, 2, 3, 4];
    assert(v.prod() == 24);
    
  • pub const fn prod_along(self, axis: usize) -> Array<T, { N - 1 }>§

    Source

    The product of each lane along axis; that axis is dropped.

  • pub const fn cumprod(self) -> Array<T, 1>§

    Source

    The running products in row-major order, as a rank-1 array. NumPy: np.cumprod(a).

  • pub const fn cumprod_along(self, axis: usize) -> Array<T, N>§

    Source

    The running products of each lane along axis, same shape.

  • pub const fn min(self) -> T | none§

    Source

    The smallest element; none when empty. A NaN anywhere makes the result NaN (nanmin skips it). NumPy: a.min().

    nabi
    use core::array::Vector;
    
    let v: Vector<f64> = [3, 1, 2];
    assert((v.min() ?? 0.0) == 1.0 && (v.max() ?? 0.0) == 3.0);
    let e: Vector<f64> = [];
    assert(e.min() is none);
    
  • pub const fn max(self) -> T | none§

    Source

    The largest element; none when empty. A NaN anywhere makes the result NaN (nanmax skips it). NumPy: a.max().

  • pub const fn nanmin(self) -> T | none§

    Source

    The smallest element that is not NaN; none when there is none. NumPy: np.nanmin(a).

    nabi
    use core::array::Vector;
    
    let v: Vector<f64> = [f64::NAN, 4.0, 2.0];
    assert((v.min() ?? 0.0).is_nan() && (v.nanmin() ?? 0.0) == 2.0);
    
  • pub const fn nanmax(self) -> T | none§

    Source

    The largest element that is not NaN; none when there is none.

  • pub const fn argmin(self) -> usize | none§

    Source

    The row-major index of the smallest element (the first of equals, or of the first NaN); none when empty. NumPy: a.argmin(); unravel turns it into an index per axis.

    nabi
    use core::array::Matrix;
    
    let m: Matrix<i64> = [[4, 1], [0, 9]];
    assert((m.argmin() ?? 9) == 2 && (m.argmax() ?? 9) == 3);
    assert(m.unravel(2) == [1, 0]);
    
  • pub const fn argmax(self) -> usize | none§

    Source

    The row-major index of the largest element (the first of equals, or of the first NaN); none when empty. NumPy: a.argmax().

  • pub const fn nanargmin(self) -> usize | none§

    Source

    argmin over the elements that are not NaN.

  • pub const fn nanargmax(self) -> usize | none§

    Source

    argmax over the elements that are not NaN.

  • pub const fn min_along(self, axis: usize) -> Array<T, { N - 1 }>§

    Source

    The smallest element of each lane along axis; that axis is dropped. A lane with a NaN gives NaN. Panics when the axis has length 0.

    nabi
    use core::array::Matrix;
    
    let m: Matrix<i64> = [[4, 1, 7], [0, 9, 2]];
    assert(m.min_along(0).to_list() == [0, 1, 2]);
    assert(m.max_along(1).to_list() == [7, 9]);
    
  • pub const fn max_along(self, axis: usize) -> Array<T, { N - 1 }>§

    Source

    The largest element of each lane along axis; that axis is dropped. A lane with a NaN gives NaN.

  • pub const fn nanmin_along(self, axis: usize) -> Array<T, { N - 1 }>§

    Source

    min_along leaving NaN out; a lane of only NaN gives NaN.

  • pub const fn nanmax_along(self, axis: usize) -> Array<T, { N - 1 }>§

    Source

    max_along leaving NaN out; a lane of only NaN gives NaN.

  • pub const fn argmin_along(self, axis: usize) -> Array<usize, { N - 1 }>§

    Source

    The index along axis of each lane's smallest element.

    nabi
    use core::array::Matrix;
    
    let m: Matrix<i64> = [[4, 1, 7], [0, 9, 2]];
    assert(m.argmin_along(1).to_list() == [1, 0]);
    assert(m.argmax_along(0).to_list() == [0, 1, 0]);
    
  • pub const fn argmax_along(self, axis: usize) -> Array<usize, { N - 1 }>§

    Source

    The index along axis of each lane's largest element.

  • pub const fn unravel(self, i: usize) -> [usize; N]§

    Source

    The index per axis of row-major position i (what argmin gives). Panics when i is past the last element. NumPy: np.unravel_index.

  • pub const fn mean(self) -> T§

    Source

    The mean of every element; NaN when empty. The pairwise sum over the count, then one pass adding the mean residual. NumPy: a.mean().

    nabi
    use core::array::Matrix;
    
    let m: Matrix<f64> = [[1, 2, 3], [4, 5, 6]];
    assert(m.mean() == 3.5);
    assert(m.mean_along(0).to_list() == [2.5, 3.5, 4.5]);
    
  • pub const fn mean_along(self, axis: usize) -> Array<T, { N - 1 }>§

    Source

    The mean of each lane along axis; that axis is dropped.

  • pub const fn var(self, ddof: usize = 0) -> T§

    Source

    The variance, dividing by n - ddof (ddof: 1 for the sample variance); NaN when n <= ddof. Corrected two-pass. NumPy: a.var(ddof=..).

    nabi
    use core::array::Vector;
    
    let v: Vector<f64> = [2, 4, 4, 4, 5, 5, 7, 9];
    assert(v.var() == 4.0 && v.std() == 2.0);
    assert(v.var(ddof: 1) == 32.0 / 7.0);
    
  • pub const fn std(self, ddof: usize = 0) -> T§

    Source

    The standard deviation, sqrt(var(ddof)).

  • pub const fn var_along(self, axis: usize, ddof: usize = 0) -> Array<T, { N - 1 }>§

    Source

    The variance of each lane along axis; that axis is dropped.

  • pub const fn std_along(self, axis: usize, ddof: usize = 0) -> Array<T, { N - 1 }>§

    Source

    The standard deviation of each lane along axis.

  • pub const fn nansum(self) -> T§

    Source

    The sum of the elements that are not NaN (0 when there are none). NumPy: np.nansum(a).

    nabi
    use core::array::Vector;
    
    let v: Vector<f64> = [1.0, f64::NAN, 2.0];
    assert(v.sum().is_nan() && v.nansum() == 3.0 && v.nanmean() == 1.5);
    
  • pub const fn nanmean(self) -> T§

    Source

    The mean of the elements that are not NaN; NaN when there are none. NumPy: np.nanmean(a).

  • pub const fn nansum_along(self, axis: usize) -> Array<T, { N - 1 }>§

    Source

    nansum of each lane along axis.

  • pub const fn nanmean_along(self, axis: usize) -> Array<T, { N - 1 }>§

    Source

    nanmean of each lane along axis.

  • pub const fn any(self) -> bool§

    Source

    true when some element is (false when empty). With a dot comparison it tests a condition: (a .> 0.0).any().

    nabi
    use core::array::Vector;
    
    let v: Vector<f64> = [1.0, -2.0, 3.0];
    assert((v .< 0.0).any() && !(v .< 0.0).all() && (v .< 0.0).count() == 1);
    
  • pub const fn all(self) -> bool§

    Source

    true when every element is (true when empty).

  • pub const fn count(self) -> usize§

    Source

    The number of true elements. NumPy: np.count_nonzero(a).

  • pub const fn any_along(self, axis: usize) -> Array<bool, { N - 1 }>§

    Source

    any of each lane along axis.

    nabi
    use core::array::Matrix;
    
    let m: Matrix<bool> = [[true, false], [false, false]];
    assert(m.any_along(1).to_list() == [true, false]);
    assert(m.all_along(0).to_list() == [false, false]);
    
  • pub const fn all_along(self, axis: usize) -> Array<bool, { N - 1 }>§

    Source

    all of each lane along axis.

  • pub const fn count_along(self, axis: usize) -> Array<usize, { N - 1 }>§

    Source

    count of each lane along axis.

  • pub const fn sorted_along(self, axis: usize) -> Array<T, N>§

    Source

    A copy with each lane along axis sorted ascending, stable; NaN last. NumPy: np.sort(a, axis).

    nabi
    use core::array::Matrix;
    
    let m: Matrix<f64> = [[3, 1, 2], [0, f64::NAN, -1]];
    let s = m.sorted_along(1);
    assert(s[0, :].to_list() == [1.0, 2.0, 3.0]);
    assert(s[1, 0] == -1.0 && s[1, 2].is_nan());
    assert(m.sorted_along(0)[0, :].to_list() == [0.0, 1.0, -1.0]);
    
  • pub const fn sort_along(self, axis: usize)§

    Source

    Each lane along axis sorted in place (through a view too).

  • pub const fn sorted(self) -> Array<T, N>§

    Source

    A sorted copy: a vector's elements ascending; for more axes, each lane of the last. NumPy: np.sort(a).

    nabi
    use core::array::Vector;
    
    let v: Vector<i64> = [3, 1, 2];
    assert(v.sorted().to_list() == [1, 2, 3]);
    v.sort();
    assert(v.to_list() == [1, 2, 3]);
    
  • pub const fn sort(self)§

    Source

    sorted in place.

  • pub const fn argsort_along(self, axis: usize) -> Array<usize, N>§

    Source

    The order that sorts each lane along axis: positions along it, stable, NaN last. NumPy: np.argsort(a, axis, kind="stable").

    nabi
    use core::array::Vector;
    
    let v: Vector<f64> = [0.5, -2.0, 9.0];
    assert(v.argsort_along(0).to_list() == [1, 0, 2]);
    
Trait implementations 11

ArrayIterSource

pub struct ArrayIter<T, const N: usize>

The elements of an array in row-major order, a row of the last axis at a time (all of them at once when contiguous).

Trait implementations 1
  • impl<T, const N: usize> Iterator for ArrayIter<T, N>

Type aliases

VectorSource

pub type Vector<T> = Array<T, 1>

A rank-1 array.

MatrixSource

pub type Matrix<T> = Array<T, 2>

A rank-2 array.

Functions

concatSource

pub const fn concat<T, const N: usize>(parts: List<Array<T, N>>, axis: usize = 0) -> Array<T, N>

The arrays of parts joined along axis, in order; every other axis must agree. Panics for no parts or an axis past the rank. NumPy: np.concatenate(parts, axis).

nabi
use core::array::{Matrix, concat};

let a: Matrix<i64> = [[1, 2], [3, 4]];
let b: Matrix<i64> = [[5, 6]];
assert(concat([a, b]).shape() == [3, 2]);
assert(concat([a, a], axis: 1).to_list() == [1, 2, 1, 2, 3, 4, 3, 4]);

stackSource

pub const fn stack<T, const N: usize>(parts: List<Array<T, N>>, axis: usize = 0) -> Array<T, { N + 1 }>

The arrays of parts, all of one shape, joined on a new axis at axis (0 puts them first). NumPy: np.stack(parts, axis).

nabi
use core::array::{Vector, stack};

let a: Vector<f64> = [1, 2, 3];
let b: Vector<f64> = [4, 5, 6];
assert(stack([a, b]).shape() == [2, 3]);
assert(stack([a, b], axis: 1).to_list() == [1.0, 4.0, 2.0, 5.0, 3.0, 6.0]);

whereSource

pub const fn where<T, const N: usize>(mask: Array<bool, N>, a: Array<T, N>, b: Array<T, N>) -> Array<T, N>

a's element where mask is true, else b's, the three broadcast to one shape. NumPy: np.where(mask, a, b). For a scalar on one side, fill_masked a copy: x.copy().fill_masked(x .< 0.0, 0.0).

nabi
use core::array::{Vector, where};

let x: Vector<f64> = [-1, 2, -3];
let y: Vector<f64> = [10, 20, 30];
assert(where(x .> 0.0, x, y).to_list() == [10.0, 2.0, 30.0]);

arangeSource

pub fn arange<T: FloatingPoint>(start: T, stop: T, step: T) -> Array<T, 1>

start, start + step, .. while before stop, as a vector (see num::arange). A zero or NaN step panics. NumPy: np.arange.

linspaceSource

pub fn linspace<T: FloatingPoint>(start: T, stop: T, n: usize) -> Array<T, 1>

n evenly spaced values from start to stop, both included, as a vector (see num::linspace for a list). NumPy: np.linspace.

nabi
use core::array::{arange, linspace};

assert(linspace(0.0, 1.0, 5).to_list() == [0.0, 0.25, 0.5, 0.75, 1.0]);
assert(arange(0.0, 1.0, 0.25).to_list() == [0.0, 0.25, 0.5, 0.75]);

from_fnSource

pub const fn from_fn<T, const N: usize>(f: fn(usize) -> T) -> [T; N]

A fixed array whose element i is f(i), called in order: each element its own value, where [v; N] copies one (v a resource, or not Clone). N comes from the expected type or ::<T, N>.

broadcast_shapeSource

pub const fn broadcast_shape<const N: usize>(a: [usize; N], b: [usize; N]) -> [usize; N]

The shape two operands of an element-wise operation stretch to: per axis, equal lengths stay, a length 1 stretches to the other. Any other pair panics with both shapes. Operands of different rank are first given axes of length 1 at the front (v[none, :]).

Implementations

impl [

  • pub const fn len(self) -> usize§

    Source

    The element count N.

  • pub const fn is_empty(self) -> bool§

    Source

    true when N is 0.

  • pub const fn iter(self) -> SliceIter<T>§

    Source

    The elements front to back, over a copy (arrays are values).

  • pub const fn first(self) -> T | none§

    Source

    The first element; none when N is 0.

  • pub const fn last(self) -> T | none§

    Source

    The last element; none when N is 0.

  • pub const fn map<U>(self, f: fn(T) -> U) -> [U; N]§

    Source

    A new array of f applied to each element, in order.

  • pub const fn fill(ref self, x: T)§

    Source

    Overwrite every element with x, in place.

  • pub const fn contains(self, x: T) -> bool§

    Source

    true when an element equals x.

Trait implementations 9

impl Pinned

  • pub fn as_data_ptr(self) -> Ptr<T>§

    Source

    The array's first element (index 0 on every axis); dangling for an empty array. Steps along each axis are its strides(), in elements.

  • pub fn as_data_many_ptr(self) -> ManyPtr<T>§

    Source

    as_data_ptr as a pointer to the run of elements.