broadcast_apply

Function broadcast_apply 

Source
pub fn broadcast_apply<T, R, F>(
    a: ArrayView<'_, T, Ix2>,
    b: ArrayView<'_, T, Ix1>,
    op: F,
) -> Result<Array<R, Ix2>, &'static str>
where T: Clone + Default, R: Clone + Default, F: Fn(&T, &T) -> R,
Expand description

Apply an element-wise binary operation to two arrays with broadcasting

§Arguments

  • a - First array (2D)
  • b - Second array (can be 1D or 2D)
  • op - Binary operation to apply to each pair of elements

§Returns

A 2D array containing the result of the operation applied element-wise

§Examples

use ndarray::array;
use scirs2_core::ndarray_ext::broadcast_apply;

let a = array![[1, 2, 3], [4, 5, 6]];
let b = array![10, 20, 30];
let result = broadcast_apply(a.view(), b.view(), |x, y| x + y).unwrap();
assert_eq!(result.shape(), &[2, 3]);
assert_eq!(result[[0, 0]], 11);
assert_eq!(result[[1, 2]], 36);