Type Definition ndarray::Array[][src]

type Array<A, D> = ArrayBase<OwnedRepr<A>, D>;
Expand description

An array that owns its data uniquely.

Array is the main n-dimensional array type, and it owns all its array elements.

The Array<A, D> is parameterized by A for the element type and D for the dimensionality.

ArrayBase is used to implement both the owned arrays and the views; see its docs for an overview of all array features.

See also:

Implementations

impl<A> Array<A, Ix0>[src]

Methods specific to Array0.

See also all methods for ArrayBase

pub fn into_scalar(self) -> A[src]

Returns the single element in the array without cloning it.

use ndarray::{arr0, Array0};

// `Foo` doesn't implement `Clone`.
#[derive(Debug, Eq, PartialEq)]
struct Foo;

let array: Array0<Foo> = arr0(Foo);
let scalar: Foo = array.into_scalar();
assert_eq!(scalar, Foo);

impl<A, D> Array<A, D> where
    D: Dimension
[src]

Methods specific to Array.

See also all methods for ArrayBase

pub fn into_raw_vec(self) -> Vec<A>[src]

Return a vector of the elements in the array, in the way they are stored internally.

If the array is in standard memory layout, the logical element order of the array (.iter() order) and of the returned vector will be the same.

impl<A> Array<A, Ix2>[src]

Methods specific to Array2.

See also all methods for ArrayBase

pub fn push_row(&mut self, row: ArrayView<'_, A, Ix1>) -> Result<(), ShapeError> where
    A: Clone
[src]

Append a row to an array

Errors with a shape error if the length of the row does not match the length of the rows in the array.

The memory layout of the self array matters for ensuring that the append is efficient. Appending automatically changes memory layout of the array so that it is appended to along the “growing axis”.

Ensure appending is efficient by, for example, appending to an empty array and then always appending along the same axis. For rows, ndarray’s default layout is efficient for appending.

Notice that an empty array (where it has an axis of length zero) is the simplest starting point. When repeatedly appending to a single axis, the amortized average complexity of each append is O(m), where m is the length of the row.

use ndarray::{Array, ArrayView, array};

// create an empty array and append
let mut a = Array::zeros((0, 4));
a.push_row(ArrayView::from(&[ 1.,  2.,  3.,  4.])).unwrap();
a.push_row(ArrayView::from(&[-1., -2., -3., -4.])).unwrap();

assert_eq!(
    a,
    array![[ 1.,  2.,  3.,  4.],
           [-1., -2., -3., -4.]]);

pub fn push_column(
    &mut self,
    column: ArrayView<'_, A, Ix1>
) -> Result<(), ShapeError> where
    A: Clone
[src]

Append a column to an array

Errors with a shape error if the length of the column does not match the length of the columns in the array.

The memory layout of the self array matters for ensuring that the append is efficient. Appending automatically changes memory layout of the array so that it is appended to along the “growing axis”.

Ensure appending is efficient by, for example, appending to an empty array and then always appending along the same axis. For columns, column major (“F”) memory layout is efficient for appending.

Notice that an empty array (where it has an axis of length zero) is the simplest starting point. When repeatedly appending to a single axis, the amortized average complexity of each append is O(m), where m is the length of the row.

use ndarray::{Array, ArrayView, array};

// create an empty array and append
let mut a = Array::zeros((2, 0));
a.push_column(ArrayView::from(&[1., 2.])).unwrap();
a.push_column(ArrayView::from(&[-1., -2.])).unwrap();

assert_eq!(
    a,
    array![[1., -1.],
           [2., -2.]]);

impl<A, D> Array<A, D> where
    D: Dimension
[src]

pub fn move_into<'a, AM>(self, new_array: AM) where
    AM: Into<ArrayViewMut<'a, A, D>>,
    A: 'a, 
[src]

Move all elements from self into new_array, which must be of the same shape but can have a different memory layout. The destination is overwritten completely.

The destination should be a mut reference to an array or an ArrayViewMut with A elements.

Panics if the shapes don’t agree.

Example

use ndarray::Array;

// Usage example of move_into in safe code
let mut a = Array::default((10, 10));
let b = Array::from_shape_fn((10, 10), |(i, j)| (i + j).to_string());
b.move_into(&mut a);

pub fn move_into_uninit<'a, AM>(self, new_array: AM) where
    AM: Into<ArrayViewMut<'a, MaybeUninit<A>, D>>,
    A: 'a, 
[src]

Move all elements from self into new_array, which must be of the same shape but can have a different memory layout. The destination is overwritten completely.

The destination should be a mut reference to an array or an ArrayViewMut with MaybeUninit<A> elements (which are overwritten without dropping any existing value).

Minor implementation note: Owned arrays like self may be sliced in place and own elements that are not part of their active view; these are dropped at the end of this function, after all elements in the “active view” are moved into new_array. If there is a panic in drop of any such element, other elements may be leaked.

Panics if the shapes don’t agree.

Example

use ndarray::Array;

let a = Array::from_iter(0..100).into_shape((10, 10)).unwrap();
let mut b = Array::uninit((10, 10));
a.move_into_uninit(&mut b);
unsafe {
    // we can now promise we have fully initialized `b`.
    let b = b.assume_init();
}

pub fn push(
    &mut self,
    axis: Axis,
    array: ArrayView<'_, A, D::Smaller>
) -> Result<(), ShapeError> where
    A: Clone,
    D: RemoveAxis
[src]

Append an array to the array along an axis

Where the item to push to the array has one dimension less than the self array. This method is equivalent to self.append(axis, array.insert_axis(axis)).

The axis-to-append-to axis must be the array’s “growing axis” for this operation to succeed. The growing axis is the outermost or last-visited when elements are visited in memory order:

axis must be the growing axis of the current array, an axis with length 0 or 1.

  • This is the 0th axis for standard layout arrays
  • This is the n-1 th axis for fortran layout arrays
  • If the array is empty (the axis or any other has length 0) or if axis has length 1, then the array can always be appended.

Errors with a shape error if the shape of self does not match the array-to-append; all axes except the axis along which it being appended matter for this check.

The memory layout of the self array matters for ensuring that the append is efficient. Appending automatically changes memory layout of the array so that it is appended to along the “growing axis”.

Ensure appending is efficient by for example starting from an empty array and/or always appending to an array along the same axis.

The amortized average complexity of the append, when appending along its growing axis, is O(m) where m is the length of the row.

The memory layout of the argument array does not matter to the same extent.

use ndarray::{Array, ArrayView, array, Axis};

// create an empty array and push rows to it
let mut a = Array::zeros((0, 4));
let ones  = ArrayView::from(&[1.; 4]);
let zeros = ArrayView::from(&[0.; 4]);
a.push(Axis(0), ones).unwrap();
a.push(Axis(0), zeros).unwrap();
a.push(Axis(0), ones).unwrap();

assert_eq!(
    a,
    array![[1., 1., 1., 1.],
           [0., 0., 0., 0.],
           [1., 1., 1., 1.]]);

pub fn append(
    &mut self,
    axis: Axis,
    array: ArrayView<'_, A, D>
) -> Result<(), ShapeError> where
    A: Clone,
    D: RemoveAxis
[src]

Append an array to the array along an axis

The axis-to-append-to axis must be the array’s “growing axis” for this operation to succeed. The growing axis is the outermost or last-visited when elements are visited in memory order:

axis must be the growing axis of the current array, an axis with length 0 or 1.

  • This is the 0th axis for standard layout arrays
  • This is the n-1 th axis for fortran layout arrays
  • If the array is empty (the axis or any other has length 0) or if axis has length 1, then the array can always be appended.

Errors with a shape error if the shape of self does not match the array-to-append; all axes except the axis along which it being appended matter for this check.

The memory layout of the self array matters for ensuring that the append is efficient. Appending automatically changes memory layout of the array so that it is appended to along the “growing axis”.

Ensure appending is efficient by for example starting from an empty array and/or always appending to an array along the same axis.

The amortized average complexity of the append, when appending along its growing axis, is O(m) where m is the length of the row.

The memory layout of the argument array does not matter to the same extent.

use ndarray::{Array, ArrayView, array, Axis};

// create an empty array and append
let mut a = Array::zeros((0, 4));
let ones  = ArrayView::from(&[1.; 8]).into_shape((2, 4)).unwrap();
let zeros = ArrayView::from(&[0.; 8]).into_shape((2, 4)).unwrap();
a.append(Axis(0), ones).unwrap();
a.append(Axis(0), zeros).unwrap();
a.append(Axis(0), ones).unwrap();

assert_eq!(
    a,
    array![[1., 1., 1., 1.],
           [1., 1., 1., 1.],
           [0., 0., 0., 0.],
           [0., 0., 0., 0.],
           [1., 1., 1., 1.],
           [1., 1., 1., 1.]]);

Trait Implementations

impl<A, D> IntoIterator for Array<A, D> where
    D: Dimension
[src]

type Item = A

The type of the elements being iterated over.

type IntoIter = IntoIter<A, D>

Which kind of iterator are we turning this into?

fn into_iter(self) -> Self::IntoIter[src]

Creates an iterator from a value. Read more

impl<'a, A, D> IntoParallelIterator for &'a Array<A, D> where
    D: Dimension,
    A: Sync
[src]

Requires crate feature rayon.

type Item = &'a A

The type of item that the parallel iterator will produce.

type Iter = Parallel<ArrayView<'a, A, D>>

The parallel iterator type that will be created.

fn into_par_iter(self) -> Self::Iter[src]

Converts self into a parallel iterator. Read more

impl<'a, A, D> IntoParallelIterator for &'a mut Array<A, D> where
    D: Dimension,
    A: Sync + Send
[src]

Requires crate feature rayon.

type Item = &'a mut A

The type of item that the parallel iterator will produce.

type Iter = Parallel<ArrayViewMut<'a, A, D>>

The parallel iterator type that will be created.

fn into_par_iter(self) -> Self::Iter[src]

Converts self into a parallel iterator. Read more