Struct rerun::external::ndarray::Zip

source ·
pub struct Zip<Parts, D> {
    parts: Parts,
    dimension: D,
    layout: Layout,
    layout_tendency: i32,
}
Expand description

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: for_each 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!() 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)
    .for_each(|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.rows())
    .for_each(|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)));


// Example 3: Recreate Example 2 using map_collect to make a new array

let totals2 = Zip::from(a.rows()).map_collect(|row| row.sum());

// Check the result against the previous example.
assert_eq!(totals, totals2);

Fields§

§parts: Parts§dimension: D§layout: Layout§layout_tendency: i32

Implementations§

source§

impl<P, D> Zip<(P,), D>
where D: Dimension, P: NdProducer<Dim = D>,

source

pub fn from<IP>(p: IP) -> Zip<(P,), D>
where 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).

source§

impl<P, D> Zip<(Indices<D>, P), D>
where D: Dimension + Copy, P: NdProducer<Dim = D>,

source

pub fn indexed<IP>(p: IP) -> Zip<(Indices<D>, P), D>
where 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.

source§

impl<Parts, D> Zip<Parts, D>
where D: Dimension,

source

pub fn size(&self) -> usize

Return a the number of element tuples in the Zip

source§

impl<D, P1> Zip<(P1,), D>
where D: Dimension, P1: NdProducer<Dim = D>,

source

pub fn for_each<F>(self, function: F)
where F: FnMut(<P1 as NdProducer>::Item),

Apply a function to all elements of the input arrays, visiting elements in lock step.

source

pub fn fold<F, Acc>(self, acc: Acc, function: F) -> Acc
where 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);
source

pub fn fold_while<F, Acc>(self, acc: Acc, function: F) -> FoldWhile<Acc>
where 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.

source

pub fn all<F>(self, predicate: F) -> bool
where 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));
source

pub fn any<F>(self, predicate: F) -> bool
where F: FnMut(<P1 as NdProducer>::Item) -> bool,

Tests if at least one element of the iterator matches a predicate.

Returns true if predicate evaluates to true for at least one element. Returns false 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).any(|&a, &b| a == b));
assert!(!Zip::from(&a).and(&b).any(|&a, &b| a - 1 == b));
source

pub fn and<P>(self, p: P) -> Zip<(P1, <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.

source

pub fn and_broadcast<'a, P, D2, Elem>( self, p: P ) -> Zip<(P1, ArrayBase<ViewRepr<&'a Elem>, D>), D>
where P: IntoNdProducer<Dim = D2, Output = ArrayBase<ViewRepr<&'a Elem>, D2>, Item = &'a Elem>, D2: Dimension,

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.

source

pub fn map_collect<R>( self, f: impl FnMut(<P1 as NdProducer>::Item) -> R ) -> ArrayBase<OwnedRepr<R>, D>

Map and collect the results into a new array, which has the same size as the inputs.

If all inputs are c- or f-order respectively, that is preserved in the output.

source

pub fn map_assign_into<R, Q>( self, into: Q, f: impl FnMut(<P1 as NdProducer>::Item) -> R )
where Q: IntoNdProducer<Dim = D>, <Q as IntoNdProducer>::Item: AssignElem<R>,

Map and assign the results into the producer into, which should have the same size as the other inputs.

The producer should have assignable items as dictated by the AssignElem trait, for example &mut R.

source

pub fn split(self) -> (Zip<(P1,), D>, Zip<(P1,), D>)

Split the Zip evenly in two.

It will be split in the way that best preserves element locality.

source§

impl<D, P1, P2> Zip<(P1, P2), D>
where D: Dimension, P1: NdProducer<Dim = D>, P2: NdProducer<Dim = D>,

source

pub fn for_each<F>(self, function: F)
where 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.

source

pub fn fold<F, Acc>(self, acc: Acc, function: F) -> Acc
where 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);
source

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>,

Apply a fold function to the input arrays while the return value is FoldWhile::Continue, visiting elements in lock step.

source

pub fn all<F>(self, predicate: F) -> bool
where 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));
source

pub fn any<F>(self, predicate: F) -> bool
where F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item) -> bool,

Tests if at least one element of the iterator matches a predicate.

Returns true if predicate evaluates to true for at least one element. Returns false 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).any(|&a, &b| a == b));
assert!(!Zip::from(&a).and(&b).any(|&a, &b| a - 1 == b));
source

pub fn and<P>(self, p: P) -> Zip<(P1, P2, <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.

source

pub fn and_broadcast<'a, P, D2, Elem>( self, p: P ) -> Zip<(P1, P2, ArrayBase<ViewRepr<&'a Elem>, D>), D>
where P: IntoNdProducer<Dim = D2, Output = ArrayBase<ViewRepr<&'a Elem>, D2>, Item = &'a Elem>, D2: Dimension,

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.

source

pub fn map_collect<R>( self, f: impl FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item) -> R ) -> ArrayBase<OwnedRepr<R>, D>

Map and collect the results into a new array, which has the same size as the inputs.

If all inputs are c- or f-order respectively, that is preserved in the output.

source

pub fn map_assign_into<R, Q>( self, into: Q, f: impl FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item) -> R )
where Q: IntoNdProducer<Dim = D>, <Q as IntoNdProducer>::Item: AssignElem<R>,

Map and assign the results into the producer into, which should have the same size as the other inputs.

The producer should have assignable items as dictated by the AssignElem trait, for example &mut R.

source

pub fn split(self) -> (Zip<(P1, P2), D>, Zip<(P1, P2), D>)

Split the Zip evenly in two.

It will be split in the way that best preserves element locality.

source§

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>,

source

pub fn for_each<F>(self, function: F)
where 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.

source

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,

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);
source

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>,

Apply a fold function to the input arrays while the return value is FoldWhile::Continue, visiting elements in lock step.

source

pub fn all<F>(self, predicate: F) -> bool
where 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));
source

pub fn any<F>(self, predicate: F) -> bool
where F: FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item) -> bool,

Tests if at least one element of the iterator matches a predicate.

Returns true if predicate evaluates to true for at least one element. Returns false 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).any(|&a, &b| a == b));
assert!(!Zip::from(&a).and(&b).any(|&a, &b| a - 1 == b));
source

pub fn and<P>(self, p: P) -> Zip<(P1, P2, P3, <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.

source

pub fn and_broadcast<'a, P, D2, Elem>( self, p: P ) -> Zip<(P1, P2, P3, ArrayBase<ViewRepr<&'a Elem>, D>), D>
where P: IntoNdProducer<Dim = D2, Output = ArrayBase<ViewRepr<&'a Elem>, D2>, Item = &'a Elem>, D2: Dimension,

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.

source

pub fn map_collect<R>( self, f: impl FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item) -> R ) -> ArrayBase<OwnedRepr<R>, D>

Map and collect the results into a new array, which has the same size as the inputs.

If all inputs are c- or f-order respectively, that is preserved in the output.

source

pub fn map_assign_into<R, Q>( self, into: Q, f: impl FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item) -> R )
where Q: IntoNdProducer<Dim = D>, <Q as IntoNdProducer>::Item: AssignElem<R>,

Map and assign the results into the producer into, which should have the same size as the other inputs.

The producer should have assignable items as dictated by the AssignElem trait, for example &mut R.

source

pub fn split(self) -> (Zip<(P1, P2, P3), D>, Zip<(P1, P2, P3), D>)

Split the Zip evenly in two.

It will be split in the way that best preserves element locality.

source§

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>,

source

pub fn for_each<F>(self, function: F)
where 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.

source

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,

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);
source

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>,

Apply a fold function to the input arrays while the return value is FoldWhile::Continue, visiting elements in lock step.

source

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,

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));
source

pub fn any<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,

Tests if at least one element of the iterator matches a predicate.

Returns true if predicate evaluates to true for at least one element. Returns false 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).any(|&a, &b| a == b));
assert!(!Zip::from(&a).and(&b).any(|&a, &b| a - 1 == b));
source

pub fn and<P>( 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.

source

pub fn and_broadcast<'a, P, D2, Elem>( self, p: P ) -> Zip<(P1, P2, P3, P4, ArrayBase<ViewRepr<&'a Elem>, D>), D>
where P: IntoNdProducer<Dim = D2, Output = ArrayBase<ViewRepr<&'a Elem>, D2>, Item = &'a Elem>, D2: Dimension,

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.

source

pub fn map_collect<R>( self, f: impl FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item) -> R ) -> ArrayBase<OwnedRepr<R>, D>

Map and collect the results into a new array, which has the same size as the inputs.

If all inputs are c- or f-order respectively, that is preserved in the output.

source

pub fn map_assign_into<R, Q>( self, into: Q, f: impl FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item) -> R )
where Q: IntoNdProducer<Dim = D>, <Q as IntoNdProducer>::Item: AssignElem<R>,

Map and assign the results into the producer into, which should have the same size as the other inputs.

The producer should have assignable items as dictated by the AssignElem trait, for example &mut R.

source

pub fn split(self) -> (Zip<(P1, P2, P3, P4), D>, Zip<(P1, P2, P3, P4), D>)

Split the Zip evenly in two.

It will be split in the way that best preserves element locality.

source§

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>,

source

pub fn for_each<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),

Apply a function to all elements of the input arrays, visiting elements in lock step.

source

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,

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);
source

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>,

Apply a fold function to the input arrays while the return value is FoldWhile::Continue, visiting elements in lock step.

source

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,

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));
source

pub fn any<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,

Tests if at least one element of the iterator matches a predicate.

Returns true if predicate evaluates to true for at least one element. Returns false 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).any(|&a, &b| a == b));
assert!(!Zip::from(&a).and(&b).any(|&a, &b| a - 1 == b));
source

pub fn and<P>( 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.

source

pub fn and_broadcast<'a, P, D2, Elem>( self, p: P ) -> Zip<(P1, P2, P3, P4, P5, ArrayBase<ViewRepr<&'a Elem>, D>), D>
where P: IntoNdProducer<Dim = D2, Output = ArrayBase<ViewRepr<&'a Elem>, D2>, Item = &'a Elem>, D2: Dimension,

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.

source

pub fn map_collect<R>( self, f: impl FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item, <P5 as NdProducer>::Item) -> R ) -> ArrayBase<OwnedRepr<R>, D>

Map and collect the results into a new array, which has the same size as the inputs.

If all inputs are c- or f-order respectively, that is preserved in the output.

source

pub fn map_assign_into<R, Q>( self, into: Q, f: impl FnMut(<P1 as NdProducer>::Item, <P2 as NdProducer>::Item, <P3 as NdProducer>::Item, <P4 as NdProducer>::Item, <P5 as NdProducer>::Item) -> R )
where Q: IntoNdProducer<Dim = D>, <Q as IntoNdProducer>::Item: AssignElem<R>,

Map and assign the results into the producer into, which should have the same size as the other inputs.

The producer should have assignable items as dictated by the AssignElem trait, for example &mut R.

source

pub fn split( 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.

source§

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>,

source

pub fn for_each<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),

Apply a function to all elements of the input arrays, visiting elements in lock step.

source

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,

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);
source

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>,

Apply a fold function to the input arrays while the return value is FoldWhile::Continue, visiting elements in lock step.

source

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,

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));
source

pub fn any<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,

Tests if at least one element of the iterator matches a predicate.

Returns true if predicate evaluates to true for at least one element. Returns false 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).any(|&a, &b| a == b));
assert!(!Zip::from(&a).and(&b).any(|&a, &b| a - 1 == b));
source

pub fn split( 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§

source§

impl<Parts, D> Clone for Zip<Parts, D>
where Parts: Clone, D: Clone,

source§

fn clone(&self) -> Zip<Parts, D>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<Parts, D> Debug for Zip<Parts, D>
where Parts: Debug, D: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Parts, D> Freeze for Zip<Parts, D>
where Parts: Freeze, D: Freeze,

§

impl<Parts, D> RefUnwindSafe for Zip<Parts, D>
where Parts: RefUnwindSafe, D: RefUnwindSafe,

§

impl<Parts, D> Send for Zip<Parts, D>
where Parts: Send, D: Send,

§

impl<Parts, D> Sync for Zip<Parts, D>
where Parts: Sync, D: Sync,

§

impl<Parts, D> Unpin for Zip<Parts, D>
where Parts: Unpin, D: Unpin,

§

impl<Parts, D> UnwindSafe for Zip<Parts, D>
where Parts: UnwindSafe, D: UnwindSafe,

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> Az for T

source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

source§

fn cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> CheckedAs for T

source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
source§

impl<T> DynClone for T
where T: Clone,

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> IntoRequest<T> for T

source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
source§

impl<Src, Dst> LosslessTryInto<Dst> for Src
where Dst: LosslessTryFrom<Src>,

source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
source§

impl<Src, Dst> LossyInto<Dst> for Src
where Dst: LossyFrom<Src>,

source§

fn lossy_into(self) -> Dst

Performs the conversion.
source§

impl<T> OverflowingAs for T

source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> SaturatingAs for T

source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
§

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

§

fn to<T>(self) -> T
where Self: Into<T>,

Converts to T by calling Into<T>::into.
§

fn try_to<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Tries to convert to T by calling TryInto<T>::try_into.
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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>,

§

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.
source§

impl<T> UnwrappedAs for T

source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
source§

impl<T> WrappingAs for T

source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.
§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

§

impl<T> Ungil for T
where T: Send,

§

impl<T> WasmNotSend for T
where T: Send,

§

impl<T> WasmNotSendSync for T

§

impl<T> WasmNotSync for T
where T: Sync,