Skip to content
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

Support aliasing in ArrayView::from_shape #438

Open
jturner314 opened this issue Apr 12, 2018 · 2 comments
Open

Support aliasing in ArrayView::from_shape #438

jturner314 opened this issue Apr 12, 2018 · 2 comments

Comments

@jturner314
Copy link
Member

It is safe for ArrayView instances to have aliasing elements. It would be good to add support for this in ArrayView::from_shape. I'm not sure whether we should support all possible types of aliasing, but it would be good to at least add support for stride 0 for axes with length greater than 1. (Views like this are often the result of broadcasting.)

@LukeMathWalker
Copy link
Member

What do you mean by aliasing elements? @jturner314

@jturner314
Copy link
Member Author

I'm referring to the case where there exists an element such that multiple different indices can be used to access that element. This is situation is safe for ArrayView but not for ArrayViewMut. This typically occurs with axes that have zero stride as a result of broadcasting, but it would be nice to support aliasing in ArrayView::from_shape for manually-created views.

For example, consider

extern crate ndarray;

use ndarray::prelude::*;

fn main() {
    let a = ArrayView1::from(&[1, 2, 3]);
    let b = a.broadcast((2, 3)).unwrap();
    println!("{:?}", b);
}

which prints

[[1, 2, 3],
 [1, 2, 3]] shape=[2, 3], strides=[0, 1], layout=Custom (0x0), const ndim=2

The elements are aliased in the sense that e.g. b[(0,0)] and b[(1,0)] refer to the same element.

It would be nice to obtain b directly without broadcasting like this:

extern crate ndarray;

use ndarray::prelude::*;

fn main() {
    let b = ArrayView::from_shape((2, 3).strides((0, 1)), &[1, 2, 3]).unwrap();
    println!("{:?}", b);
}

but that currently exits with "thread 'main' panicked at 'called Result::unwrap() on an Err value: ShapeError/Unsupported: unsupported operation', src/libcore/result.rs:997:5".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants