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
impl NdIter
Sourcepub fn binary_map<T, U, D1, D2, F>(
a: &Array<T, D1>,
b: &Array<T, D2>,
f: F,
) -> FerrayResult<Array<U, IxDyn>>
pub fn binary_map<T, U, D1, D2, F>( a: &Array<T, D1>, b: &Array<T, D2>, f: F, ) -> FerrayResult<Array<U, IxDyn>>
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:
- Computes the broadcast shape
- Broadcasts both arrays (zero-copy via stride tricks)
- Iterates in row-major order, applying
fto each pair - 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.
Sourcepub fn binary_map_mixed<A, B, U, D1, D2, F>(
a: &Array<A, D1>,
b: &Array<B, D2>,
f: F,
) -> FerrayResult<Array<U, IxDyn>>
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>>
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.
Sourcepub 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<()>
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<()>
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.
Sourcepub fn unary_map<T, U, D, F>(
a: &Array<T, D>,
f: F,
) -> FerrayResult<Array<U, IxDyn>>
pub fn unary_map<T, U, D, F>( a: &Array<T, D>, f: F, ) -> FerrayResult<Array<U, IxDyn>>
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.
Sourcepub fn unary_map_into<T, D>(
a: &Array<T, D>,
out: &mut Array<T, IxDyn>,
f: impl Fn(T) -> T,
) -> FerrayResult<()>
pub fn unary_map_into<T, D>( a: &Array<T, D>, out: &mut Array<T, IxDyn>, f: impl Fn(T) -> T, ) -> FerrayResult<()>
Apply a unary function elementwise into a pre-allocated output.
Sourcepub fn broadcast_shape(
a_shape: &[usize],
b_shape: &[usize],
) -> FerrayResult<Vec<usize>>
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.
Sourcepub fn binary_iter<'a, T, D1, D2>(
a: &'a Array<T, D1>,
b: &'a Array<T, D2>,
) -> FerrayResult<BinaryBroadcastIter<'a, T>>
pub fn binary_iter<'a, T, D1, D2>( a: &'a Array<T, D1>, b: &'a Array<T, D2>, ) -> FerrayResult<BinaryBroadcastIter<'a, T>>
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.