Skip to content

Commit 755c02f

Browse files
committed
Add insert/index_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 1347d05 commit 755c02f

File tree

2 files changed

+67
-5
lines changed

2 files changed

+67
-5
lines changed

src/impl_dyn.rs

+58
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+
/// Collapses the array to `index` along the axis and removes the axis,
39+
/// modifying the shape and strides in-place.
40+
///
41+
/// **Panics** if `axis` or `index` is out of bounds.
42+
///
43+
/// ```
44+
/// use ndarray::{Axis, arr1, arr2};
45+
///
46+
/// let mut a = arr2(&[[1, 2, 3], [4, 5, 6]]).into_dyn();
47+
/// assert_eq!(a.shape(), &[2, 3]);
48+
///
49+
/// a.index_axis_inplace(Axis(1), 1);
50+
/// assert_eq!(a, arr1(&[2, 5]).into_dyn());
51+
/// assert_eq!(a.shape(), &[2]);
52+
/// ```
53+
pub fn index_axis_inplace(&mut self, axis: Axis, index: usize) {
54+
self.collapse_axis(axis, index);
55+
self.dim = self.dim.remove_axis(axis);
56+
self.strides = self.strides.remove_axis(axis);
57+
}
58+
}

src/lib.rs

+9-5
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`
@@ -522,11 +523,12 @@ pub type Ixs = isize;
522523
///
523524
/// Subview methods allow you to restrict the array view while removing one
524525
/// axis from the array. Methods for selecting individual subviews include
525-
/// [`.index_axis()`], [`.index_axis_mut()`], and [`.index_axis_move()`]. You
526-
/// can also select a subview by using a single index instead of a range when
527-
/// slicing. Some other methods, such as [`.fold_axis()`], [`.axis_iter()`],
528-
/// [`.axis_iter_mut()`], [`.outer_iter()`], and [`.outer_iter_mut()`] operate
529-
/// on all the subviews along an axis.
526+
/// [`.index_axis()`], [`.index_axis_mut()`], [`.index_axis_move()`], and
527+
/// [`.index_axis_inplace()`]. You can also select a subview by using a single
528+
/// index instead of a range when slicing. Some other methods, such as
529+
/// [`.fold_axis()`], [`.axis_iter()`], [`.axis_iter_mut()`],
530+
/// [`.outer_iter()`], and [`.outer_iter_mut()`] operate on all the subviews
531+
/// along an axis.
530532
///
531533
/// A related method is [`.collapse_axis()`], which modifies the view in the
532534
/// same way as [`.index_axis()`] except for removing the collapsed axis, since
@@ -539,6 +541,7 @@ pub type Ixs = isize;
539541
/// [`.axis_iter_mut()`]: #method.axis_iter_mut
540542
/// [`.fold_axis()`]: #method.fold_axis
541543
/// [`.index_axis()`]: #method.index_axis
544+
/// [`.index_axis_inplace()`]: #method.index_axis_inplace
542545
/// [`.index_axis_mut()`]: #method.index_axis_mut
543546
/// [`.index_axis_move()`]: #method.index_axis_move
544547
/// [`.collapse_axis()`]: #method.collapse_axis
@@ -1143,6 +1146,7 @@ impl<A, S, D> ArrayBase<S, D>
11431146

11441147
mod impl_1d;
11451148
mod impl_2d;
1149+
mod impl_dyn;
11461150

11471151
mod numeric;
11481152

0 commit comments

Comments
 (0)