From 24aebf0c2ec15f95bbbdf36c7263afa7890a2b65 Mon Sep 17 00:00:00 2001 From: Jim Turner Date: Tue, 16 Mar 2021 00:52:02 -0400 Subject: [PATCH 1/2] Rename AxisSliceInfo to SliceInfoElem MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The name `AxisSliceInfo` isn't a great description for this type because it doesn't correspond to slicing information for a single input axis or a single output axis. This will be especially true if we add another variant for ellipsis-type functionality. The name `SliceInfoElem` more clearly describes the purpose of this type – to be an element in a list describing the slicing information. --- blas-tests/tests/oper.rs | 5 +- src/dimension/mod.rs | 16 ++--- src/impl_methods.rs | 16 ++--- src/lib.rs | 2 +- src/slice.rs | 152 +++++++++++++++++++-------------------- tests/array.rs | 58 +++++++-------- tests/oper.rs | 4 +- 7 files changed, 126 insertions(+), 127 deletions(-) diff --git a/blas-tests/tests/oper.rs b/blas-tests/tests/oper.rs index 27bf94f09..d6230cced 100644 --- a/blas-tests/tests/oper.rs +++ b/blas-tests/tests/oper.rs @@ -6,8 +6,7 @@ extern crate num_traits; use ndarray::linalg::general_mat_mul; use ndarray::linalg::general_mat_vec_mul; use ndarray::prelude::*; -use ndarray::{AxisSliceInfo, Ix, Ixs, Slice}; -use ndarray::{Data, LinalgScalar}; +use ndarray::{Data, Ix, Ixs, LinalgScalar, Slice, SliceInfoElem}; use approx::{assert_abs_diff_eq, assert_relative_eq}; use defmac::defmac; @@ -419,7 +418,7 @@ fn scaled_add_3() { let mut a = range_mat64(m, k); let mut answer = a.clone(); let cdim = if n == 1 { vec![q] } else { vec![n, q] }; - let cslice: Vec = if n == 1 { + let cslice: Vec = if n == 1 { vec![Slice::from(..).step_by(s2).into()] } else { vec![ diff --git a/src/dimension/mod.rs b/src/dimension/mod.rs index f94a7135c..fb062513e 100644 --- a/src/dimension/mod.rs +++ b/src/dimension/mod.rs @@ -8,7 +8,7 @@ use crate::error::{from_kind, ErrorKind, ShapeError}; use crate::slice::SliceArg; -use crate::{AxisSliceInfo, Ix, Ixs, Slice}; +use crate::{Ix, Ixs, Slice, SliceInfoElem}; use num_integer::div_floor; pub use self::axes::{axes_of, Axes, AxisDescription}; @@ -608,15 +608,15 @@ pub fn slices_intersect( indices1.as_ref().iter().filter(|si| !si.is_new_axis()), indices2.as_ref().iter().filter(|si| !si.is_new_axis()), ) { - // The slices do not intersect iff any pair of `AxisSliceInfo` does not intersect. + // The slices do not intersect iff any pair of `SliceInfoElem` does not intersect. match (si1, si2) { ( - AxisSliceInfo::Slice { + SliceInfoElem::Slice { start: start1, end: end1, step: step1, }, - AxisSliceInfo::Slice { + SliceInfoElem::Slice { start: start2, end: end2, step: step2, @@ -637,8 +637,8 @@ pub fn slices_intersect( return false; } } - (AxisSliceInfo::Slice { start, end, step }, AxisSliceInfo::Index(ind)) - | (AxisSliceInfo::Index(ind), AxisSliceInfo::Slice { start, end, step }) => { + (SliceInfoElem::Slice { start, end, step }, SliceInfoElem::Index(ind)) + | (SliceInfoElem::Index(ind), SliceInfoElem::Slice { start, end, step }) => { let ind = abs_index(axis_len, ind); let (min, max) = match slice_min_max(axis_len, Slice::new(start, end, step)) { Some(m) => m, @@ -648,14 +648,14 @@ pub fn slices_intersect( return false; } } - (AxisSliceInfo::Index(ind1), AxisSliceInfo::Index(ind2)) => { + (SliceInfoElem::Index(ind1), SliceInfoElem::Index(ind2)) => { let ind1 = abs_index(axis_len, ind1); let ind2 = abs_index(axis_len, ind2); if ind1 != ind2 { return false; } } - (AxisSliceInfo::NewAxis, _) | (_, AxisSliceInfo::NewAxis) => unreachable!(), + (SliceInfoElem::NewAxis, _) | (_, SliceInfoElem::NewAxis) => unreachable!(), } } true diff --git a/src/impl_methods.rs b/src/impl_methods.rs index a0833048e..4696fccbd 100644 --- a/src/impl_methods.rs +++ b/src/impl_methods.rs @@ -34,7 +34,7 @@ use crate::iter::{ }; use crate::slice::{MultiSliceArg, SliceArg}; use crate::stacking::concatenate; -use crate::{AxisSliceInfo, NdIndex, Slice}; +use crate::{NdIndex, Slice, SliceInfoElem}; /// # Methods For All Array Types impl ArrayBase @@ -415,7 +415,7 @@ where let mut old_axis = 0; let mut new_axis = 0; info.as_ref().iter().for_each(|&ax_info| match ax_info { - AxisSliceInfo::Slice { start, end, step } => { + SliceInfoElem::Slice { start, end, step } => { // Slice the axis in-place to update the `dim`, `strides`, and `ptr`. self.slice_axis_inplace(Axis(old_axis), Slice { start, end, step }); // Copy the sliced dim and stride to corresponding axis. @@ -424,7 +424,7 @@ where old_axis += 1; new_axis += 1; } - AxisSliceInfo::Index(index) => { + SliceInfoElem::Index(index) => { // Collapse the axis in-place to update the `ptr`. let i_usize = abs_index(self.len_of(Axis(old_axis)), index); self.collapse_axis(Axis(old_axis), i_usize); @@ -434,7 +434,7 @@ where // is zero length. old_axis += 1; } - AxisSliceInfo::NewAxis => { + SliceInfoElem::NewAxis => { // Set the dim and stride of the new axis. new_dim[new_axis] = 1; new_strides[new_axis] = 0; @@ -465,7 +465,7 @@ where /// /// - if an index is out of bounds /// - if a step size is zero - /// - if [`AxisSliceInfo::NewAxis`] is in `info`, e.g. if [`NewAxis`] was + /// - if [`SliceInfoElem::NewAxis`] is in `info`, e.g. if [`NewAxis`] was /// used in the [`s!`] macro /// - if `D` is `IxDyn` and `info` does not match the number of array axes pub fn slice_collapse(&mut self, info: I) @@ -479,16 +479,16 @@ where ); let mut axis = 0; info.as_ref().iter().for_each(|&ax_info| match ax_info { - AxisSliceInfo::Slice { start, end, step } => { + SliceInfoElem::Slice { start, end, step } => { self.slice_axis_inplace(Axis(axis), Slice { start, end, step }); axis += 1; } - AxisSliceInfo::Index(index) => { + SliceInfoElem::Index(index) => { let i_usize = abs_index(self.len_of(Axis(axis)), index); self.collapse_axis(Axis(axis), i_usize); axis += 1; } - AxisSliceInfo::NewAxis => panic!("`slice_collapse` does not support `NewAxis`."), + SliceInfoElem::NewAxis => panic!("`slice_collapse` does not support `NewAxis`."), }); debug_assert_eq!(axis, self.ndim()); } diff --git a/src/lib.rs b/src/lib.rs index 10a16ea8b..263afa2d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -144,7 +144,7 @@ pub use crate::dimension::NdIndex; pub use crate::error::{ErrorKind, ShapeError}; pub use crate::indexes::{indices, indices_of}; pub use crate::slice::{ - AxisSliceInfo, MultiSliceArg, NewAxis, Slice, SliceArg, SliceInfo, SliceNextDim, + MultiSliceArg, NewAxis, Slice, SliceArg, SliceInfo, SliceInfoElem, SliceNextDim, }; use crate::iterators::Baseiter; diff --git a/src/slice.rs b/src/slice.rs index a5d65a2e3..1965defd2 100644 --- a/src/slice.rs +++ b/src/slice.rs @@ -78,33 +78,33 @@ pub struct NewAxis; /// A slice (range with step), an index, or a new axis token. /// /// See also the [`s![]`](macro.s!.html) macro for a convenient way to create a -/// `SliceInfo<[AxisSliceInfo; n], Din, Dout>`. +/// `SliceInfo<[SliceInfoElem; n], Din, Dout>`. /// /// ## Examples /// -/// `AxisSliceInfo::Index(a)` is the index `a`. It can also be created with -/// `AxisSliceInfo::from(a)`. The Python equivalent is `[a]`. The macro +/// `SliceInfoElem::Index(a)` is the index `a`. It can also be created with +/// `SliceInfoElem::from(a)`. The Python equivalent is `[a]`. The macro /// equivalent is `s![a]`. /// -/// `AxisSliceInfo::Slice { start: 0, end: None, step: 1 }` is the full range -/// of an axis. It can also be created with `AxisSliceInfo::from(..)`. The +/// `SliceInfoElem::Slice { start: 0, end: None, step: 1 }` is the full range +/// of an axis. It can also be created with `SliceInfoElem::from(..)`. The /// Python equivalent is `[:]`. The macro equivalent is `s![..]`. /// -/// `AxisSliceInfo::Slice { start: a, end: Some(b), step: 2 }` is every second +/// `SliceInfoElem::Slice { start: a, end: Some(b), step: 2 }` is every second /// element from `a` until `b`. It can also be created with -/// `AxisSliceInfo::from(Slice::from(a..b).step_by(2))`. The Python equivalent +/// `SliceInfoElem::from(Slice::from(a..b).step_by(2))`. The Python equivalent /// is `[a:b:2]`. The macro equivalent is `s![a..b;2]`. /// -/// `AxisSliceInfo::Slice { start: a, end: None, step: -1 }` is every element, +/// `SliceInfoElem::Slice { start: a, end: None, step: -1 }` is every element, /// from `a` until the end, in reverse order. It can also be created with -/// `AxisSliceInfo::from(Slice::from(a..).step_by(-1))`. The Python equivalent +/// `SliceInfoElem::from(Slice::from(a..).step_by(-1))`. The Python equivalent /// is `[a::-1]`. The macro equivalent is `s![a..;-1]`. /// -/// `AxisSliceInfo::NewAxis` is a new axis of length 1. It can also be created -/// with `AxisSliceInfo::from(NewAxis)`. The Python equivalent is +/// `SliceInfoElem::NewAxis` is a new axis of length 1. It can also be created +/// with `SliceInfoElem::from(NewAxis)`. The Python equivalent is /// `[np.newaxis]`. The macro equivalent is `s![NewAxis]`. #[derive(Debug, PartialEq, Eq, Hash)] -pub enum AxisSliceInfo { +pub enum SliceInfoElem { /// A range with step size. `end` is an exclusive index. Negative `begin` /// or `end` indexes are counted from the back of the axis. If `end` is /// `None`, the slice extends to the end of the axis. @@ -119,30 +119,30 @@ pub enum AxisSliceInfo { NewAxis, } -copy_and_clone! {AxisSliceInfo} +copy_and_clone! {SliceInfoElem} -impl AxisSliceInfo { +impl SliceInfoElem { /// Returns `true` if `self` is a `Slice` value. pub fn is_slice(&self) -> bool { - matches!(self, AxisSliceInfo::Slice { .. }) + matches!(self, SliceInfoElem::Slice { .. }) } /// Returns `true` if `self` is an `Index` value. pub fn is_index(&self) -> bool { - matches!(self, AxisSliceInfo::Index(_)) + matches!(self, SliceInfoElem::Index(_)) } /// Returns `true` if `self` is a `NewAxis` value. pub fn is_new_axis(&self) -> bool { - matches!(self, AxisSliceInfo::NewAxis) + matches!(self, SliceInfoElem::NewAxis) } } -impl fmt::Display for AxisSliceInfo { +impl fmt::Display for SliceInfoElem { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { - AxisSliceInfo::Index(index) => write!(f, "{}", index)?, - AxisSliceInfo::Slice { start, end, step } => { + SliceInfoElem::Index(index) => write!(f, "{}", index)?, + SliceInfoElem::Slice { start, end, step } => { if start != 0 { write!(f, "{}", start)?; } @@ -154,7 +154,7 @@ impl fmt::Display for AxisSliceInfo { write!(f, ";{}", step)?; } } - AxisSliceInfo::NewAxis => write!(f, stringify!(NewAxis))?, + SliceInfoElem::NewAxis => write!(f, stringify!(NewAxis))?, } Ok(()) } @@ -223,9 +223,9 @@ macro_rules! impl_slice_variant_from_range { impl_slice_variant_from_range!(Slice, Slice, isize); impl_slice_variant_from_range!(Slice, Slice, usize); impl_slice_variant_from_range!(Slice, Slice, i32); -impl_slice_variant_from_range!(AxisSliceInfo, AxisSliceInfo::Slice, isize); -impl_slice_variant_from_range!(AxisSliceInfo, AxisSliceInfo::Slice, usize); -impl_slice_variant_from_range!(AxisSliceInfo, AxisSliceInfo::Slice, i32); +impl_slice_variant_from_range!(SliceInfoElem, SliceInfoElem::Slice, isize); +impl_slice_variant_from_range!(SliceInfoElem, SliceInfoElem::Slice, usize); +impl_slice_variant_from_range!(SliceInfoElem, SliceInfoElem::Slice, i32); impl From for Slice { #[inline] @@ -238,10 +238,10 @@ impl From for Slice { } } -impl From for AxisSliceInfo { +impl From for SliceInfoElem { #[inline] - fn from(_: RangeFull) -> AxisSliceInfo { - AxisSliceInfo::Slice { + fn from(_: RangeFull) -> SliceInfoElem { + SliceInfoElem::Slice { start: 0, end: None, step: 1, @@ -249,10 +249,10 @@ impl From for AxisSliceInfo { } } -impl From for AxisSliceInfo { +impl From for SliceInfoElem { #[inline] - fn from(s: Slice) -> AxisSliceInfo { - AxisSliceInfo::Slice { + fn from(s: Slice) -> SliceInfoElem { + SliceInfoElem::Slice { start: s.start, end: s.end, step: s.step, @@ -260,24 +260,24 @@ impl From for AxisSliceInfo { } } -macro_rules! impl_axissliceinfo_from_index { +macro_rules! impl_sliceinfoelem_from_index { ($index:ty) => { - impl From<$index> for AxisSliceInfo { + impl From<$index> for SliceInfoElem { #[inline] - fn from(r: $index) -> AxisSliceInfo { - AxisSliceInfo::Index(r as isize) + fn from(r: $index) -> SliceInfoElem { + SliceInfoElem::Index(r as isize) } } }; } -impl_axissliceinfo_from_index!(isize); -impl_axissliceinfo_from_index!(usize); -impl_axissliceinfo_from_index!(i32); +impl_sliceinfoelem_from_index!(isize); +impl_sliceinfoelem_from_index!(usize); +impl_sliceinfoelem_from_index!(i32); -impl From for AxisSliceInfo { +impl From for SliceInfoElem { #[inline] - fn from(_: NewAxis) -> AxisSliceInfo { - AxisSliceInfo::NewAxis + fn from(_: NewAxis) -> SliceInfoElem { + SliceInfoElem::NewAxis } } @@ -285,9 +285,9 @@ impl From for AxisSliceInfo { /// /// This trait is unsafe to implement because the implementation must ensure /// that `D`, `Self::OutDim`, `self.in_dim()`, and `self.out_ndim()` are -/// consistent with the `&[AxisSliceInfo]` returned by `self.as_ref()` and that +/// consistent with the `&[SliceInfoElem]` returned by `self.as_ref()` and that /// `self.as_ref()` always returns the same value when called multiple times. -pub unsafe trait SliceArg: AsRef<[AxisSliceInfo]> { +pub unsafe trait SliceArg: AsRef<[SliceInfoElem]> { /// Dimensionality of the output array. type OutDim: Dimension; @@ -322,7 +322,7 @@ macro_rules! impl_slicearg_samedim { ($in_dim:ty) => { unsafe impl SliceArg<$in_dim> for SliceInfo where - T: AsRef<[AxisSliceInfo]>, + T: AsRef<[SliceInfoElem]>, Dout: Dimension, { type OutDim = Dout; @@ -349,7 +349,7 @@ impl_slicearg_samedim!(Ix6); unsafe impl SliceArg for SliceInfo where - T: AsRef<[AxisSliceInfo]>, + T: AsRef<[SliceInfoElem]>, Din: Dimension, Dout: Dimension, { @@ -366,7 +366,7 @@ where private_impl! {} } -unsafe impl SliceArg for [AxisSliceInfo] { +unsafe impl SliceArg for [SliceInfoElem] { type OutDim = IxDyn; fn in_ndim(&self) -> usize { @@ -382,8 +382,8 @@ unsafe impl SliceArg for [AxisSliceInfo] { /// Represents all of the necessary information to perform a slice. /// -/// The type `T` is typically `[AxisSliceInfo; n]`, `&[AxisSliceInfo]`, or -/// `Vec`. The type `Din` is the dimension of the array to be +/// The type `T` is typically `[SliceInfoElem; n]`, `&[SliceInfoElem]`, or +/// `Vec`. The type `Din` is the dimension of the array to be /// sliced, and `Dout` is the output dimension after calling [`.slice()`]. Note /// that if `Din` is a fixed dimension type (`Ix0`, `Ix1`, `Ix2`, etc.), the /// `SliceInfo` instance can still be used to slice an array with dimension @@ -408,7 +408,7 @@ where } } -fn check_dims_for_sliceinfo(indices: &[AxisSliceInfo]) -> Result<(), ShapeError> +fn check_dims_for_sliceinfo(indices: &[SliceInfoElem]) -> Result<(), ShapeError> where Din: Dimension, Dout: Dimension, @@ -428,7 +428,7 @@ where impl SliceInfo where - T: AsRef<[AxisSliceInfo]>, + T: AsRef<[SliceInfoElem]>, Din: Dimension, Dout: Dimension, { @@ -482,7 +482,7 @@ where /// /// If `Din` is a fixed-size dimension type, then this is equivalent to /// `Din::NDIM.unwrap()`. Otherwise, the value is calculated by iterating - /// over the `AxisSliceInfo` elements. + /// over the `SliceInfoElem` elements. pub fn in_ndim(&self) -> usize { if let Some(ndim) = Din::NDIM { ndim @@ -497,7 +497,7 @@ where /// /// If `Dout` is a fixed-size dimension type, then this is equivalent to /// `Dout::NDIM.unwrap()`. Otherwise, the value is calculated by iterating - /// over the `AxisSliceInfo` elements. + /// over the `SliceInfoElem` elements. pub fn out_ndim(&self) -> usize { if let Some(ndim) = Dout::NDIM { ndim @@ -507,7 +507,7 @@ where } } -impl<'a, Din, Dout> TryFrom<&'a [AxisSliceInfo]> for SliceInfo<&'a [AxisSliceInfo], Din, Dout> +impl<'a, Din, Dout> TryFrom<&'a [SliceInfoElem]> for SliceInfo<&'a [SliceInfoElem], Din, Dout> where Din: Dimension, Dout: Dimension, @@ -515,17 +515,17 @@ where type Error = ShapeError; fn try_from( - indices: &'a [AxisSliceInfo], - ) -> Result, ShapeError> { + indices: &'a [SliceInfoElem], + ) -> Result, ShapeError> { unsafe { - // This is okay because `&[AxisSliceInfo]` always returns the same + // This is okay because `&[SliceInfoElem]` always returns the same // value for `.as_ref()`. Self::new(indices) } } } -impl TryFrom> for SliceInfo, Din, Dout> +impl TryFrom> for SliceInfo, Din, Dout> where Din: Dimension, Dout: Dimension, @@ -533,8 +533,8 @@ where type Error = ShapeError; fn try_from( - indices: Vec, - ) -> Result, Din, Dout>, ShapeError> { + indices: Vec, + ) -> Result, Din, Dout>, ShapeError> { unsafe { // This is okay because `Vec` always returns the same value for // `.as_ref()`. @@ -545,8 +545,8 @@ where macro_rules! impl_tryfrom_array_for_sliceinfo { ($len:expr) => { - impl TryFrom<[AxisSliceInfo; $len]> - for SliceInfo<[AxisSliceInfo; $len], Din, Dout> + impl TryFrom<[SliceInfoElem; $len]> + for SliceInfo<[SliceInfoElem; $len], Din, Dout> where Din: Dimension, Dout: Dimension, @@ -554,10 +554,10 @@ macro_rules! impl_tryfrom_array_for_sliceinfo { type Error = ShapeError; fn try_from( - indices: [AxisSliceInfo; $len], - ) -> Result, ShapeError> { + indices: [SliceInfoElem; $len], + ) -> Result, ShapeError> { unsafe { - // This is okay because `[AxisSliceInfo; N]` always returns + // This is okay because `[SliceInfoElem; N]` always returns // the same value for `.as_ref()`. Self::new(indices) } @@ -575,25 +575,25 @@ impl_tryfrom_array_for_sliceinfo!(6); impl_tryfrom_array_for_sliceinfo!(7); impl_tryfrom_array_for_sliceinfo!(8); -impl AsRef<[AxisSliceInfo]> for SliceInfo +impl AsRef<[SliceInfoElem]> for SliceInfo where - T: AsRef<[AxisSliceInfo]>, + T: AsRef<[SliceInfoElem]>, Din: Dimension, Dout: Dimension, { - fn as_ref(&self) -> &[AxisSliceInfo] { + fn as_ref(&self) -> &[SliceInfoElem] { self.indices.as_ref() } } impl<'a, T, Din, Dout> From<&'a SliceInfo> - for SliceInfo<&'a [AxisSliceInfo], Din, Dout> + for SliceInfo<&'a [SliceInfoElem], Din, Dout> where - T: AsRef<[AxisSliceInfo]>, + T: AsRef<[SliceInfoElem]>, Din: Dimension, Dout: Dimension, { - fn from(info: &'a SliceInfo) -> SliceInfo<&'a [AxisSliceInfo], Din, Dout> { + fn from(info: &'a SliceInfo) -> SliceInfo<&'a [SliceInfoElem], Din, Dout> { SliceInfo { in_dim: info.in_dim, out_dim: info.out_dim, @@ -681,8 +681,8 @@ impl_slicenextdim!((), NewAxis, Ix0, Ix1); /// counted from the end of the axis. Step sizes are also signed and may be /// negative, but must not be zero. /// -/// The syntax is `s![` *[ axis-slice-info [, axis-slice-info [ , ... ] ] ]* -/// `]`, where *axis-slice-info* is any of the following: +/// The syntax is `s![` *[ elem [, elem [ , ... ] ] ]* `]`, where *elem* is any +/// of the following: /// /// * *index*: an index to use for taking a subview with respect to that axis. /// (The index is selected. The axis is removed except with @@ -698,7 +698,7 @@ impl_slicenextdim!((), NewAxis, Ix0, Ix1); /// [`Slice`]: struct.Slice.html /// [`NewAxis`]: struct.NewAxis.html /// -/// The number of *axis-slice-info*, not including *new-axis*, must match the +/// The number of *elem*, not including *new-axis*, must match the /// number of axes in the array. *index*, *range*, *slice*, *step*, and /// *new-axis* can be expressions. *index* must be of type `isize`, `usize`, or /// `i32`. *range* must be of type `Range`, `RangeTo`, `RangeFrom`, or @@ -850,13 +850,13 @@ macro_rules! s( }; // Catch-all clause for syntax errors (@parse $($t:tt)*) => { compile_error!("Invalid syntax in s![] call.") }; - // convert range/index/new-axis into AxisSliceInfo + // convert range/index/new-axis into SliceInfoElem (@convert $r:expr) => { - <$crate::AxisSliceInfo as ::std::convert::From<_>>::from($r) + <$crate::SliceInfoElem as ::std::convert::From<_>>::from($r) }; - // convert range/index/new-axis and step into AxisSliceInfo + // convert range/index/new-axis and step into SliceInfoElem (@convert $r:expr, $s:expr) => { - <$crate::AxisSliceInfo as ::std::convert::From<_>>::from( + <$crate::SliceInfoElem as ::std::convert::From<_>>::from( <$crate::Slice as ::std::convert::From<_>>::from($r).step_by($s as isize) ) }; diff --git a/tests/array.rs b/tests/array.rs index 6bcdf7633..458b8e792 100644 --- a/tests/array.rs +++ b/tests/array.rs @@ -12,7 +12,7 @@ use itertools::{enumerate, zip, Itertools}; use ndarray::prelude::*; use ndarray::{arr3, rcarr2}; use ndarray::indices; -use ndarray::{AxisSliceInfo, Slice, SliceInfo}; +use ndarray::{Slice, SliceInfo, SliceInfoElem}; use std::convert::TryFrom; macro_rules! assert_panics { @@ -220,19 +220,19 @@ fn test_slice_dyninput_array_fixed() { fn test_slice_array_dyn() { let mut arr = Array3::::zeros((5, 2, 5)); let info = SliceInfo::<_, Ix3, IxDyn>::try_from([ - AxisSliceInfo::from(1..), - AxisSliceInfo::from(1), - AxisSliceInfo::from(NewAxis), - AxisSliceInfo::from(Slice::from(..).step_by(2)), + SliceInfoElem::from(1..), + SliceInfoElem::from(1), + SliceInfoElem::from(NewAxis), + SliceInfoElem::from(Slice::from(..).step_by(2)), ]) .unwrap(); arr.slice(info); arr.slice_mut(info); arr.view().slice_move(info); let info2 = SliceInfo::<_, Ix3, IxDyn>::try_from([ - AxisSliceInfo::from(1..), - AxisSliceInfo::from(1), - AxisSliceInfo::from(Slice::from(..).step_by(2)), + SliceInfoElem::from(1..), + SliceInfoElem::from(1), + SliceInfoElem::from(Slice::from(..).step_by(2)), ]) .unwrap(); arr.view().slice_collapse(info2); @@ -242,19 +242,19 @@ fn test_slice_array_dyn() { fn test_slice_dyninput_array_dyn() { let mut arr = Array3::::zeros((5, 2, 5)).into_dyn(); let info = SliceInfo::<_, Ix3, IxDyn>::try_from([ - AxisSliceInfo::from(1..), - AxisSliceInfo::from(1), - AxisSliceInfo::from(NewAxis), - AxisSliceInfo::from(Slice::from(..).step_by(2)), + SliceInfoElem::from(1..), + SliceInfoElem::from(1), + SliceInfoElem::from(NewAxis), + SliceInfoElem::from(Slice::from(..).step_by(2)), ]) .unwrap(); arr.slice(info); arr.slice_mut(info); arr.view().slice_move(info); let info2 = SliceInfo::<_, Ix3, IxDyn>::try_from([ - AxisSliceInfo::from(1..), - AxisSliceInfo::from(1), - AxisSliceInfo::from(Slice::from(..).step_by(2)), + SliceInfoElem::from(1..), + SliceInfoElem::from(1), + SliceInfoElem::from(Slice::from(..).step_by(2)), ]) .unwrap(); arr.view().slice_collapse(info2); @@ -264,19 +264,19 @@ fn test_slice_dyninput_array_dyn() { fn test_slice_dyninput_vec_fixed() { let mut arr = Array3::::zeros((5, 2, 5)).into_dyn(); let info = &SliceInfo::<_, Ix3, Ix3>::try_from(vec![ - AxisSliceInfo::from(1..), - AxisSliceInfo::from(1), - AxisSliceInfo::from(NewAxis), - AxisSliceInfo::from(Slice::from(..).step_by(2)), + SliceInfoElem::from(1..), + SliceInfoElem::from(1), + SliceInfoElem::from(NewAxis), + SliceInfoElem::from(Slice::from(..).step_by(2)), ]) .unwrap(); arr.slice(info); arr.slice_mut(info); arr.view().slice_move(info); let info2 = SliceInfo::<_, Ix3, Ix2>::try_from(vec![ - AxisSliceInfo::from(1..), - AxisSliceInfo::from(1), - AxisSliceInfo::from(Slice::from(..).step_by(2)), + SliceInfoElem::from(1..), + SliceInfoElem::from(1), + SliceInfoElem::from(Slice::from(..).step_by(2)), ]) .unwrap(); arr.view().slice_collapse(info2); @@ -286,19 +286,19 @@ fn test_slice_dyninput_vec_fixed() { fn test_slice_dyninput_vec_dyn() { let mut arr = Array3::::zeros((5, 2, 5)).into_dyn(); let info = &SliceInfo::<_, Ix3, IxDyn>::try_from(vec![ - AxisSliceInfo::from(1..), - AxisSliceInfo::from(1), - AxisSliceInfo::from(NewAxis), - AxisSliceInfo::from(Slice::from(..).step_by(2)), + SliceInfoElem::from(1..), + SliceInfoElem::from(1), + SliceInfoElem::from(NewAxis), + SliceInfoElem::from(Slice::from(..).step_by(2)), ]) .unwrap(); arr.slice(info); arr.slice_mut(info); arr.view().slice_move(info); let info2 = SliceInfo::<_, Ix3, IxDyn>::try_from(vec![ - AxisSliceInfo::from(1..), - AxisSliceInfo::from(1), - AxisSliceInfo::from(Slice::from(..).step_by(2)), + SliceInfoElem::from(1..), + SliceInfoElem::from(1), + SliceInfoElem::from(Slice::from(..).step_by(2)), ]) .unwrap(); arr.view().slice_collapse(info2); diff --git a/tests/oper.rs b/tests/oper.rs index 1decd2125..ed612bad2 100644 --- a/tests/oper.rs +++ b/tests/oper.rs @@ -561,7 +561,7 @@ fn scaled_add_2() { #[test] fn scaled_add_3() { use approx::assert_relative_eq; - use ndarray::{AxisSliceInfo, Slice, SliceInfo}; + use ndarray::{Slice, SliceInfo, SliceInfoElem}; use std::convert::TryFrom; let beta = -2.3; @@ -583,7 +583,7 @@ fn scaled_add_3() { let mut a = range_mat64(m, k); let mut answer = a.clone(); let cdim = if n == 1 { vec![q] } else { vec![n, q] }; - let cslice: Vec = if n == 1 { + let cslice: Vec = if n == 1 { vec![Slice::from(..).step_by(s2).into()] } else { vec![ From 252c2761fa8dc5e13d1202d3731261add4ff23d5 Mon Sep 17 00:00:00 2001 From: Jim Turner Date: Tue, 16 Mar 2021 18:31:43 -0400 Subject: [PATCH 2/2] Fix docs for slice;step case in s![] Before, the docs were misleading about the calculation of the final step size. --- src/slice.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/slice.rs b/src/slice.rs index 1965defd2..3c554a5ca 100644 --- a/src/slice.rs +++ b/src/slice.rs @@ -690,8 +690,8 @@ impl_slicenextdim!((), NewAxis, Ix0, Ix1); /// * *range*: a range with step size 1 to use for slicing that axis. /// * *range* `;` *step*: a range with step size *step* to use for slicing that axis. /// * *slice*: a [`Slice`] instance to use for slicing that axis. -/// * *slice* `;` *step*: a range constructed from the start and end of a [`Slice`] -/// instance, with new step size *step*, to use for slicing that axis. +/// * *slice* `;` *step*: a range constructed from a [`Slice`] instance, +/// multiplying the step size by *step*, to use for slicing that axis. /// * *new-axis*: a [`NewAxis`] instance that represents the creation of a new axis. /// (Except for [`.slice_collapse()`], which panics on [`NewAxis`] elements.) ///