Struct ndarray::Zip[][src]

pub struct Zip<Parts, D> { /* fields omitted */ }
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: 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)));


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

let mut totals2 = Zip::from(a.genrows()).apply_collect(|row| row.sum());

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

Implementations

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).

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.

Return a the number of element tuples in the Zip

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

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

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

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

Include the producer p in the Zip.

Panics if p’s shape doesn’t match the Zip’s exactly.

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.

Apply 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.

Apply 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.

Split the Zip evenly in two.

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

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

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

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

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

Include the producer p in the Zip.

Panics if p’s shape doesn’t match the Zip’s exactly.

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.

Apply 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.

Apply 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.

Split the Zip evenly in two.

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

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

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

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

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

Include the producer p in the Zip.

Panics if p’s shape doesn’t match the Zip’s exactly.

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.

Apply 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.

Apply 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.

Split the Zip evenly in two.

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

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

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

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

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

Include the producer p in the Zip.

Panics if p’s shape doesn’t match the Zip’s exactly.

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.

Apply 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.

Apply 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.

Split the Zip evenly in two.

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

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

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

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

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

Include the producer p in the Zip.

Panics if p’s shape doesn’t match the Zip’s exactly.

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.

Apply 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.

Apply 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.

Split the Zip evenly in two.

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

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

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

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

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

Split the Zip evenly in two.

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

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.