[−][src]Struct tract_nnef::prelude::tract_ndarray::Zip
Lock step function application across several arrays or other producers.
Zip allows matching several producers to each other elementwise and applying a function over all tuples of elements (one item from each input at a time).
In general, the zip uses a tuple of producers
(NdProducer
trait) that all have to be of the
same shape. The NdProducer implementation defines what its item type is
(for example if it's a shared reference, mutable reference or an array
view etc).
If all the input arrays are of the same memory layout the zip performs much better and the compiler can usually vectorize the loop (if applicable).
The order elements are visited is not specified. The producers don’t have to have the same item type.
The Zip
has two methods for function application: apply
and
fold_while
. The zip object can be split, which allows parallelization.
A read-only zip object (no mutable producers) can be cloned.
See also the azip!()
macro which offers a convenient shorthand
to common ways to use Zip
.
use ndarray::Zip; use ndarray::Array2; type M = Array2<f64>; // Create four 2d arrays of the same size let mut a = M::zeros((64, 32)); let b = M::from_elem(a.dim(), 1.); let c = M::from_elem(a.dim(), 2.); let d = M::from_elem(a.dim(), 3.); // Example 1: Perform an elementwise arithmetic operation across // the four arrays a, b, c, d. Zip::from(&mut a) .and(&b) .and(&c) .and(&d) .apply(|w, &x, &y, &z| { *w += x + y * z; }); // Example 2: Create a new array `totals` with one entry per row of `a`. // Use Zip to traverse the rows of `a` and assign to the corresponding // entry in `totals` with the sum across each row. // This is possible because the producer for `totals` and the row producer // for `a` have the same shape and dimensionality. // The rows producer yields one array view (`row`) per iteration. use ndarray::{Array1, Axis}; let mut totals = Array1::zeros(a.nrows()); Zip::from(&mut totals) .and(a.genrows()) .apply(|totals, row| *totals = row.sum()); // Check the result against the built in `.sum_axis()` along axis 1. assert_eq!(totals, a.sum_axis(Axis(1)));
Implementations
impl<P, D> Zip<(P,), D> where
D: Dimension,
P: NdProducer<Dim = D>,
[src]
D: Dimension,
P: NdProducer<Dim = D>,
pub fn from<IP>(p: IP) -> Zip<(P,), D> where
IP: IntoNdProducer<Dim = D, Output = P, Item = <P as NdProducer>::Item>,
[src]
IP: IntoNdProducer<Dim = D, Output = P, Item = <P as NdProducer>::Item>,
Create a new Zip
from the input array or other producer p
.
The Zip will take the exact dimension of p
and all inputs
must have the same dimensions (or be broadcast to them).
impl<P, D> Zip<(Indices<D>, P), D> where
D: Dimension + Copy,
P: NdProducer<Dim = D>,
[src]
D: Dimension + Copy,
P: NdProducer<Dim = D>,
pub fn indexed<IP>(p: IP) -> Zip<(Indices<D>, P), D> where
IP: IntoNdProducer<Dim = D, Output = P, Item = <P as NdProducer>::Item>,
[src]
IP: IntoNdProducer<Dim = D, Output = P, Item = <P as NdProducer>::Item>,
Create a new Zip
with an index producer and the producer p
.
The Zip will take the exact dimension of p
and all inputs
must have the same dimensions (or be broadcast to them).
Note: Indexed zip has overhead.
impl<Parts, D> Zip<Parts, D> where
D: Dimension,
[src]
D: Dimension,
impl<D, P1> Zip<(P1,), D> where
D: Dimension,
P1: NdProducer<Dim = D>,
[src]
D: Dimension,
P1: NdProducer<Dim = D>,
pub fn apply<F>(self, function: F) where
F: FnMut(<P1 as NdProducer>::Item),
[src]
F: FnMut(<P1 as NdProducer>::Item),
Apply a function to all elements of the input arrays, visiting elements in lock step.
pub fn fold<F, Acc>(self, acc: Acc, function: F) -> Acc where
F: FnMut(Acc, <P1 as NdProducer>::Item) -> Acc,
[src]
F: FnMut(Acc, <P1 as NdProducer>::Item) -> Acc,
Apply a fold function to all elements of the input arrays, visiting elements in lock step.
Example
The expression tr(AᵀB)
can be more efficiently computed as
the equivalent expression ∑ᵢⱼ(A∘B)ᵢⱼ
(i.e. the sum of the
elements of the entry-wise product). It would be possible to
evaluate this expression by first computing the entry-wise
product, A∘B
, and then computing the elementwise sum of that
product, but it's possible to do this in a single loop (and
avoid an extra heap allocation if A
and B
can't be
consumed) by using Zip
:
use ndarray::{array, Zip}; let a = array![[1, 5], [3, 7]]; let b = array![[2, 4], [8, 6]]; // Without using `Zip`. This involves two loops and an extra // heap allocation for the result of `&a * &b`. let sum_prod_nonzip = (&a * &b).sum(); // Using `Zip`. This is a single loop without any heap allocations. let sum_prod_zip = Zip::from(&a).and(&b).fold(0, |acc, a, b| acc + a * b); assert_eq!(sum_prod_nonzip, sum_prod_zip);
pub fn fold_while<F, Acc>(self, acc: Acc, function: F) -> FoldWhile<Acc> where
F: FnMut(Acc, <P1 as NdProducer>::Item) -> FoldWhile<Acc>,
[src]
F: FnMut(Acc, <P1 as NdProducer>::Item) -> FoldWhile<Acc>,
Apply a fold function to the input arrays while the return
value is FoldWhile::Continue
, visiting elements in lock step.
pub fn all<F>(self, predicate: F) -> bool where
F: FnMut(<P1 as NdProducer>::Item) -> bool,
[src]
F: FnMut(<P1 as NdProducer>::Item) -> bool,
Tests if every element of the iterator matches a predicate.
Returns true
if predicate
evaluates to true
for all elements.
Returns true
if the input arrays are empty.
Example:
use ndarray::{array, Zip}; let a = array![1, 2, 3]; let b = array![1, 4, 9]; assert!(Zip::from(&a).and(&b).all(|&a, &b| a * a == b));
pub fn and<P>(self, p: P) -> Zip<(P1, <P as IntoNdProducer>::Output), D> where
P: IntoNdProducer<Dim = D>,
[src]
P: IntoNdProducer<Dim = D>,
Include the producer p
in the Zip.
Panics if p
’s shape doesn’t match the Zip’s exactly.
pub fn and_broadcast<'a, P, D2, Elem>(
self,
p: P
) -> Zip<(P1, ArrayBase<ViewRepr<&'a Elem>, D>), D> where
D2: Dimension,
P: IntoNdProducer<Dim = D2, Output = ArrayBase<ViewRepr<&'a Elem>, D2>, Item = &'a Elem>,
[src]
self,
p: P
) -> Zip<(P1, ArrayBase<ViewRepr<&'a Elem>, D>), D> where
D2: Dimension,
P: IntoNdProducer<Dim = D2, Output = ArrayBase<ViewRepr<&'a Elem>, D2>, Item = &'a Elem>,
Include the producer p
in the Zip, broadcasting if needed.
If their shapes disagree, rhs
is broadcast to the shape of self
.
Panics if broadcasting isn’t possible.
pub fn split(self) -> (Zip<(P1,), D>, Zip<(P1,), D>)
[src]
Split the Zip
evenly in two.
It will be split in the way that best preserves element locality.
impl<D, P1, P2> Zip<(P1, P2), D> where
D: Dimension,
P1: NdProducer<Dim = D>,
P2: NdProducer<Dim = D>,
[src]
D: Dimension,
P1: NdProducer<Dim = D>,
P2: NdProducer<Dim = D>,
pub fn apply<F>(self, function: F) where
F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item),
[src]
F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item),
Apply a function to all elements of the input arrays, visiting elements in lock step.
pub fn fold<F, Acc>(self, acc: Acc, function: F) -> Acc where
F: FnMut(Acc, <P1 as NdProducer>::Item, <P2 as NdProducer>::Item) -> Acc,
[src]
F: FnMut(Acc, <P1 as NdProducer>::Item, <P2 as NdProducer>::Item) -> Acc,
Apply a fold function to all elements of the input arrays, visiting elements in lock step.
Example
The expression tr(AᵀB)
can be more efficiently computed as
the equivalent expression ∑ᵢⱼ(A∘B)ᵢⱼ
(i.e. the sum of the
elements of the entry-wise product). It would be possible to
evaluate this expression by first computing the entry-wise
product, A∘B
, and then computing the elementwise sum of that
product, but it's possible to do this in a single loop (and
avoid an extra heap allocation if A
and B
can't be
consumed) by using Zip
:
use ndarray::{array, Zip}; let a = array![[1, 5], [3, 7]]; let b = array![[2, 4], [8, 6]]; // Without using `Zip`. This involves two loops and an extra // heap allocation for the result of `&a * &b`. let sum_prod_nonzip = (&a * &b).sum(); // Using `Zip`. This is a single loop without any heap allocations. let sum_prod_zip = Zip::from(&a).and(&b).fold(0, |acc, a, b| acc + a * b); assert_eq!(sum_prod_nonzip, sum_prod_zip);
pub fn fold_while<F, Acc>(self, acc: Acc, function: F) -> FoldWhile<Acc> where
F: FnMut(Acc, <P1 as NdProducer>::Item, <P2 as NdProducer>::Item) -> FoldWhile<Acc>,
[src]
F: FnMut(Acc, <P1 as NdProducer>::Item, <P2 as NdProducer>::Item) -> FoldWhile<Acc>,
Apply a fold function to the input arrays while the return
value is FoldWhile::Continue
, visiting elements in lock step.
pub fn all<F>(self, predicate: F) -> bool where
F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item) -> bool,
[src]
F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item) -> bool,
Tests if every element of the iterator matches a predicate.
Returns true
if predicate
evaluates to true
for all elements.
Returns true
if the input arrays are empty.
Example:
use ndarray::{array, Zip}; let a = array![1, 2, 3]; let b = array![1, 4, 9]; assert!(Zip::from(&a).and(&b).all(|&a, &b| a * a == b));
pub fn and<P>(self, p: P) -> Zip<(P1, P2, <P as IntoNdProducer>::Output), D> where
P: IntoNdProducer<Dim = D>,
[src]
P: IntoNdProducer<Dim = D>,
Include the producer p
in the Zip.
Panics if p
’s shape doesn’t match the Zip’s exactly.
pub fn and_broadcast<'a, P, D2, Elem>(
self,
p: P
) -> Zip<(P1, P2, ArrayBase<ViewRepr<&'a Elem>, D>), D> where
D2: Dimension,
P: IntoNdProducer<Dim = D2, Output = ArrayBase<ViewRepr<&'a Elem>, D2>, Item = &'a Elem>,
[src]
self,
p: P
) -> Zip<(P1, P2, ArrayBase<ViewRepr<&'a Elem>, D>), D> where
D2: Dimension,
P: IntoNdProducer<Dim = D2, Output = ArrayBase<ViewRepr<&'a Elem>, D2>, Item = &'a Elem>,
Include the producer p
in the Zip, broadcasting if needed.
If their shapes disagree, rhs
is broadcast to the shape of self
.
Panics if broadcasting isn’t possible.
pub fn split(self) -> (Zip<(P1, P2), D>, Zip<(P1, P2), D>)
[src]
Split the Zip
evenly in two.
It will be split in the way that best preserves element locality.
impl<D, P1, P2, P3> Zip<(P1, P2, P3), D> where
D: Dimension,
P1: NdProducer<Dim = D>,
P2: NdProducer<Dim = D>,
P3: NdProducer<Dim = D>,
[src]
D: Dimension,
P1: NdProducer<Dim = D>,
P2: NdProducer<Dim = D>,
P3: NdProducer<Dim = D>,
pub fn apply<F>(self, function: F) where
F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item),
[src]
F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item),
Apply a function to all elements of the input arrays, visiting elements in lock step.
pub fn fold<F, Acc>(self, acc: Acc, function: F) -> Acc where
F: FnMut(Acc, <P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item) -> Acc,
[src]
F: FnMut(Acc, <P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item) -> Acc,
Apply a fold function to all elements of the input arrays, visiting elements in lock step.
Example
The expression tr(AᵀB)
can be more efficiently computed as
the equivalent expression ∑ᵢⱼ(A∘B)ᵢⱼ
(i.e. the sum of the
elements of the entry-wise product). It would be possible to
evaluate this expression by first computing the entry-wise
product, A∘B
, and then computing the elementwise sum of that
product, but it's possible to do this in a single loop (and
avoid an extra heap allocation if A
and B
can't be
consumed) by using Zip
:
use ndarray::{array, Zip}; let a = array![[1, 5], [3, 7]]; let b = array![[2, 4], [8, 6]]; // Without using `Zip`. This involves two loops and an extra // heap allocation for the result of `&a * &b`. let sum_prod_nonzip = (&a * &b).sum(); // Using `Zip`. This is a single loop without any heap allocations. let sum_prod_zip = Zip::from(&a).and(&b).fold(0, |acc, a, b| acc + a * b); assert_eq!(sum_prod_nonzip, sum_prod_zip);
pub fn fold_while<F, Acc>(self, acc: Acc, function: F) -> FoldWhile<Acc> where
F: FnMut(Acc, <P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item) -> FoldWhile<Acc>,
[src]
F: FnMut(Acc, <P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item) -> FoldWhile<Acc>,
Apply a fold function to the input arrays while the return
value is FoldWhile::Continue
, visiting elements in lock step.
pub fn all<F>(self, predicate: F) -> bool where
F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item) -> bool,
[src]
F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item) -> bool,
Tests if every element of the iterator matches a predicate.
Returns true
if predicate
evaluates to true
for all elements.
Returns true
if the input arrays are empty.
Example:
use ndarray::{array, Zip}; let a = array![1, 2, 3]; let b = array![1, 4, 9]; assert!(Zip::from(&a).and(&b).all(|&a, &b| a * a == b));
pub fn and<P>(self, p: P) -> Zip<(P1, P2, P3, <P as IntoNdProducer>::Output), D> where
P: IntoNdProducer<Dim = D>,
[src]
P: IntoNdProducer<Dim = D>,
Include the producer p
in the Zip.
Panics if p
’s shape doesn’t match the Zip’s exactly.
pub fn and_broadcast<'a, P, D2, Elem>(
self,
p: P
) -> Zip<(P1, P2, P3, ArrayBase<ViewRepr<&'a Elem>, D>), D> where
D2: Dimension,
P: IntoNdProducer<Dim = D2, Output = ArrayBase<ViewRepr<&'a Elem>, D2>, Item = &'a Elem>,
[src]
self,
p: P
) -> Zip<(P1, P2, P3, ArrayBase<ViewRepr<&'a Elem>, D>), D> where
D2: Dimension,
P: IntoNdProducer<Dim = D2, Output = ArrayBase<ViewRepr<&'a Elem>, D2>, Item = &'a Elem>,
Include the producer p
in the Zip, broadcasting if needed.
If their shapes disagree, rhs
is broadcast to the shape of self
.
Panics if broadcasting isn’t possible.
pub fn split(self) -> (Zip<(P1, P2, P3), D>, Zip<(P1, P2, P3), D>)
[src]
Split the Zip
evenly in two.
It will be split in the way that best preserves element locality.
impl<D, P1, P2, P3, P4> Zip<(P1, P2, P3, P4), D> where
D: Dimension,
P1: NdProducer<Dim = D>,
P2: NdProducer<Dim = D>,
P3: NdProducer<Dim = D>,
P4: NdProducer<Dim = D>,
[src]
D: Dimension,
P1: NdProducer<Dim = D>,
P2: NdProducer<Dim = D>,
P3: NdProducer<Dim = D>,
P4: NdProducer<Dim = D>,
pub fn apply<F>(self, function: F) where
F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item),
[src]
F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item),
Apply a function to all elements of the input arrays, visiting elements in lock step.
pub fn fold<F, Acc>(self, acc: Acc, function: F) -> Acc where
F: FnMut(Acc, <P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item) -> Acc,
[src]
F: FnMut(Acc, <P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item) -> Acc,
Apply a fold function to all elements of the input arrays, visiting elements in lock step.
Example
The expression tr(AᵀB)
can be more efficiently computed as
the equivalent expression ∑ᵢⱼ(A∘B)ᵢⱼ
(i.e. the sum of the
elements of the entry-wise product). It would be possible to
evaluate this expression by first computing the entry-wise
product, A∘B
, and then computing the elementwise sum of that
product, but it's possible to do this in a single loop (and
avoid an extra heap allocation if A
and B
can't be
consumed) by using Zip
:
use ndarray::{array, Zip}; let a = array![[1, 5], [3, 7]]; let b = array![[2, 4], [8, 6]]; // Without using `Zip`. This involves two loops and an extra // heap allocation for the result of `&a * &b`. let sum_prod_nonzip = (&a * &b).sum(); // Using `Zip`. This is a single loop without any heap allocations. let sum_prod_zip = Zip::from(&a).and(&b).fold(0, |acc, a, b| acc + a * b); assert_eq!(sum_prod_nonzip, sum_prod_zip);
pub fn fold_while<F, Acc>(self, acc: Acc, function: F) -> FoldWhile<Acc> where
F: FnMut(Acc, <P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item) -> FoldWhile<Acc>,
[src]
F: FnMut(Acc, <P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item) -> FoldWhile<Acc>,
Apply a fold function to the input arrays while the return
value is FoldWhile::Continue
, visiting elements in lock step.
pub fn all<F>(self, predicate: F) -> bool where
F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item) -> bool,
[src]
F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item) -> bool,
Tests if every element of the iterator matches a predicate.
Returns true
if predicate
evaluates to true
for all elements.
Returns true
if the input arrays are empty.
Example:
use ndarray::{array, Zip}; let a = array![1, 2, 3]; let b = array![1, 4, 9]; assert!(Zip::from(&a).and(&b).all(|&a, &b| a * a == b));
pub fn and<P>(
self,
p: P
) -> Zip<(P1, P2, P3, P4, <P as IntoNdProducer>::Output), D> where
P: IntoNdProducer<Dim = D>,
[src]
self,
p: P
) -> Zip<(P1, P2, P3, P4, <P as IntoNdProducer>::Output), D> where
P: IntoNdProducer<Dim = D>,
Include the producer p
in the Zip.
Panics if p
’s shape doesn’t match the Zip’s exactly.
pub fn and_broadcast<'a, P, D2, Elem>(
self,
p: P
) -> Zip<(P1, P2, P3, P4, ArrayBase<ViewRepr<&'a Elem>, D>), D> where
D2: Dimension,
P: IntoNdProducer<Dim = D2, Output = ArrayBase<ViewRepr<&'a Elem>, D2>, Item = &'a Elem>,
[src]
self,
p: P
) -> Zip<(P1, P2, P3, P4, ArrayBase<ViewRepr<&'a Elem>, D>), D> where
D2: Dimension,
P: IntoNdProducer<Dim = D2, Output = ArrayBase<ViewRepr<&'a Elem>, D2>, Item = &'a Elem>,
Include the producer p
in the Zip, broadcasting if needed.
If their shapes disagree, rhs
is broadcast to the shape of self
.
Panics if broadcasting isn’t possible.
pub fn split(self) -> (Zip<(P1, P2, P3, P4), D>, Zip<(P1, P2, P3, P4), D>)
[src]
Split the Zip
evenly in two.
It will be split in the way that best preserves element locality.
impl<D, P1, P2, P3, P4, P5> Zip<(P1, P2, P3, P4, P5), D> where
D: Dimension,
P1: NdProducer<Dim = D>,
P2: NdProducer<Dim = D>,
P3: NdProducer<Dim = D>,
P4: NdProducer<Dim = D>,
P5: NdProducer<Dim = D>,
[src]
D: Dimension,
P1: NdProducer<Dim = D>,
P2: NdProducer<Dim = D>,
P3: NdProducer<Dim = D>,
P4: NdProducer<Dim = D>,
P5: NdProducer<Dim = D>,
pub fn apply<F>(self, function: F) where
F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item, <P5 as NdProducer>::Item),
[src]
F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item, <P5 as NdProducer>::Item),
Apply a function to all elements of the input arrays, visiting elements in lock step.
pub fn fold<F, Acc>(self, acc: Acc, function: F) -> Acc where
F: FnMut(Acc, <P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item, <P5 as NdProducer>::Item) -> Acc,
[src]
F: FnMut(Acc, <P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item, <P5 as NdProducer>::Item) -> Acc,
Apply a fold function to all elements of the input arrays, visiting elements in lock step.
Example
The expression tr(AᵀB)
can be more efficiently computed as
the equivalent expression ∑ᵢⱼ(A∘B)ᵢⱼ
(i.e. the sum of the
elements of the entry-wise product). It would be possible to
evaluate this expression by first computing the entry-wise
product, A∘B
, and then computing the elementwise sum of that
product, but it's possible to do this in a single loop (and
avoid an extra heap allocation if A
and B
can't be
consumed) by using Zip
:
use ndarray::{array, Zip}; let a = array![[1, 5], [3, 7]]; let b = array![[2, 4], [8, 6]]; // Without using `Zip`. This involves two loops and an extra // heap allocation for the result of `&a * &b`. let sum_prod_nonzip = (&a * &b).sum(); // Using `Zip`. This is a single loop without any heap allocations. let sum_prod_zip = Zip::from(&a).and(&b).fold(0, |acc, a, b| acc + a * b); assert_eq!(sum_prod_nonzip, sum_prod_zip);
pub fn fold_while<F, Acc>(self, acc: Acc, function: F) -> FoldWhile<Acc> where
F: FnMut(Acc, <P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item, <P5 as NdProducer>::Item) -> FoldWhile<Acc>,
[src]
F: FnMut(Acc, <P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item, <P5 as NdProducer>::Item) -> FoldWhile<Acc>,
Apply a fold function to the input arrays while the return
value is FoldWhile::Continue
, visiting elements in lock step.
pub fn all<F>(self, predicate: F) -> bool where
F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item, <P5 as NdProducer>::Item) -> bool,
[src]
F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item, <P5 as NdProducer>::Item) -> bool,
Tests if every element of the iterator matches a predicate.
Returns true
if predicate
evaluates to true
for all elements.
Returns true
if the input arrays are empty.
Example:
use ndarray::{array, Zip}; let a = array![1, 2, 3]; let b = array![1, 4, 9]; assert!(Zip::from(&a).and(&b).all(|&a, &b| a * a == b));
pub fn and<P>(
self,
p: P
) -> Zip<(P1, P2, P3, P4, P5, <P as IntoNdProducer>::Output), D> where
P: IntoNdProducer<Dim = D>,
[src]
self,
p: P
) -> Zip<(P1, P2, P3, P4, P5, <P as IntoNdProducer>::Output), D> where
P: IntoNdProducer<Dim = D>,
Include the producer p
in the Zip.
Panics if p
’s shape doesn’t match the Zip’s exactly.
pub fn and_broadcast<'a, P, D2, Elem>(
self,
p: P
) -> Zip<(P1, P2, P3, P4, P5, ArrayBase<ViewRepr<&'a Elem>, D>), D> where
D2: Dimension,
P: IntoNdProducer<Dim = D2, Output = ArrayBase<ViewRepr<&'a Elem>, D2>, Item = &'a Elem>,
[src]
self,
p: P
) -> Zip<(P1, P2, P3, P4, P5, ArrayBase<ViewRepr<&'a Elem>, D>), D> where
D2: Dimension,
P: IntoNdProducer<Dim = D2, Output = ArrayBase<ViewRepr<&'a Elem>, D2>, Item = &'a Elem>,
Include the producer p
in the Zip, broadcasting if needed.
If their shapes disagree, rhs
is broadcast to the shape of self
.
Panics if broadcasting isn’t possible.
pub fn split(
self
) -> (Zip<(P1, P2, P3, P4, P5), D>, Zip<(P1, P2, P3, P4, P5), D>)
[src]
self
) -> (Zip<(P1, P2, P3, P4, P5), D>, Zip<(P1, P2, P3, P4, P5), D>)
Split the Zip
evenly in two.
It will be split in the way that best preserves element locality.
impl<D, P1, P2, P3, P4, P5, P6> Zip<(P1, P2, P3, P4, P5, P6), D> where
D: Dimension,
P1: NdProducer<Dim = D>,
P2: NdProducer<Dim = D>,
P3: NdProducer<Dim = D>,
P4: NdProducer<Dim = D>,
P5: NdProducer<Dim = D>,
P6: NdProducer<Dim = D>,
[src]
D: Dimension,
P1: NdProducer<Dim = D>,
P2: NdProducer<Dim = D>,
P3: NdProducer<Dim = D>,
P4: NdProducer<Dim = D>,
P5: NdProducer<Dim = D>,
P6: NdProducer<Dim = D>,
pub fn apply<F>(self, function: F) where
F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item, <P5 as NdProducer>::Item, <P6 as NdProducer>::Item),
[src]
F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item, <P5 as NdProducer>::Item, <P6 as NdProducer>::Item),
Apply a function to all elements of the input arrays, visiting elements in lock step.
pub fn fold<F, Acc>(self, acc: Acc, function: F) -> Acc where
F: FnMut(Acc, <P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item, <P5 as NdProducer>::Item, <P6 as NdProducer>::Item) -> Acc,
[src]
F: FnMut(Acc, <P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item, <P5 as NdProducer>::Item, <P6 as NdProducer>::Item) -> Acc,
Apply a fold function to all elements of the input arrays, visiting elements in lock step.
Example
The expression tr(AᵀB)
can be more efficiently computed as
the equivalent expression ∑ᵢⱼ(A∘B)ᵢⱼ
(i.e. the sum of the
elements of the entry-wise product). It would be possible to
evaluate this expression by first computing the entry-wise
product, A∘B
, and then computing the elementwise sum of that
product, but it's possible to do this in a single loop (and
avoid an extra heap allocation if A
and B
can't be
consumed) by using Zip
:
use ndarray::{array, Zip}; let a = array![[1, 5], [3, 7]]; let b = array![[2, 4], [8, 6]]; // Without using `Zip`. This involves two loops and an extra // heap allocation for the result of `&a * &b`. let sum_prod_nonzip = (&a * &b).sum(); // Using `Zip`. This is a single loop without any heap allocations. let sum_prod_zip = Zip::from(&a).and(&b).fold(0, |acc, a, b| acc + a * b); assert_eq!(sum_prod_nonzip, sum_prod_zip);
pub fn fold_while<F, Acc>(self, acc: Acc, function: F) -> FoldWhile<Acc> where
F: FnMut(Acc, <P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item, <P5 as NdProducer>::Item, <P6 as NdProducer>::Item) -> FoldWhile<Acc>,
[src]
F: FnMut(Acc, <P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item, <P5 as NdProducer>::Item, <P6 as NdProducer>::Item) -> FoldWhile<Acc>,
Apply a fold function to the input arrays while the return
value is FoldWhile::Continue
, visiting elements in lock step.
pub fn all<F>(self, predicate: F) -> bool where
F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item, <P5 as NdProducer>::Item, <P6 as NdProducer>::Item) -> bool,
[src]
F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item, <P5 as NdProducer>::Item, <P6 as NdProducer>::Item) -> bool,
Tests if every element of the iterator matches a predicate.
Returns true
if predicate
evaluates to true
for all elements.
Returns true
if the input arrays are empty.
Example:
use ndarray::{array, Zip}; let a = array![1, 2, 3]; let b = array![1, 4, 9]; assert!(Zip::from(&a).and(&b).all(|&a, &b| a * a == b));
pub fn split(
self
) -> (Zip<(P1, P2, P3, P4, P5, P6), D>, Zip<(P1, P2, P3, P4, P5, P6), D>)
[src]
self
) -> (Zip<(P1, P2, P3, P4, P5, P6), D>, Zip<(P1, P2, P3, P4, P5, P6), D>)
Split the Zip
evenly in two.
It will be split in the way that best preserves element locality.
Trait Implementations
impl<Parts, D> Clone for Zip<Parts, D> where
D: Clone,
Parts: Clone,
[src]
D: Clone,
Parts: Clone,
impl<Parts, D> Debug for Zip<Parts, D> where
D: Debug,
Parts: Debug,
[src]
D: Debug,
Parts: Debug,
Auto Trait Implementations
impl<Parts, D> RefUnwindSafe for Zip<Parts, D> where
D: RefUnwindSafe,
Parts: RefUnwindSafe,
D: RefUnwindSafe,
Parts: RefUnwindSafe,
impl<Parts, D> Send for Zip<Parts, D> where
D: Send,
Parts: Send,
D: Send,
Parts: Send,
impl<Parts, D> Sync for Zip<Parts, D> where
D: Sync,
Parts: Sync,
D: Sync,
Parts: Sync,
impl<Parts, D> Unpin for Zip<Parts, D> where
D: Unpin,
Parts: Unpin,
D: Unpin,
Parts: Unpin,
impl<Parts, D> UnwindSafe for Zip<Parts, D> where
D: UnwindSafe,
Parts: UnwindSafe,
D: UnwindSafe,
Parts: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow(&self) -> &TⓘNotable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;
[src]
Notable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut TⓘNotable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;
[src]
Notable traits for &'_ mut W
impl<'_, W> Write for &'_ mut W where
W: Write + ?Sized, impl<'_, R> Read for &'_ mut R where
R: Read + ?Sized, impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;
impl<T> Downcast for T where
T: Any,
T: Any,
fn into_any(self: Box<T>) -> Box<dyn Any + 'static>ⓘNotable traits for Box<W>
impl<W> Write for Box<W> where
W: Write + ?Sized, impl<R> Read for Box<R> where
R: Read + ?Sized, impl<F> Future for Box<F> where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<I> Iterator for Box<I> where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
Notable traits for Box<W>
impl<W> Write for Box<W> where
W: Write + ?Sized, impl<R> Read for Box<R> where
R: Read + ?Sized, impl<F> Future for Box<F> where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;impl<I> Iterator for Box<I> where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn as_any(&self) -> &(dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
impl<T> DowncastSync for T where
T: Send + Sync + Any,
T: Send + Sync + Any,
impl<T> DynClone for T where
T: Clone,
[src]
T: Clone,
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,