rust-ndarray / ndarray

ndarray: an N-dimensional array with array views, multidimensional slicing, and efficient operations
https://docs.rs/ndarray/
Apache License 2.0
3.63k stars 307 forks source link

ndarray::ArrayViewMut::windows should produce mutable windows #1357

Open Chris00 opened 10 months ago

Chris00 commented 10 months ago

Calling .windows() on an ndarray::ArrayViewMut should return mutable windows. That's very useful when, for example, you iterate over a partition of an interval and the quantities you compute affect values living on both ends of the interval.

bluss commented 10 months ago

That's not possible by lifetime rules in Rust - the iterator's elements are simultaneously valid, and since they are windows, they partially overlap, which is not allowed for mutable references.

We'll need to update the issue with a plan for a way to get the intended effect in a way we can implement.

akern40 commented 1 month ago

There is a pattern known as "lending iterators" which allow things like windows_mut. See this article by Niko Matsakis for the general concept, and these crates for implementations of that pattern. We could then support a windows_mut function that returns this kind of iterator.