Skip to content

Commit 90f65d6

Browse files
committed
Add insert/remove_axis_inplace for IxDyn arrays
Without these methods, users must have ownership of dynamic-dimensional arrays to insert/remove axes, which is unnecessarily restrictive.
1 parent ae13fd2 commit 90f65d6

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/impl_dyn.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2018 bluss and ndarray developers.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
//! Methods for dynamic-dimensional arrays.
10+
use imp_prelude::*;
11+
12+
/// # Methods for Dynamic-Dimensional Arrays
13+
impl<A, S> ArrayBase<S, IxDyn>
14+
where
15+
S: Data<Elem = A>,
16+
{
17+
/// Insert new array axis of length 1 at `axis`, modifying the shape and
18+
/// strides in-place.
19+
///
20+
/// **Panics** if the axis is out of bounds.
21+
///
22+
/// ```
23+
/// use ndarray::{Axis, arr2, arr3};
24+
///
25+
/// let mut a = arr2(&[[1, 2, 3], [4, 5, 6]]).into_dyn();
26+
/// assert_eq!(a.shape(), &[2, 3]);
27+
///
28+
/// a.insert_axis_inplace(Axis(1));
29+
/// assert_eq!(a, arr3(&[[[1, 2, 3]], [[4, 5, 6]]]).into_dyn());
30+
/// assert_eq!(a.shape(), &[2, 1, 3]);
31+
/// ```
32+
pub fn insert_axis_inplace(&mut self, axis: Axis) {
33+
assert!(axis.index() <= self.ndim());
34+
self.dim = self.dim.insert_axis(axis);
35+
self.strides = self.strides.insert_axis(axis);
36+
}
37+
38+
/// Remove array axis `axis`, modifying the shape and strides in-place.
39+
///
40+
/// **Panics** if the axis is out of bounds or its length is zero.
41+
///
42+
/// ```
43+
/// use ndarray::{Axis, arr1, arr2};
44+
///
45+
/// let mut a = arr2(&[[1, 2, 3], [4, 5, 6]]).into_dyn();
46+
/// assert_eq!(a.shape(), &[2, 3]);
47+
///
48+
/// a.remove_axis_inplace(Axis(1));
49+
/// assert_eq!(a, arr1(&[1, 4]).into_dyn());
50+
/// assert_eq!(a.shape(), &[2]);
51+
/// ```
52+
pub fn remove_axis_inplace(&mut self, axis: Axis) {
53+
let len = self.len_of(axis);
54+
assert_ne!(len, 0, "Length of removed axis must be nonzero.");
55+
self.dim = self.dim.remove_axis(axis);
56+
self.strides = self.strides.remove_axis(axis);
57+
}
58+
}

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ pub type Ixs = isize;
240240
/// + [Conversions Between Array Types](#conversions-between-array-types)
241241
/// + [Constructor Methods for Owned Arrays](#constructor-methods-for-owned-arrays)
242242
/// + [Methods For All Array Types](#methods-for-all-array-types)
243-
///
243+
/// + [Methods for Dynamic-Dimensional Arrays](#methods-for-dynamic-dimensional-arrays)
244244
///
245245
/// ## `Array`
246246
///
@@ -1108,6 +1108,7 @@ impl<A, S, D> ArrayBase<S, D>
11081108

11091109
mod impl_1d;
11101110
mod impl_2d;
1111+
mod impl_dyn;
11111112

11121113
mod numeric;
11131114

0 commit comments

Comments
 (0)