Skip to content

Add insert/remove_axis_inplace for IxDyn arrays #533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/impl_dyn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2018 bluss and ndarray developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Methods for dynamic-dimensional arrays.
use imp_prelude::*;

/// # Methods for Dynamic-Dimensional Arrays
impl<A, S> ArrayBase<S, IxDyn>
where
S: Data<Elem = A>,
{
/// Insert new array axis of length 1 at `axis`, modifying the shape and
/// strides in-place.
///
/// **Panics** if the axis is out of bounds.
///
/// ```
/// use ndarray::{Axis, arr2, arr3};
///
/// let mut a = arr2(&[[1, 2, 3], [4, 5, 6]]).into_dyn();
/// assert_eq!(a.shape(), &[2, 3]);
///
/// a.insert_axis_inplace(Axis(1));
/// assert_eq!(a, arr3(&[[[1, 2, 3]], [[4, 5, 6]]]).into_dyn());
/// assert_eq!(a.shape(), &[2, 1, 3]);
/// ```
pub fn insert_axis_inplace(&mut self, axis: Axis) {
assert!(axis.index() <= self.ndim());
self.dim = self.dim.insert_axis(axis);
self.strides = self.strides.insert_axis(axis);
}

/// Remove array axis `axis`, modifying the shape and strides in-place.
///
/// **Panics** if the axis is out of bounds or its length is zero.
///
/// ```
/// use ndarray::{Axis, arr1, arr2};
///
/// let mut a = arr2(&[[1, 2, 3], [4, 5, 6]]).into_dyn();
/// assert_eq!(a.shape(), &[2, 3]);
///
/// a.remove_axis_inplace(Axis(1));
/// assert_eq!(a, arr1(&[1, 4]).into_dyn());
/// assert_eq!(a.shape(), &[2]);
/// ```
pub fn remove_axis_inplace(&mut self, axis: Axis) {
let len = self.len_of(axis);
assert_ne!(len, 0, "Length of removed axis must be nonzero.");
self.dim = self.dim.remove_axis(axis);
self.strides = self.strides.remove_axis(axis);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess removing an axis can never make us lose the first element (at index [0, 0, ..]) right, so we don't need to recompute the array's pointer here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct. We need to assert that the axis length is nonzero for a similar reason.

}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ pub type Ixs = isize;
/// + [Methods For All Array Types](#methods-for-all-array-types)
/// + [Methods For 1-D Arrays](#methods-for-1-d-arrays)
/// + [Methods For 2-D Arrays](#methods-for-2-d-arrays)
/// + [Methods for Dynamic-Dimensional Arrays](#methods-for-dynamic-dimensional-arrays)
/// + [Numerical Methods for Arrays](#numerical-methods-for-arrays)
///
/// ## `Array`
Expand Down Expand Up @@ -1138,6 +1139,7 @@ impl<A, S, D> ArrayBase<S, D>

mod impl_1d;
mod impl_2d;
mod impl_dyn;

mod numeric;

Expand Down