Skip to content

Commit 228a15e

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 0a02be3 commit 228a15e

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ pub type Ixs = isize;
242242
/// + [Methods For All Array Types](#methods-for-all-array-types)
243243
/// + [Methods For 1-D Arrays](#methods-for-1-d-arrays)
244244
/// + [Methods For 2-D Arrays](#methods-for-2-d-arrays)
245+
/// + [Methods for Dynamic-Dimensional Arrays](#methods-for-dynamic-dimensional-arrays)
245246
/// + [Numerical Methods for Arrays](#numerical-methods-for-arrays)
246247
///
247248
/// ## `Array`
@@ -1138,6 +1139,7 @@ impl<A, S, D> ArrayBase<S, D>
11381139

11391140
mod impl_1d;
11401141
mod impl_2d;
1142+
mod impl_dyn;
11411143

11421144
mod numeric;
11431145

0 commit comments

Comments
 (0)