core::array
이 페이지의 내용
- Structs
Array79allall_alongall_closeanyany_alongargmaxargmax_alongargminargmin_alongargsort_alongas_listas_ptrassignassign_maskedatbroadcast_tocastcopycountcount_alongcumprodcumprod_alongcumsumcumsum_alongfillfill_maskedfrom_fnfrom_listfrom_raw_partsfullgetis_closeis_contiguousis_emptyiterlenmapmaskedmaxmax_alongmeanmean_alongminmin_alongnanargmaxnanargminnanmaxnanmax_alongnanmeannanmean_alongnanminnanmin_alongnansumnansum_alongndimonespermuteprodprod_alongraw_partsreshapesetshapesortsort_alongsortedsorted_alongstdstd_alongstridessumsum_alongtto_listunravelvarvar_alongview_listzeros
- Type aliases
- Functions
- Implementations
impl Pinned2
API 참조는 영어로만 있습니다. 표준 라이브러리 소스의 영어 문서 주석에서 만듭니다.
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.
Every element
v.Element at index
idxisf(idx), filled in row-major order.The elements of
xsin row-major order as an array ofshape; panics unless the counts agree.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 withxs[a:b]. Panics unless the counts agree (from_listcopies).The elements as a list sharing this array's buffer, no copy, when they are contiguous from its start (a fresh array, a
view_listone); elsenone, andto_list()copies. The list's first push moves it to a buffer of its own.The length of each axis.
The step, in elements, between neighbours along each axis.
The rank
N.The element count (the product of the shape).
truewhen some axis has length 0.truewhen the elements are adjacent in row-major order, as a fresh array's are.The element at
idx,nonewhen an index is past its axis.The element at
idx; an index past its axis panics.Store
vatidx; an index past its axis panics.Overwrite every element with
v, in place (through a view too).The elements in row-major order.
The elements in row-major order, as a new list.
A contiguous copy with its own buffer.
fof each element, as a new array of the same shape.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.The axes in reverse order, a view (
a.t()[i, j]isa[j, i]).The axes in the order
axesnames them, a view;axesmust name each axis once.This array stretched to
shapewithout a copy: an axis of length 1 repeats (stride 0), the others must match. Panics, naming both shapes, when they do not.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.
Copy
srcinto this array element by element;srcstretches (broadcast_to) to this shape.The elements where
maskistrue, in row-major order, as a new rank-1 array. The shapes must match.Store
vwheremaskistrue. The shapes must match.Store the elements of
valsin order wheremaskistrue;valsholds one pertrue.Every element
0.Every element
1.
Sourcepub const fn is_close(§self, other: Array<T, N>, rel: f64 | none = none, abs: f64 = 0.0, equal_nan: bool = false) -> Array<bool, N> Whether each element is close to
other's (broadcast to one shape):|a - b| <= max(rel * max(|a|, |b|), abs), or equal. Symmetric inaandb.reldefaults to the element type's (f64: 1e-9, as Python'smath.isclose;f32: 2^-20); passabswhen values near zero must match. NaN is close to nothing unlessequal_nan.nabiuse 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]);
Sourcepub const fn all_close(§self, other: Array<T, N>, rel: f64 | none = none, abs: f64 = 0.0, equal_nan: bool = false) -> bool Whether every element is close to
other's (seeis_close); the shapes must broadcast. NumPy:np.allclose, but symmetric and relative by default.nabiuse 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));Each element converted to
Uwithout loss (i32tof64,u8toi64, ..), as a new array of the same shape. NumPy:a.astype(U). A lossy conversion is amap:a.map(|x: f64| -> i64 { x as i64 }).nabiuse core::array::Vector; let v: Vector<i32> = [1, 2, 3]; let f: Vector<f64> = v.cast(); assert(f.mean() == 2.0);
Sourcepub const unsafe fn from_raw_parts(§buf: GcBuf<T>, off: usize, shape: [usize; N], strides: [isize; N]) -> Array<T, N> An array over
buf: the element atidxis `buf[off + sum(idx[k]- strides[k])]
. No copy and no check: every index in the shape must name a slot ofbuf. For kernels that keep their data in aGcBuf` and want the array methods on a run of it.
nabiuse 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);- strides[k])]
The buffer, the offset of the first element, the shape and the strides: what
from_raw_partstakes.The sum of every element (
0when empty), pairwise: runs of 128 summed with eight accumulators, halves added, so the float error grows with log2 of the count. NumPy:a.sum().nabiuse core::array::Matrix; let m: Matrix<f64> = [[1, 2, 3], [4, 5, 6]]; assert(m.sum() == 21.0);The sum of each lane along
axis; that axis is dropped. Panics for an axis past the rank. NumPy:a.sum(axis=k).nabiuse 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]);The running sums in row-major order, as a rank-1 array (the last is
sum()). NumPy:np.cumsum(a).nabiuse core::array::Vector; let v: Vector<i64> = [1, 2, 3, 4]; assert(v.cumsum().to_list() == [1, 3, 6, 10]);The running sums of each lane along
axis, same shape. NumPy:np.cumsum(a, axis=k).nabiuse 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]);The product of every element (
1when empty), in row-major order. NumPy:a.prod().nabiuse core::array::Vector; let v: Vector<i64> = [1, 2, 3, 4]; assert(v.prod() == 24);The product of each lane along
axis; that axis is dropped.The running products in row-major order, as a rank-1 array. NumPy:
np.cumprod(a).The running products of each lane along
axis, same shape.The smallest element;
nonewhen empty. A NaN anywhere makes the result NaN (nanminskips it). NumPy:a.min().nabiuse 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);The largest element;
nonewhen empty. A NaN anywhere makes the result NaN (nanmaxskips it). NumPy:a.max().The smallest element that is not NaN;
nonewhen there is none. NumPy:np.nanmin(a).nabiuse 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);The largest element that is not NaN;
nonewhen there is none.The row-major index of the smallest element (the first of equals, or of the first NaN);
nonewhen empty. NumPy:a.argmin();unravelturns it into an index per axis.nabiuse 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]);The row-major index of the largest element (the first of equals, or of the first NaN);
nonewhen empty. NumPy:a.argmax().argminover the elements that are not NaN.argmaxover the elements that are not NaN.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.nabiuse 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]);The largest element of each lane along
axis; that axis is dropped. A lane with a NaN gives NaN.min_alongleaving NaN out; a lane of only NaN gives NaN.max_alongleaving NaN out; a lane of only NaN gives NaN.The index along
axisof each lane's smallest element.nabiuse 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]);The index along
axisof each lane's largest element.The index per axis of row-major position
i(whatargmingives). Panics wheniis past the last element. NumPy:np.unravel_index.The mean of every element; NaN when empty. The pairwise sum over the count, then one pass adding the mean residual. NumPy:
a.mean().nabiuse 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]);The mean of each lane along
axis; that axis is dropped.The variance, dividing by
n - ddof(ddof: 1for the sample variance); NaN whenn <= ddof. Corrected two-pass. NumPy:a.var(ddof=..).nabiuse 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);The standard deviation,
sqrt(var(ddof)).The variance of each lane along
axis; that axis is dropped.The standard deviation of each lane along
axis.The sum of the elements that are not NaN (0 when there are none). NumPy:
np.nansum(a).nabiuse 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);The mean of the elements that are not NaN; NaN when there are none. NumPy:
np.nanmean(a).nansumof each lane alongaxis.nanmeanof each lane alongaxis.truewhen some element is (falsewhen empty). With a dot comparison it tests a condition:(a .> 0.0).any().nabiuse 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);truewhen every element is (truewhen empty).The number of
trueelements. NumPy:np.count_nonzero(a).anyof each lane alongaxis.nabiuse 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]);allof each lane alongaxis.countof each lane alongaxis.A copy with each lane along
axissorted ascending, stable; NaN last. NumPy:np.sort(a, axis).nabiuse 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]);Each lane along
axissorted in place (through a view too).A sorted copy: a vector's elements ascending; for more axes, each lane of the last. NumPy:
np.sort(a).nabiuse 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]);sortedin place.The order that sorts each lane along
axis: positions along it, stable, NaN last. NumPy:np.argsort(a, axis, kind="stable").nabiuse 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
impl<T, const N: usize> !Sync for Array<T, N>impl<T, const N: usize> AxisIndexed for Array<T, N>impl<T, const N: usize> Broadcastable<N> for Array<T, N>impl<T, U, const N: usize> Materialize<U, N> for Array<T, N>impl<T, const N: usize> IntoIterator for Array<T, N>impl<T, const N: usize> Pinnable for Array<T, N>impl<T> From<List<T>> for Array<T, 1>impl<T: PartialEq, const N: usize> PartialEq for Array<T, N>impl<T: Clone, const N: usize> Clone for Array<T, N>impl<T: Debug, const N: usize> Debug for Array<T, N>impl<T: Debug, const N: usize> Display for Array<T, N>
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).
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).
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).
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.
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 [
The element count
N.truewhenNis 0.The elements front to back, over a copy (arrays are values).
The first element;
nonewhenNis 0.The last element;
nonewhenNis 0.A new array of
fapplied to each element, in order.Overwrite every element with
x, in place.truewhen an element equalsx.
Trait implementations 9
impl<T: Clone, const N: usize> Clone for [T; N]impl<T: Copy, const N: usize> Copy for [T; N]impl<T: Default, const N: usize> Default for [T; N]impl<T: Debug, const N: usize> Debug for [T; N]impl<T: PartialEq, const N: usize> PartialEq for [T; N]impl<T: Eq, const N: usize> Eq for [T; N]impl<T: PartialEq + PartialOrd, const N: usize> PartialOrd for [T; N]impl<T: Ord, const N: usize> Ord for [T; N]impl<T: Hash, const N: usize> Hash for [T; N]
impl Pinned
The array's first element (index 0 on every axis); dangling for an empty array. Steps along each axis are its
strides(), in elements.as_data_ptras a pointer to the run of elements.