8000 Add support for inserting new axes while slicing by jturner314 · Pull Request #570 · rust-ndarray/ndarray · GitHub
[go: up one dir, main page]

Skip to content

Add support for inserting new axes while slicing #570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Mar 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
24a3299
Rename SliceOrIndex to AxisSliceInfo
jturner314 Dec 9, 2018
6a16b88
Switch from Dimension::SliceArg to CanSlice trait
jturner314 Dec 9, 2018
546b69c
Add support for inserting new axes while slicing
jturner314 Dec 9, 2018
6e335ca
Rename SliceInfo generic params to Din and Dout
jturner314 Dec 17, 2018
d6b9cb0
Improve code style
jturner314 Dec 17, 2018
438d69a
Derive Clone, Copy, and Debug for NewAxis
jturner314 Dec 17, 2018
6050df3
Use stringify! for string literal of type name
jturner314 Dec 18, 2018
8d45268
Make step_by panic for variants other than Slice
jturner314 Dec 18, 2018
1d15275
Add DimAdd trait
jturner314 Dec 18, 2018
41cc4a1
Replace SliceNextIn/OutDim with SliceArg trait
jturner314 Dec 18, 2018
c66ad8c
Combine DimAdd impls for Ix0
jturner314 Feb 7, 2021
7776bfc
Implement CanSlice<IxDyn> for [AxisSliceInfo]
jturner314 Feb 14, 2021
ab79d28
Change SliceInfo to be repr(transparent)
jturner314 Feb 15, 2021
615113e
Add debug assertions to SliceInfo::new_unchecked
jturner314 Feb 15, 2021
e66e3c8
Fix safety of SliceInfo::new
jturner314 Feb 15, 2021
3ba6ceb
Add some impls of TryFrom for SliceInfo
jturner314 Feb 15, 2021
815e708
Make slice_move not call slice_collapse
jturner314 Feb 16, 2021
25a7bb0
Make slice_collapse return Err(_) for NewAxis
jturner314 Feb 16, 2021
5202a50
Expose CanSlice trait in public API
jturner314 Feb 16, 2021
319701d
Expose MultiSlice trait in public API
jturner314 Feb 16, 2021
d5d6482
Add DimAdd bounds to Dimension trait
jturner314 Feb 16, 2021
9614b13
Revert "Make slice_collapse return Err(_) for NewAxis"
jturner314 Feb 17, 2021
61cf7c0
Make slice_collapse panic on NewAxis
jturner314 Feb 17, 2021
91dbf3f
Rename DimAdd::Out to DimAdd::Output
jturner314 Feb 17, 2021
5dc77bd
Rename SliceArg to SliceNextDim
jturner314 Feb 17, 2021
87515c6
Rename CanSlice to SliceArg
jturner314 Feb 17, 2021
c4efbbf
Rename MultiSlice to MultiSliceArg
jturner314 Feb 17, 2021
7506f90
Clarify docs of .slice_collapse()
jturner314 Feb 17, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
Next Next commit
Rename SliceOrIndex to AxisSliceInfo
  • Loading branch information
jturner314 authored and bluss committed Mar 12, 2021
commit 24a3299007ae569c9c2aa6c4038ccd7e2302ee8d
8 changes: 4 additions & 4 deletions blas-tests/tests/oper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ 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, SliceInfo};
use ndarray::{Data, LinalgScalar};
use ndarray::{Ix, Ixs, SliceInfo, SliceOrIndex};

use approx::{assert_abs_diff_eq, assert_relative_eq};
use defmac::defmac;
Expand Down Expand Up @@ -420,11 +420,11 @@ fn scaled_add_3() {
let mut answer = a.clone();
let cdim = if n == 1 { vec![q] } else { vec![n, q] };
let cslice = if n == 1 {
vec![SliceOrIndex::from(..).step_by(s2)]
vec![AxisSliceInfo::from(..).step_by(s2)]
} else {
vec![
SliceOrIndex::from(..).step_by(s1),
SliceOrIndex::from(..).step_by(s2),
AxisSliceInfo::from(..).step_by(s1),
AxisSliceInfo::from(..).step_by(s2),
]
};

Expand Down
22 changes: 11 additions & 11 deletions src/dimension/dimension_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{Axis, DimMax};
use crate::IntoDimension;
use crate::RemoveAxis;
use crate::{ArrayView1, ArrayViewMut1};
use crate::{Dim, Ix, Ix0, Ix1, Ix2, Ix3, Ix4, Ix5, Ix6, IxDyn, IxDynImpl, Ixs, SliceOrIndex};
use crate::{AxisSliceInfo, Dim, Ix, Ix0, Ix1, Ix2, Ix3, Ix4, Ix5, Ix6, IxDyn, IxDynImpl, Ixs};

/// Array shape and index trait.
///
Expand Down Expand Up @@ -63,14 +63,14 @@ pub trait Dimension:
/// size, which you pass by reference. For the dynamic dimension it is
/// a slice.
///
/// - For `Ix1`: `[SliceOrIndex; 1]`
/// - For `Ix2`: `[SliceOrIndex; 2]`
/// - For `Ix1`: `[AxisSliceInfo; 1]`
/// - For `Ix2`: `[AxisSliceInfo; 2]`
/// - and so on..
/// - For `IxDyn`: `[SliceOrIndex]`
/// - For `IxDyn`: `[AxisSliceInfo]`
///
/// The easiest way to create a `&SliceInfo<SliceArg, Do>` is using the
/// [`s![]`](macro.s!.html) macro.
type SliceArg: ?Sized + AsRef<[SliceOrIndex]>;
type SliceArg: ?Sized + AsRef<[AxisSliceInfo]>;
/// Pattern matching friendly form of the dimension value.
///
/// - For `Ix1`: `usize`,
Expand Down Expand Up @@ -399,7 +399,7 @@ macro_rules! impl_insert_axis_array(

impl Dimension for Dim<[Ix; 0]> {
const NDIM: Option<usize> = Some(0);
type SliceArg = [SliceOrIndex; 0];
type SliceArg = [AxisSliceInfo; 0];
type Pattern = ();
type Smaller = Self;
type Larger = Ix1;
Expand Down Expand Up @@ -443,7 +443,7 @@ impl Dimension for Dim<[Ix; 0]> {

impl Dimension for Dim<[Ix; 1]> {
const NDIM: Option<usize> = Some(1);
type SliceArg = [SliceOrIndex; 1];
type SliceArg = [AxisSliceInfo; 1];
type Pattern = Ix;
type Smaller = Ix0;
type Larger = Ix2;
Expand Down Expand Up @@ -559,7 +559,7 @@ impl Dimension for Dim<[Ix; 1]> {

impl Dimension for Dim<[Ix; 2]> {
const NDIM: Option<usize> = Some(2);
type SliceArg = [SliceOrIndex; 2];
type SliceArg = [AxisSliceInfo; 2];
type Pattern = (Ix, Ix);
type Smaller = Ix1;
type Larger = Ix3;
Expand Down Expand Up @@ -716,7 +716,7 @@ impl Dimension for Dim<[Ix; 2]> {

impl Dimension for Dim<[Ix; 3]> {
const NDIM: Option<usize> = Some(3);
type SliceArg = [SliceOrIndex; 3];
type SliceArg = [AxisSliceInfo; 3];
type Pattern = (Ix, Ix, Ix);
type Smaller = Ix2;
type Larger = Ix4;
Expand Down Expand Up @@ -839,7 +839,7 @@ macro_rules! large_dim {
($n:expr, $name:ident, $pattern:ty, $larger:ty, { $($insert_axis:tt)* }) => (
impl Dimension for Dim<[Ix; $n]> {
const NDIM: Option<usize> = Some($n);
type SliceArg = [SliceOrIndex; $n];
type SliceArg = [AxisSliceInfo; $n];
type Pattern = $pattern;
type Smaller = Dim<[Ix; $n - 1]>;
type Larger = $larger;
Expand Down Expand Up @@ -890,7 +890,7 @@ large_dim!(6, Ix6, (Ix, Ix, Ix, Ix, Ix, Ix), IxDyn, {
/// and memory wasteful, but it allows an arbitrary and dynamic number of axes.
impl Dimension for IxDyn {
const NDIM: Option<usize> = None;
type SliceArg = [SliceOrIndex];
type SliceArg = [AxisSliceInfo];
type Pattern = Self;
type Smaller = Self;
type Larger = Self;
Expand Down
14 changes: 7 additions & 7 deletions src/dimension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// except according to those terms.

use crate::error::{from_kind, ErrorKind, ShapeError};
use crate::{Ix, Ixs, Slice, SliceOrIndex};
use crate::{AxisSliceInfo, Ix, Ixs, Slice};
use num_integer::div_floor;

pub use self::axes::{axes_of, Axes, AxisDescription};
Expand Down Expand Up @@ -601,15 +601,15 @@ pub fn slices_intersect<D: Dimension>(
) -> bool {
debug_assert_eq!(indices1.as_ref().len(), indices2.as_ref().len());
for (&axis_len, &si1, &si2) in izip!(dim.slice(), indices1.as_ref(), indices2.as_ref()) {
// The slices do not intersect iff any pair of `SliceOrIndex` does not intersect.
// The slices do not intersect iff any pair of `AxisSliceInfo` does not intersect.
match (si1, si2) {
(
SliceOrIndex::Slice {
AxisSliceInfo::Slice {
start: start1,
end: end1,
step: step1,
},
SliceOrIndex::Slice {
AxisSliceInfo::Slice {
start: start2,
end: end2,
step: step2,
Expand All @@ -630,8 +630,8 @@ pub fn slices_intersect<D: Dimension>(
return false;
}
}
(SliceOrIndex::Slice { start, end, step }, SliceOrIndex::Index(ind))
| (SliceOrIndex::Index(ind), SliceOrIndex::Slice { start, end, step }) => {
(AxisSliceInfo::Slice { start, end, step }, AxisSliceInfo::Index(ind))
| (AxisSliceInfo::Index(ind), AxisSliceInfo::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,
Expand All @@ -641,7 +641,7 @@ pub fn slices_intersect<D: Dimension>(
return false;
}
}
(SliceOrIndex::Index(ind1), SliceOrIndex::Index(ind2)) => {
(AxisSliceInfo::Index(ind1), AxisSliceInfo::Index(ind2)) => {
let ind1 = abs_index(axis_len, ind1);
let ind2 = abs_index(axis_len, ind2);
if ind1 != ind2 {
Expand Down
14 changes: 7 additions & 7 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::iter::{
};
use crate::slice::MultiSlice;
use crate::stacking::concatenate;
use crate::{NdIndex, Slice, SliceInfo, SliceOrIndex};
use crate::{AxisSliceInfo, NdIndex, Slice, SliceInfo};

/// # Methods For All Array Types
impl<A, S, D> ArrayBase<S, D>
Expand Down Expand Up @@ -417,16 +417,16 @@ where
// Slice and collapse in-place without changing the number of dimensions.
self.slice_collapse(&*info);

let indices: &[SliceOrIndex] = (**info).as_ref();
let indices: &[AxisSliceInfo] = (**info).as_ref();

// Copy the dim and strides that remain after removing the subview axes.
let out_ndim = info.out_ndim();
let mut new_dim = Do::zeros(out_ndim);
let mut new_strides = Do::zeros(out_ndim);
izip!(self.dim.slice(), self.strides.slice(), indices)
.filter_map(|(d, s, slice_or_index)| match slice_or_index {
SliceOrIndex::Slice { .. } => Some((d, s)),
SliceOrIndex::Index(_) => None,
AxisSliceInfo::Slice { .. } => Some((d, s)),
AxisSliceInfo::Index(_) => None,
})
.zip(izip!(new_dim.slice_mut(), new_strides.slice_mut()))
.for_each(|((d, s), (new_d, new_s))| {
Expand Down Expand Up @@ -455,16 +455,16 @@ where
/// **Panics** if an index is out of bounds or step size is zero.<br>
/// (**Panics** if `D` is `IxDyn` and `indices` does not match the number of array axes.)
pub fn slice_collapse(&mut self, indices: &D::SliceArg) {
let indices: &[SliceOrIndex] = indices.as_ref();
let indices: &[AxisSliceInfo] = indices.as_ref();
assert_eq!(indices.len(), self.ndim());
indices
.iter()
.enumerate()
.for_each(|(axis, &slice_or_index)| match slice_or_index {
SliceOrIndex::Slice { start, end, step } => {
AxisSliceInfo::Slice { start, end, step } => {
self.slice_axis_inplace(Axis(axis), Slice { start, end, step })
}
SliceOrIndex::Index(index) => {
AxisSliceInfo::Index(index) => {
let i_usize = abs_index(self.len_of(Axis(axis)), index);
self.collapse_axis(Axis(axis), i_usize)
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub use crate::dimension::IxDynImpl;
pub use crate::dimension::NdIndex;
pub use crate::error::{ErrorKind, ShapeError};
pub use crate::indexes::{indices, indices_of};
pub use crate::slice::{Slice, SliceInfo, SliceNextDim, SliceOrIndex};
pub use crate::slice::{AxisSliceInfo, Slice, SliceInfo, SliceNextDim};

use crate::iterators::Baseiter;
use crate::iterators::{ElementsBase, ElementsBaseMut, Iter, IterMut, Lanes};
Expand Down
Loading
0