Skip to main content

NdIter

Struct NdIter 

Source
pub struct NdIter;
Expand description

A broadcast iterator over two arrays, yielding element pairs.

The two input arrays are broadcast to a common shape. Iteration proceeds in row-major (C) order over the broadcast shape.

§Example

let a = Array::<f64, Ix2>::from_vec(Ix2::new([3, 1]), vec![1.0, 2.0, 3.0]).unwrap();
let b = Array::<f64, Ix1>::from_vec(Ix1::new([4]), vec![10.0, 20.0, 30.0, 40.0]).unwrap();
let result = NdIter::binary_map(&a, &b, |x, y| x + y).unwrap();
assert_eq!(result.shape(), &[3, 4]);

Implementations§

Source§

impl NdIter

Source

pub fn binary_map<T, U, D1, D2, F>( a: &Array<T, D1>, b: &Array<T, D2>, f: F, ) -> FerrayResult<Array<U, IxDyn>>
where T: Element + Copy, U: Element, D1: Dimension, D2: Dimension, F: Fn(T, T) -> U,

Apply a binary function elementwise over two broadcast arrays, returning a new array with the broadcast shape.

This is the workhorse for ufunc-style binary operations. It:

  1. Computes the broadcast shape
  2. Broadcasts both arrays (zero-copy via stride tricks)
  3. Iterates in row-major order, applying f to each pair
  4. Returns the result as Array<U, IxDyn>

For contiguous inner loops, the implementation uses slice access when both broadcast views are contiguous.

§Errors

Returns FerrayError::BroadcastFailure if shapes are incompatible.

Source

pub fn binary_map_mixed<A, B, U, D1, D2, F>( a: &Array<A, D1>, b: &Array<B, D2>, f: F, ) -> FerrayResult<Array<U, IxDyn>>
where A: Element + Copy, B: Element + Copy, U: Element, D1: Dimension, D2: Dimension, F: Fn(A, B) -> U,

Apply a binary function where inputs may have different element types.

Unlike [binary_map] which requires both arrays to have the same T, this accepts Array<A, D1> and Array<B, D2> with different element types, mapping (A, B) -> U.

This is essential for mixed-type operations like comparing an integer array against a float threshold, or operations after type promotion.

Source

pub fn binary_map_into<T, D1, D2>( a: &Array<T, D1>, b: &Array<T, D2>, out: &mut Array<T, IxDyn>, f: impl Fn(T, T) -> T, ) -> FerrayResult<()>
where T: Element + Copy, D1: Dimension, D2: Dimension,

Apply a binary function elementwise, writing results into a pre-allocated output array. Avoids allocation for repeated operations.

The output array must have the broadcast shape.

§Errors

Returns errors on shape mismatch or broadcast failure.

Source

pub fn unary_map<T, U, D, F>( a: &Array<T, D>, f: F, ) -> FerrayResult<Array<U, IxDyn>>
where T: Element + Copy, U: Element, D: Dimension, F: Fn(T) -> U,

Apply a unary function elementwise, returning a new array.

This is simpler than the binary case (no broadcasting needed) but provides the same contiguous-slice optimization.

Source

pub fn unary_map_into<T, D>( a: &Array<T, D>, out: &mut Array<T, IxDyn>, f: impl Fn(T) -> T, ) -> FerrayResult<()>
where T: Element + Copy, D: Dimension,

Apply a unary function elementwise into a pre-allocated output.

Source

pub fn broadcast_shape( a_shape: &[usize], b_shape: &[usize], ) -> FerrayResult<Vec<usize>>

Compute the broadcast shape of two arrays without iterating.

Useful for pre-allocating output arrays.

Source

pub fn binary_iter<'a, T, D1, D2>( a: &'a Array<T, D1>, b: &'a Array<T, D2>, ) -> FerrayResult<BinaryBroadcastIter<'a, T>>
where T: Element + Copy, D1: Dimension, D2: Dimension,

Iterate over two broadcast arrays, yielding (&T, &T) pairs.

This returns an iterator that can be consumed lazily. The arrays are broadcast to a common shape and iterated in row-major order.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.