-
Notifications
You must be signed in to change notification settings - Fork 323
Is indexing with a variable number of indices supported? #865
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
Comments
At the moment, the best way to accomplish this would be to call This seems like a common enough task that it would be worth adding pub fn lane<I>(&self, axis: Axis, index: I) -> ArrayView1<'_, A>
where
S: Data,
D: RemoveAxis,
I: NdIndex<D::Smaller>,
{
self.view().lane_move(axis, index)
}
pub fn lane_mut<I>(&mut self, axis: Axis, index: I) -> ArrayViewMut1<'_, A>
where
S: DataMut,
D: RemoveAxis,
I: NdIndex<D::Smaller>,
{
self.view_mut().lane_move(axis, index)
}
pub fn lane_move<I>(self, axis: Axis, index: I) -> ArrayBase<S, Ix1>
where
D: RemoveAxis,
I: NdIndex<D::Smaller>,
{
let ax = axis.index();
let removed_dim = self.dim.remove_axis(axis);
let removed_strides = self.strides.remove_axis(axis);
// FIXME: Add better panic handling than `.unwrap()`.
let offset = index.index_checked(&removed_dim, &removed_strides).unwrap();
ArrayBase {
ptr: unsafe { self.ptr.offset(offset) },
data: self.data,
dim: Ix1(self.dim[ax]),
strides: Ix1(self.strides[ax]),
}
} Maybe someone could finish this up (fix the Long term, I'd like to add NumPy's "ellipsis" and "new axis" slicing functionality to |
Wouldn't index_axis_move cause a copy each time? and id need to iterate over the spatial axes and do that once per axis? That seems prohibitively slow. I also need to not destroy the shape of the original array - I need to be able to get a mutable view on it without consuming the original |
Edit: If you do need to loop over the lanes rather than just index to get a single one, then you should use |
This sounds a bit like you'd like to be able to index a lanes producer, and that could be a reasonable feature request in itself. If it's an iteration - does using Zip with a lanes producer work for this use case? Or what are the limitations there? In some ways, we need to keep some of our numpy intuitions with this crate - keep operations "vectorized", avoid indexing. Indexing is bounds checked in Rust, and multidimensional bounds checking has some overhead. Ndarray tries to provide traversal features like Zip and the lanes producers, and they should be the best. |
It wouldn't be iteration over all lanes, only over a single lane (or maybe a subset of lanes). In my specific case (which again is more specific than the actual numpy feature I linked), for a tensor with Here's the workaround code I'm using now which is incredibly hacky and probably very inefficient: for coord in event_coords {
use ndarray::ArrayViewMut1;
assert_eq!(coord.len(), self.accumulator.ndim() - 1);
// TODO: Avoid this match by figuring out how to index on a variable
// number of dimensions
let filter_outputs: ArrayViewMut1<DType> = match self.accumulator.ndim() {
1 => self.accumulator.slice_mut(s![..]),
2 => self.accumulator.slice_mut(s![coord[0], ..]),
3 => self.accumulator.slice_mut(s![coord[0], coord[1], ..]),
4 => self
.accumulator
.slice_mut(s![coord[0], coord[1], coord[2], ..]),
_ => todo!(
"We currently switch on ndims at runtime, but we \
ultimately want to not have to do this and instead use \
slicing with variable numbers of indices. For this reason,
only common shapes are supported. See \
https://github.com/rust-ndarray/ndarray/issues/865"
),
};
todo!("do something with `filter_outputs`")
} |
I'd like to index a tensor along all but the last axis, getting as a result a 1d view of the innermost axis. I thought I'd be able to use
ArrayBase::slice()
for this, but I need create the index with thes!
macro and I can't figure out how to do that with a variable number of indices (such as if the user provides the coordinates of the spatial dimensions in a tensor, and I want to slice on that leaving a 1d view of the feature dimension where the feature dimension is the innermost dimension).I asked a stack overflow question for this here, if it is indeed supported then perhaps we could add a stack overflow answer there? If not, when would support be added and is there a workaround in the meantime?
The equivalent numpy feature, which is actually slightly more general than my specific use case since I only need a view on the innermost axis, is here
The text was updated successfully, but these errors were encountered: