Skip to content

Commit b308eb8

Browse files
committed
Rename subview to index_axis
1 parent 3641ce5 commit b308eb8

File tree

9 files changed

+70
-58
lines changed

9 files changed

+70
-58
lines changed

examples/sort-axis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<A, D> PermuteArray for Array<A, D>
110110
for i in 0..axis_len {
111111
let perm_i = perm.indices[i];
112112
Zip::from(result.index_axis_mut(axis, perm_i))
113-
.and(self.subview(axis, i))
113+
.and(self.index_axis(axis, i))
114114
.apply(|to, from| {
115115
copy_nonoverlapping(from, to, 1)
116116
});

src/doc/ndarray_for_numpy_users/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@
238238
//! ------|-----------|------
239239
//! `a[-1]` | [`a[a.len() - 1]`][.index()] | access the last element in 1-D array `a`
240240
//! `a[1, 4]` | [`a[[1, 4]]`][.index()] | access the element in row 1, column 4
241-
//! `a[1]` or `a[1, :, :]` | [`a.slice(s![1, .., ..])`][.slice()] or [`a.subview(Axis(0), 1)`][.subview()] | get a 2-D subview of a 3-D array at index 1 of axis 0
241+
//! `a[1]` or `a[1, :, :]` | [`a.slice(s![1, .., ..])`][.slice()] or [`a.index_axis(Axis(0), 1)`][.index_axis()] | get a 2-D subview of a 3-D array at index 1 of axis 0
242242
//! `a[0:5]` or `a[:5]` or `a[0:5, :]` | [`a.slice(s![0..5, ..])`][.slice()] or [`a.slice(s![..5, ..])`][.slice()] or [`a.slice_axis(Axis(0), Slice::from(0..5))`][.slice_axis()] | get the first 5 rows of a 2-D array
243243
//! `a[-5:]` or `a[-5:, :]` | [`a.slice(s![-5.., ..])`][.slice()] or [`a.slice_axis(Axis(0), Slice::from(-5..))`][.slice_axis()] | get the last 5 rows of a 2-D array
244244
//! `a[:3, 4:9]` | [`a.slice(s![..3, 4..9])`][.slice()] | columns 4, 5, 6, 7, and 8 of the first 3 rows
@@ -625,7 +625,7 @@
625625
//! [stack!]: ../../macro.stack.html
626626
//! [stack()]: ../../fn.stack.html
627627
//! [.strides()]: ../../struct.ArrayBase.html#method.strides
628-
//! [.subview()]: ../../struct.ArrayBase.html#method.subview
628+
//! [.index_axis()]: ../../struct.ArrayBase.html#method.index_axis
629629
//! [.sum_axis()]: ../../struct.ArrayBase.html#method.sum_axis
630630
//! [.t()]: ../../struct.ArrayBase.html#method.t
631631
//! [::uninitialized()]: ../../struct.ArrayBase.html#method.uninitialized

src/impl_2d.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl<A, S> ArrayBase<S, Ix2>
1919
/// **Panics** if `index` is out of bounds.
2020
pub fn row(&self, index: Ix) -> ArrayView1<A>
2121
{
22-
self.subview(Axis(0), index)
22+
self.index_axis(Axis(0), index)
2323
}
2424

2525
/// Return a mutable array view of row `index`.
@@ -41,7 +41,7 @@ impl<A, S> ArrayBase<S, Ix2>
4141
/// **Panics** if `index` is out of bounds.
4242
pub fn column(&self, index: Ix) -> ArrayView1<A>
4343
{
44-
self.subview(Axis(1), index)
44+
self.index_axis(Axis(1), index)
4545
}
4646

4747
/// Return a mutable array view of column `index`.

src/impl_methods.rs

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,34 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
542542
}
543543
}
544544

545+
/// Along `axis`, select the subview `index` and return a
546+
/// view with that axis removed.
547+
///
548+
/// See [*Subviews*](#subviews) for full documentation.
549+
///
550+
/// **Panics** if `axis` or `index` is out of bounds.
551+
///
552+
/// ```
553+
/// use ndarray::{arr2, ArrayView, Axis};
554+
///
555+
/// let a = arr2(&[[1., 2. ], // ... axis 0, row 0
556+
/// [3., 4. ], // --- axis 0, row 1
557+
/// [5., 6. ]]); // ... axis 0, row 2
558+
/// // . \
559+
/// // . axis 1, column 1
560+
/// // axis 1, column 0
561+
/// assert!(
562+
/// a.index_axis(Axis(0), 1) == ArrayView::from(&[3., 4.]) &&
563+
/// a.index_axis(Axis(1), 1) == ArrayView::from(&[2., 4., 6.])
564+
/// );
565+
/// ```
566+
pub fn index_axis(&self, axis: Axis, index: usize) -> ArrayView<A, D::Smaller>
567+
where
568+
D: RemoveAxis,
569+
{
570+
self.view().index_axis_move(axis, index)
571+
}
572+
545573
/// Along `axis`, select the subview `index` and return a read-write view
546574
/// with the axis removed.
547575
///
@@ -577,7 +605,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
577605
/// Along `axis`, select the subview `index` and return `self`
578606
/// with that axis removed.
579607
///
580-
/// See [`.subview()`](#method.subview) and [*Subviews*](#subviews) for full documentation.
608+
/// See [`.index_axis()`](#method.index_axis) and [*Subviews*](#subviews) for full documentation.
581609
pub fn index_axis_move(mut self, axis: Axis, index: usize) -> ArrayBase<S, D::Smaller>
582610
where
583611
D: RemoveAxis,
@@ -603,28 +631,12 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
603631
/// Along `axis`, select the subview `index` and return a
604632
/// view with that axis removed.
605633
///
606-
/// See [*Subviews*](#subviews) for full documentation.
607-
///
608634
/// **Panics** if `axis` or `index` is out of bounds.
609-
///
610-
/// ```
611-
/// use ndarray::{arr2, ArrayView, Axis};
612-
///
613-
/// let a = arr2(&[[1., 2. ], // ... axis 0, row 0
614-
/// [3., 4. ], // --- axis 0, row 1
615-
/// [5., 6. ]]); // ... axis 0, row 2
616-
/// // . \
617-
/// // . axis 1, column 1
618-
/// // axis 1, column 0
619-
/// assert!(
620-
/// a.subview(Axis(0), 1) == ArrayView::from(&[3., 4.]) &&
621-
/// a.subview(Axis(1), 1) == ArrayView::from(&[2., 4., 6.])
622-
/// );
623-
/// ```
635+
#[deprecated(note="renamed to `index_axis`", since="0.12.1")]
624636
pub fn subview(&self, axis: Axis, index: Ix) -> ArrayView<A, D::Smaller>
625637
where D: RemoveAxis,
626638
{
627-
self.view().index_axis_move(axis, index)
639+
self.index_axis(axis, index)
628640
}
629641

630642
/// Along `axis`, select the subview `index` and return a read-write view
@@ -1894,7 +1906,7 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
18941906
let view_stride = self.strides.axis(axis);
18951907
// use the 0th subview as a map to each 1d array view extended from
18961908
// the 0th element.
1897-
self.subview(axis, 0).map(|first_elt| {
1909+
self.index_axis(axis, 0).map(|first_elt| {
18981910
unsafe {
18991911
mapping(ArrayView::new_(first_elt, Ix1(view_len), Ix1(view_stride)))
19001912
}

src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -520,14 +520,14 @@ pub type Ixs = isize;
520520
/// ## Subviews
521521
///
522522
/// Subview methods allow you to restrict the array view while removing one
523-
/// axis from the array. Subview methods include [`.subview()`],
523+
/// axis from the array. Subview methods include [`.index_axis()`],
524524
/// [`.index_axis_mut()`], [`.index_axis_move()`], and [`.collapse_axis()`]. You
525525
/// can also take a subview by using a single index instead of a range when
526526
/// slicing.
527527
///
528528
/// Subview takes two arguments: `axis` and `index`.
529529
///
530-
/// [`.subview()`]: #method.subview
530+
/// [`.index_axis()`]: #method.index_axis
531531
/// [`.index_axis_mut()`]: #method.index_axis_mut
532532
/// [`.index_axis_move()`]: #method.index_axis_move
533533
/// [`.collapse_axis()`]: #method.collapse_axis
@@ -553,8 +553,8 @@ pub type Ixs = isize;
553553
/// // Let’s take a subview along the greatest dimension (axis 0),
554554
/// // taking submatrix 0, then submatrix 1
555555
///
556-
/// let sub_0 = a.subview(Axis(0), 0);
557-
/// let sub_1 = a.subview(Axis(0), 1);
556+
/// let sub_0 = a.index_axis(Axis(0), 0);
557+
/// let sub_1 = a.index_axis(Axis(0), 1);
558558
///
559559
/// assert_eq!(sub_0, aview2(&[[ 1, 2, 3],
560560
/// [ 4, 5, 6]]));
@@ -563,7 +563,7 @@ pub type Ixs = isize;
563563
/// assert_eq!(sub_0.shape(), &[2, 3]);
564564
///
565565
/// // This is the subview picking only axis 2, column 0
566-
/// let sub_col = a.subview(Axis(2), 0);
566+
/// let sub_col = a.index_axis(Axis(2), 0);
567567
///
568568
/// assert_eq!(sub_col, aview2(&[[ 1, 4],
569569
/// [ 7, 10]]));
@@ -574,7 +574,7 @@ pub type Ixs = isize;
574574
/// # }
575575
/// ```
576576
///
577-
/// [`.collapse_axis()`] modifies the view in the same way as [`.subview()`],
577+
/// [`.collapse_axis()`] modifies the view in the same way as [`.index_axis()`],
578578
/// but since it is *in place*, it cannot remove the collapsed axis. It becomes
579579
/// an axis of length 1.
580580
///

src/numeric/impl_numeric.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ impl<A, S, D> ArrayBase<S, D>
107107
// contiguous along the axis we are summing
108108
let ax = axis.index();
109109
for (i, elt) in enumerate(&mut res) {
110-
*elt = self.subview(Axis(1 - ax), i).sum();
110+
*elt = self.index_axis(Axis(1 - ax), i).sum();
111111
}
112112
} else {
113113
for i in 0..n {
114-
let view = self.subview(axis, i);
114+
let view = self.index_axis(axis, i);
115115
res = res + &view;
116116
}
117117
}

tests/array.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ fn test_slice_with_subview() {
253253
assert_eq!(vi.shape(), &[2, 2]);
254254
assert!(
255255
vi.iter()
256-
.zip(arr.subview(Axis(1), 2).slice(s![1.., ..;2]).iter())
256+
.zip(arr.index_axis(Axis(1), 2).slice(s![1.., ..;2]).iter())
257257
.all(|(a, b)| a == b)
258258
);
259259

@@ -262,8 +262,8 @@ fn test_slice_with_subview() {
262262
assert!(
263263
vi.iter()
264264
.zip(
265-
arr.subview(Axis(0), 1)
266-
.subview(Axis(0), 2)
265+
arr.index_axis(Axis(0), 1)
266+
.index_axis(Axis(0), 2)
267267
.slice(s![..;2])
268268
.iter()
269269
)
@@ -515,21 +515,21 @@ fn test_cow_shrink()
515515
fn test_sub()
516516
{
517517
let mat = RcArray::linspace(0., 15., 16).reshape((2, 4, 2));
518-
let s1 = mat.subview(Axis(0), 0);
519-
let s2 = mat.subview(Axis(0), 1);
518+
let s1 = mat.index_axis(Axis(0), 0);
519+
let s2 = mat.index_axis(Axis(0), 1);
520520
assert_eq!(s1.shape(), &[4, 2]);
521521
assert_eq!(s2.shape(), &[4, 2]);
522522
let n = RcArray::linspace(8., 15., 8).reshape((4,2));
523523
assert_eq!(n, s2);
524524
let m = RcArray::from_vec(vec![2., 3., 10., 11.]).reshape((2, 2));
525-
assert_eq!(m, mat.subview(Axis(1), 1));
525+
assert_eq!(m, mat.index_axis(Axis(1), 1));
526526
}
527527

528528
#[should_panic]
529529
#[test]
530530
fn test_sub_oob_1() {
531531
let mat = RcArray::linspace(0., 15., 16).reshape((2, 4, 2));
532-
mat.subview(Axis(0), 2);
532+
mat.index_axis(Axis(0), 2);
533533
}
534534

535535

@@ -652,9 +652,9 @@ fn standard_layout()
652652
assert!(!a.is_standard_layout());
653653
a.swap_axes(0, 1);
654654
assert!(a.is_standard_layout());
655-
let x1 = a.subview(Axis(0), 0);
655+
let x1 = a.index_axis(Axis(0), 0);
656656
assert!(x1.is_standard_layout());
657-
let x2 = a.subview(Axis(1), 0);
657+
let x2 = a.index_axis(Axis(1), 0);
658658
assert!(!x2.is_standard_layout());
659659
}
660660

@@ -887,7 +887,7 @@ fn zero_axes()
887887
println!("{:?}\n{:?}", b.shape(), b);
888888

889889
// we can even get a subarray of b
890-
let bsub = b.subview(Axis(0), 2);
890+
let bsub = b.index_axis(Axis(0), 2);
891891
assert_eq!(bsub.dim(), 0);
892892
}
893893

@@ -1400,9 +1400,9 @@ fn insert_axis_f() {
14001400
fn insert_axis_view() {
14011401
let a = array![[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]];
14021402

1403-
assert_eq!(a.subview(Axis(1), 0).insert_axis(Axis(0)), array![[[1, 2], [5, 6], [9, 10]]]);
1404-
assert_eq!(a.subview(Axis(1), 0).insert_axis(Axis(1)), array![[[1, 2]], [[5, 6]], [[9, 10]]]);
1405-
assert_eq!(a.subview(Axis(1), 0).insert_axis(Axis(2)), array![[[1], [2]], [[5], [6]], [[9], [10]]]);
1403+
assert_eq!(a.index_axis(Axis(1), 0).insert_axis(Axis(0)), array![[[1, 2], [5, 6], [9, 10]]]);
1404+
assert_eq!(a.index_axis(Axis(1), 0).insert_axis(Axis(1)), array![[[1, 2]], [[5, 6]], [[9, 10]]]);
1405+
assert_eq!(a.index_axis(Axis(1), 0).insert_axis(Axis(2)), array![[[1], [2]], [[5], [6]], [[9], [10]]]);
14061406
}
14071407

14081408
#[test]
@@ -1420,7 +1420,7 @@ fn char_array()
14201420
{
14211421
// test compilation & basics of non-numerical array
14221422
let cc = RcArray::from_iter("alphabet".chars()).reshape((4, 2));
1423-
assert!(cc.subview(Axis(1), 0) == RcArray::from_iter("apae".chars()));
1423+
assert!(cc.index_axis(Axis(1), 0) == RcArray::from_iter("apae".chars()));
14241424
}
14251425

14261426
#[test]

tests/dimension.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn remove_axis()
5353
assert_eq!(Dim(vec![4, 5, 6]).remove_axis(Axis(1)), Dim(vec![4, 6]));
5454

5555
let a = RcArray::<f32, _>::zeros((4,5));
56-
a.subview(Axis(1), 0);
56+
a.index_axis(Axis(1), 0);
5757

5858
let a = RcArray::<f32, _>::zeros(vec![4,5,6]);
5959
let _b = a.index_axis_move(Axis(1), 0).reshape((4, 6)).reshape(vec![2, 3, 4]);

tests/iterators.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ fn as_slice() {
9292
let a = a.reshape((2, 4));
9393
assert_slice_correct(&a);
9494

95-
assert!(a.view().subview(Axis(1), 0).as_slice().is_none());
95+
assert!(a.view().index_axis(Axis(1), 0).as_slice().is_none());
9696

9797
let v = a.view();
9898
assert_slice_correct(&v);
99-
assert_slice_correct(&v.subview(Axis(0), 0));
100-
assert_slice_correct(&v.subview(Axis(0), 1));
99+
assert_slice_correct(&v.index_axis(Axis(0), 0));
100+
assert_slice_correct(&v.index_axis(Axis(0), 1));
101101

102102
assert!(v.slice(s![.., ..1]).as_slice().is_none());
103103
println!("{:?}", v.slice(s![..1;2, ..]));
@@ -180,12 +180,12 @@ fn outer_iter() {
180180
// [8, 9],
181181
// ...
182182
assert_equal(a.outer_iter(),
183-
vec![a.subview(Axis(0), 0), a.subview(Axis(0), 1)]);
183+
vec![a.index_axis(Axis(0), 0), a.index_axis(Axis(0), 1)]);
184184
let mut b = RcArray::zeros((2, 3, 2));
185185
b.swap_axes(0, 2);
186186
b.assign(&a);
187187
assert_equal(b.outer_iter(),
188-
vec![a.subview(Axis(0), 0), a.subview(Axis(0), 1)]);
188+
vec![a.index_axis(Axis(0), 0), a.index_axis(Axis(0), 1)]);
189189

190190
let mut found_rows = Vec::new();
191191
for sub in b.outer_iter() {
@@ -210,7 +210,7 @@ fn outer_iter() {
210210
cv.assign(&a);
211211
assert_eq!(&a, &cv);
212212
assert_equal(cv.outer_iter(),
213-
vec![a.subview(Axis(0), 0), a.subview(Axis(0), 1)]);
213+
vec![a.index_axis(Axis(0), 0), a.index_axis(Axis(0), 1)]);
214214

215215
let mut found_rows = Vec::new();
216216
for sub in cv.outer_iter() {
@@ -233,9 +233,9 @@ fn axis_iter() {
233233
// [8, 9],
234234
// ...
235235
assert_equal(a.axis_iter(Axis(1)),
236-
vec![a.subview(Axis(1), 0),
237-
a.subview(Axis(1), 1),
238-
a.subview(Axis(1), 2)]);
236+
vec![a.index_axis(Axis(1), 0),
237+
a.index_axis(Axis(1), 1),
238+
a.index_axis(Axis(1), 2)]);
239239
}
240240

241241
#[test]
@@ -264,7 +264,7 @@ fn outer_iter_mut() {
264264
b.swap_axes(0, 2);
265265
b.assign(&a);
266266
assert_equal(b.outer_iter_mut(),
267-
vec![a.subview(Axis(0), 0), a.subview(Axis(0), 1)]);
267+
vec![a.index_axis(Axis(0), 0), a.index_axis(Axis(0), 1)]);
268268

269269
let mut found_rows = Vec::new();
270270
for sub in b.outer_iter_mut() {

0 commit comments

Comments
 (0)