Trait dfdx::devices::ForEachElement
source · [−]pub trait ForEachElement<T: CountElements>: AllocateZeros {
fn foreach_m<F: FnMut(&mut T::Dtype)>(a: &mut T, f: &mut F);
fn foreach_mr<F>(a: &mut T, b: &T, f: &mut F)
where
F: FnMut(&mut T::Dtype, &T::Dtype);
fn foreach_mm<F>(a: &mut T, b: &mut T, f: &mut F)
where
F: FnMut(&mut T::Dtype, &mut T::Dtype);
fn foreach_mmm<F>(a: &mut T, b: &mut T, c: &mut T, f: &mut F)
where
F: FnMut(&mut T::Dtype, &mut T::Dtype, &mut T::Dtype);
fn foreach_mrr<F>(a: &mut T, b: &T, c: &T, f: &mut F)
where
F: FnMut(&mut T::Dtype, &T::Dtype, &T::Dtype);
}
Expand description
Apply generic function to various forms/numbers of ndarrays.
The various versions that exist are:
- ForEachElement::foreach_m(), which takes 1 mut array.
- ForEachElement::foreach_mr(), which takes 1 mut array and 1 ref array
- ForEachElement::foreach_mm(), which takes 2 mut arrays
- ForEachElement::foreach_mrr(), which takes 1 mut array and 2 ref arrays
- ForEachElement::foreach_mmm(), which takes 3 mut arrays
Examples:
let mut a = [[0.0; 3]; 2];
let b = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
Cpu::foreach_mr(&mut a, &b, &mut |x, y| {
*x = 2.0 * y;
});
assert_eq!(a, [[2.0, 4.0, 6.0], [8.0, 10.0, 12.0]]);
Required Methods
sourcefn foreach_m<F: FnMut(&mut T::Dtype)>(a: &mut T, f: &mut F)
fn foreach_m<F: FnMut(&mut T::Dtype)>(a: &mut T, f: &mut F)
Mutate elements of a
by applying f
to all elements of a.
sourcefn foreach_mr<F>(a: &mut T, b: &T, f: &mut F)where
F: FnMut(&mut T::Dtype, &T::Dtype),
fn foreach_mr<F>(a: &mut T, b: &T, f: &mut F)where
F: FnMut(&mut T::Dtype, &T::Dtype),
Mutate elements of a
by applying f
to all elements of (a, b).
mr
stands for mut ref
sourcefn foreach_mm<F>(a: &mut T, b: &mut T, f: &mut F)where
F: FnMut(&mut T::Dtype, &mut T::Dtype),
fn foreach_mm<F>(a: &mut T, b: &mut T, f: &mut F)where
F: FnMut(&mut T::Dtype, &mut T::Dtype),
Mutate elements of a
and b
by applying f
to all elements of (a, b).
mm
stands for mut mut