pub struct Array<T: Element, D: Dimension> { /* private fields */ }Expand description
Implementations§
Source§impl<T: Element, D: Dimension> Array<T, D>
impl<T: Element, D: Dimension> Array<T, D>
Sourcepub fn broadcast_to(
&self,
target_shape: &[usize],
) -> FerrayResult<ArrayView<'_, T, IxDyn>>
pub fn broadcast_to( &self, target_shape: &[usize], ) -> FerrayResult<ArrayView<'_, T, IxDyn>>
Broadcast this array to the given shape, returning a dynamic-rank view.
Uses stride-0 tricks for virtual expansion — no data is copied.
§Errors
Returns FerrayError::BroadcastFailure if the array cannot be broadcast
to the target shape.
Source§impl<T: Element, D: Dimension> Array<T, D>
impl<T: Element, D: Dimension> Array<T, D>
Sourcepub fn add_promoted<U>(
&self,
other: &Array<U, D>,
) -> FerrayResult<Array<<T as Promoted<U>>::Output, D>>
pub fn add_promoted<U>( &self, other: &Array<U, D>, ) -> FerrayResult<Array<<T as Promoted<U>>::Output, D>>
Add two arrays after promoting both to their common type.
This is the explicit way to perform mixed-type addition (REQ-24).
Implicit mixed-type + does not compile.
Sourcepub fn sub_promoted<U>(
&self,
other: &Array<U, D>,
) -> FerrayResult<Array<<T as Promoted<U>>::Output, D>>
pub fn sub_promoted<U>( &self, other: &Array<U, D>, ) -> FerrayResult<Array<<T as Promoted<U>>::Output, D>>
Subtract two arrays after promoting both to their common type.
Sourcepub fn mul_promoted<U>(
&self,
other: &Array<U, D>,
) -> FerrayResult<Array<<T as Promoted<U>>::Output, D>>
pub fn mul_promoted<U>( &self, other: &Array<U, D>, ) -> FerrayResult<Array<<T as Promoted<U>>::Output, D>>
Multiply two arrays after promoting both to their common type.
Sourcepub fn div_promoted<U>(
&self,
other: &Array<U, D>,
) -> FerrayResult<Array<<T as Promoted<U>>::Output, D>>
pub fn div_promoted<U>( &self, other: &Array<U, D>, ) -> FerrayResult<Array<<T as Promoted<U>>::Output, D>>
Divide two arrays after promoting both to their common type.
Source§impl<T: Element, D: Dimension> Array<T, D>
impl<T: Element, D: Dimension> Array<T, D>
Sourcepub fn t(&self) -> ArrayView<'_, T, D>
pub fn t(&self) -> ArrayView<'_, T, D>
Transposed view (zero-copy). Reverses the axes.
This is the equivalent of NumPy’s .T property.
Sourcepub fn to_vec_flat(&self) -> Vec<T>
pub fn to_vec_flat(&self) -> Vec<T>
Convert to a flat Vec<T> in logical (row-major) order.
Sourcepub fn to_bytes(&self) -> FerrayResult<&[u8]>
pub fn to_bytes(&self) -> FerrayResult<&[u8]>
Return the raw bytes of the array data.
Only succeeds if the array is contiguous; returns an error otherwise.
Sourcepub fn flags(&self) -> ArrayFlags
pub fn flags(&self) -> ArrayFlags
Return flags describing memory properties.
Source§impl<T: Element, D: Dimension> Array<T, D>
impl<T: Element, D: Dimension> Array<T, D>
Sourcepub fn iter(&self) -> impl Iterator<Item = &T> + '_
pub fn iter(&self) -> impl Iterator<Item = &T> + '_
Iterate over all elements in logical (row-major) order.
Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> + '_
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> + '_
Mutably iterate over all elements in logical order.
Sourcepub fn indexed_iter(&self) -> impl Iterator<Item = (Vec<usize>, &T)> + '_
pub fn indexed_iter(&self) -> impl Iterator<Item = (Vec<usize>, &T)> + '_
Iterate with multi-dimensional indices.
Yields (Vec<usize>, &T) pairs in logical order. The index vector
has one entry per dimension.
Sourcepub fn flat(&self) -> impl Iterator<Item = &T> + '_
pub fn flat(&self) -> impl Iterator<Item = &T> + '_
Flat iterator — same as iter() but emphasises logical-order traversal.
Sourcepub fn lanes(
&self,
axis: Axis,
) -> FerrayResult<impl Iterator<Item = ArrayView<'_, T, Ix1>> + '_>
pub fn lanes( &self, axis: Axis, ) -> FerrayResult<impl Iterator<Item = ArrayView<'_, T, Ix1>> + '_>
Iterate over lanes (1-D slices) along the given axis.
For a 2-D array with axis=1, this yields each row.
For axis=0, this yields each column.
§Errors
Returns FerrayError::AxisOutOfBounds if axis >= ndim.
Sourcepub fn axis_iter(
&self,
axis: Axis,
) -> FerrayResult<impl Iterator<Item = ArrayView<'_, T, IxDyn>> + '_>where
D::NdarrayDim: RemoveAxis,
pub fn axis_iter(
&self,
axis: Axis,
) -> FerrayResult<impl Iterator<Item = ArrayView<'_, T, IxDyn>> + '_>where
D::NdarrayDim: RemoveAxis,
Iterate over sub-arrays along the given axis.
For a 3-D array with shape [2,3,4] and axis=0, this yields
two 2-D views each of shape [3,4].
§Errors
Returns FerrayError::AxisOutOfBounds if axis >= ndim.
Sourcepub fn axis_iter_mut(
&mut self,
axis: Axis,
) -> FerrayResult<impl Iterator<Item = ArrayViewMut<'_, T, IxDyn>> + '_>where
D::NdarrayDim: RemoveAxis,
pub fn axis_iter_mut(
&mut self,
axis: Axis,
) -> FerrayResult<impl Iterator<Item = ArrayViewMut<'_, T, IxDyn>> + '_>where
D::NdarrayDim: RemoveAxis,
Mutably iterate over sub-arrays along the given axis.
§Errors
Returns FerrayError::AxisOutOfBounds if axis >= ndim.
Source§impl<T: Element, D: Dimension> Array<T, D>
impl<T: Element, D: Dimension> Array<T, D>
Sourcepub fn mapv(&self, f: impl Fn(T) -> T) -> Self
pub fn mapv(&self, f: impl Fn(T) -> T) -> Self
Apply a closure to every element, returning a new array.
The closure receives each element by value (cloned) and must return the same type. For type-changing maps, collect via iterators.
Sourcepub fn mapv_inplace(&mut self, f: impl Fn(T) -> T)
pub fn mapv_inplace(&mut self, f: impl Fn(T) -> T)
Apply a closure to every element in place.
Sourcepub fn zip_mut_with(
&mut self,
other: &Array<T, D>,
f: impl Fn(&mut T, &T),
) -> FerrayResult<()>
pub fn zip_mut_with( &mut self, other: &Array<T, D>, f: impl Fn(&mut T, &T), ) -> FerrayResult<()>
Zip this array mutably with another array of the same shape, applying a closure to each pair of elements.
The closure receives (&mut T, &T) — the first element is from
self and can be modified, the second is from other.
§Errors
Returns FerrayError::ShapeMismatch if shapes differ.
Sourcepub fn fold_axis(
&self,
axis: Axis,
init: T,
fold: impl FnMut(&T, &T) -> T,
) -> FerrayResult<Array<T, IxDyn>>where
D::NdarrayDim: RemoveAxis,
pub fn fold_axis(
&self,
axis: Axis,
init: T,
fold: impl FnMut(&T, &T) -> T,
) -> FerrayResult<Array<T, IxDyn>>where
D::NdarrayDim: RemoveAxis,
Fold (reduce) along the given axis.
init provides the initial accumulator value for each lane.
The closure receives (accumulator, &element) and must return
the new accumulator.
Returns an array with one fewer dimension (the folded axis removed). The result is always returned as a dynamic-rank array.
§Errors
Returns FerrayError::AxisOutOfBounds if axis >= ndim.
Source§impl<T: Element, D: Dimension> Array<T, D>
impl<T: Element, D: Dimension> Array<T, D>
Sourcepub fn from_elem(dim: D, elem: T) -> FerrayResult<Self>
pub fn from_elem(dim: D, elem: T) -> FerrayResult<Self>
Create a new array filled with the given value.
§Errors
Returns FerrayError::InvalidValue if the shape has zero dimensions
but D is a fixed-rank type with nonzero rank, or vice versa.
Sourcepub fn zeros(dim: D) -> FerrayResult<Self>
pub fn zeros(dim: D) -> FerrayResult<Self>
Create a new array filled with zeros.
Sourcepub fn ones(dim: D) -> FerrayResult<Self>
pub fn ones(dim: D) -> FerrayResult<Self>
Create a new array filled with ones.
Sourcepub fn from_vec(dim: D, data: Vec<T>) -> FerrayResult<Self>
pub fn from_vec(dim: D, data: Vec<T>) -> FerrayResult<Self>
Create an array from a flat vector and a shape.
§Errors
Returns FerrayError::ShapeMismatch if data.len() does not equal
the product of the shape dimensions.
Sourcepub fn from_vec_f(dim: D, data: Vec<T>) -> FerrayResult<Self>
pub fn from_vec_f(dim: D, data: Vec<T>) -> FerrayResult<Self>
Create an array from a flat vector with Fortran (column-major) layout.
§Errors
Returns FerrayError::ShapeMismatch if lengths don’t match.
Sourcepub fn from_iter_1d(iter: impl IntoIterator<Item = T>) -> FerrayResult<Self>
pub fn from_iter_1d(iter: impl IntoIterator<Item = T>) -> FerrayResult<Self>
Create a 1-D array from an iterator.
This only makes sense for D = Ix1; for other dimensions,
collect first and use from_vec.
Sourcepub fn layout(&self) -> MemoryLayout
pub fn layout(&self) -> MemoryLayout
Return the memory layout of this array.
Sourcepub fn as_mut_ptr(&mut self) -> *mut T
pub fn as_mut_ptr(&mut self) -> *mut T
Return a mutable raw pointer to the first element.
Sourcepub fn as_slice(&self) -> Option<&[T]>
pub fn as_slice(&self) -> Option<&[T]>
Return the data as a contiguous slice, if the layout allows it.
Sourcepub fn as_slice_mut(&mut self) -> Option<&mut [T]>
pub fn as_slice_mut(&mut self) -> Option<&mut [T]>
Return the data as a contiguous mutable slice, if the layout allows it.
Source§impl<T: Element, D: Dimension> Array<T, D>
impl<T: Element, D: Dimension> Array<T, D>
Sourcepub fn view_mut(&mut self) -> ArrayViewMut<'_, T, D>
pub fn view_mut(&mut self) -> ArrayViewMut<'_, T, D>
Create a mutable view of this array.
Source§impl<T: Element, D: Dimension> Array<T, D>
impl<T: Element, D: Dimension> Array<T, D>
Sourcepub fn index_select(
&self,
axis: Axis,
indices: &[isize],
) -> FerrayResult<Array<T, IxDyn>>
pub fn index_select( &self, axis: Axis, indices: &[isize], ) -> FerrayResult<Array<T, IxDyn>>
Select elements along an axis using an array of indices.
This is advanced (fancy) indexing — it always returns a copy.
The result has the same number of dimensions as the input, but
the size along axis is replaced by indices.len().
Negative indices are supported (counting from end).
§Errors
AxisOutOfBoundsifaxis >= ndimIndexOutOfBoundsif any index is out of range
Sourcepub fn boolean_index(
&self,
mask: &Array<bool, D>,
) -> FerrayResult<Array<T, Ix1>>
pub fn boolean_index( &self, mask: &Array<bool, D>, ) -> FerrayResult<Array<T, Ix1>>
Select elements using a boolean mask.
Returns a 1-D array containing elements where mask is true.
This is always a copy.
The mask must be broadcastable to the array’s shape, or have the same total number of elements. When the mask is 1-D and the array is N-D, the mask is applied to the flattened array.
§Errors
ShapeMismatchif mask shape is incompatible
Sourcepub fn boolean_index_flat(
&self,
mask: &Array<bool, Ix1>,
) -> FerrayResult<Array<T, Ix1>>
pub fn boolean_index_flat( &self, mask: &Array<bool, Ix1>, ) -> FerrayResult<Array<T, Ix1>>
Boolean indexing with a flat mask (1-D mask on N-D array).
The mask is applied to the flattened (row-major) array.
§Errors
ShapeMismatchifmask.len() != self.size()
Sourcepub fn boolean_index_assign(
&mut self,
mask: &Array<bool, D>,
value: T,
) -> FerrayResult<()>
pub fn boolean_index_assign( &mut self, mask: &Array<bool, D>, value: T, ) -> FerrayResult<()>
Assign a scalar value to elements selected by a boolean mask.
Equivalent to a[mask] = value in NumPy.
§Errors
ShapeMismatchif mask shape differs from array shape
Sourcepub fn boolean_index_assign_array(
&mut self,
mask: &Array<bool, D>,
values: &Array<T, Ix1>,
) -> FerrayResult<()>
pub fn boolean_index_assign_array( &mut self, mask: &Array<bool, D>, values: &Array<T, Ix1>, ) -> FerrayResult<()>
Assign values from an array to elements selected by a boolean mask.
values must have exactly as many elements as mask has true
entries.
§Errors
ShapeMismatchif mask shape differs or values length mismatches
Source§impl<T: Element, D: Dimension> Array<T, D>
impl<T: Element, D: Dimension> Array<T, D>
Sourcepub fn index_axis(
&self,
axis: Axis,
index: isize,
) -> FerrayResult<ArrayView<'_, T, IxDyn>>where
D::NdarrayDim: RemoveAxis,
pub fn index_axis(
&self,
axis: Axis,
index: isize,
) -> FerrayResult<ArrayView<'_, T, IxDyn>>where
D::NdarrayDim: RemoveAxis,
Index into the array along a given axis, removing that axis.
Equivalent to a[i] for axis 0, or a[:, i] for axis 1, etc.
Returns a view with one fewer dimension (dynamic-rank).
§Errors
AxisOutOfBoundsifaxis >= ndimIndexOutOfBoundsifindexis out of range (supports negative)
Sourcepub fn slice_axis(
&self,
axis: Axis,
spec: SliceSpec,
) -> FerrayResult<ArrayView<'_, T, IxDyn>>
pub fn slice_axis( &self, axis: Axis, spec: SliceSpec, ) -> FerrayResult<ArrayView<'_, T, IxDyn>>
Slice the array along a given axis, returning a view.
The returned view shares data with the source (zero-copy).
§Errors
AxisOutOfBoundsifaxis >= ndimInvalidValueif step is zero
Sourcepub fn slice_axis_mut(
&mut self,
axis: Axis,
spec: SliceSpec,
) -> FerrayResult<ArrayViewMut<'_, T, IxDyn>>
pub fn slice_axis_mut( &mut self, axis: Axis, spec: SliceSpec, ) -> FerrayResult<ArrayViewMut<'_, T, IxDyn>>
Sourcepub fn slice_multi(
&self,
specs: &[SliceSpec],
) -> FerrayResult<ArrayView<'_, T, IxDyn>>
pub fn slice_multi( &self, specs: &[SliceSpec], ) -> FerrayResult<ArrayView<'_, T, IxDyn>>
Multi-axis slicing: apply a slice specification to each axis.
specs must have length equal to ndim(). For axes you don’t
want to slice, pass SliceSpec::full().
§Errors
InvalidValueifspecs.len() != ndim()- Any errors from individual axis slicing
Sourcepub fn insert_axis(&self, axis: Axis) -> FerrayResult<ArrayView<'_, T, IxDyn>>
pub fn insert_axis(&self, axis: Axis) -> FerrayResult<ArrayView<'_, T, IxDyn>>
Insert a new axis of length 1 at the given position.
This is equivalent to np.expand_dims or np.newaxis.
Returns a dynamic-rank view with one more dimension.
§Errors
AxisOutOfBoundsifaxis > ndim
Sourcepub fn remove_axis(&self, axis: Axis) -> FerrayResult<ArrayView<'_, T, IxDyn>>
pub fn remove_axis(&self, axis: Axis) -> FerrayResult<ArrayView<'_, T, IxDyn>>
Remove an axis of length 1.
This is equivalent to np.squeeze for a single axis.
Returns a dynamic-rank view with one fewer dimension.
§Errors
AxisOutOfBoundsifaxis >= ndimInvalidValueif the axis has size != 1
Sourcepub fn flat_index(&self, index: isize) -> FerrayResult<&T>
pub fn flat_index(&self, index: isize) -> FerrayResult<&T>
Index into the array with a flat (linear) index.
Elements are ordered in row-major (C) order.
§Errors
Returns IndexOutOfBounds if the index is out of range.
Source§impl<T: Element, D: Dimension> Array<T, D>
impl<T: Element, D: Dimension> Array<T, D>
Sourcepub fn put(&mut self, indices: &[isize], values: &[T]) -> FerrayResult<()>
pub fn put(&mut self, indices: &[isize], values: &[T]) -> FerrayResult<()>
Put values into the flattened array at the given indices.
Equivalent to np.put(a, ind, v). Modifies the array in-place.
Indices refer to the flattened (row-major) array.
Values are cycled if fewer than indices.
§Errors
IndexOutOfBoundsif any index is out of rangeInvalidValueif values is empty
Sourcepub fn put_along_axis(
&mut self,
indices: &[isize],
values: &Array<T, IxDyn>,
axis: Axis,
) -> FerrayResult<()>where
D::NdarrayDim: RemoveAxis,
pub fn put_along_axis(
&mut self,
indices: &[isize],
values: &Array<T, IxDyn>,
axis: Axis,
) -> FerrayResult<()>where
D::NdarrayDim: RemoveAxis,
Put values along an axis at specified indices.
For each index position along axis, assigns the values from the
corresponding sub-array of values.
§Errors
AxisOutOfBoundsifaxis >= ndimIndexOutOfBoundsif any index is out of range
Sourcepub fn fill_diagonal(&mut self, val: T)
pub fn fill_diagonal(&mut self, val: T)
Fill the main diagonal of a 2-D (or N-D) array with a value.
For N-D arrays, the diagonal consists of indices where all
index values are equal: a[i, i, ..., i].
Equivalent to np.fill_diagonal(a, val).
Trait Implementations§
Source§impl<T: Element, D: Dimension> AsRawBuffer for Array<T, D>
impl<T: Element, D: Dimension> AsRawBuffer for Array<T, D>
Source§fn raw_strides_bytes(&self) -> Vec<isize>
fn raw_strides_bytes(&self) -> Vec<isize>
Source§fn is_c_contiguous(&self) -> bool
fn is_c_contiguous(&self) -> bool
Source§fn is_f_contiguous(&self) -> bool
fn is_f_contiguous(&self) -> bool
Source§impl<T: Element, D: Dimension> AsType<D> for Array<T, D>
Available on non-crate feature no_std only.
impl<T: Element, D: Dimension> AsType<D> for Array<T, D>
no_std only.Source§fn astype<U: Element>(&self) -> FerrayResult<Array<U, D>>where
Self: AsTypeInner<U, D>,
fn astype<U: Element>(&self) -> FerrayResult<Array<U, D>>where
Self: AsTypeInner<U, D>,
U, returning a new array. Read moreSource§impl<T, U, D> AsTypeInner<U, D> for Array<T, D>
Available on non-crate feature no_std only.Blanket implementation: any T that can PromoteTo can astype.
impl<T, U, D> AsTypeInner<U, D> for Array<T, D>
no_std only.Blanket implementation: any T that can PromoteTo can astype.