Skip to content

Binary operation for ArrayView #237

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

Closed
termoshtt opened this issue Nov 15, 2016 · 3 comments
Closed

Binary operation for ArrayView #237

termoshtt opened this issue Nov 15, 2016 · 3 comments
Labels

Comments

@termoshtt
Copy link
Member

I'm confusing errors arising around +.

let dist = Range::new(0, 1);
let a = Array::<f64, _>::random((5, 5));
let x0 = Array::<f64, _>::random(5);
let xs: Vec<_> = a.axis_iter(Axis(0)).map(|x| x + &x0);

I expect that x + &x0 produces new array since document says

&A @ &A which produces a new Array

But it produces an error:

error[E0369]: binary operation `+` cannot be applied to type `ndarray::ArrayBase<ndarray::ViewRepr<&f64>, usize>` 

After struggling, I find it works when I rewrite the last line as following:

let xs: Vec<_> = a.axis_iter(Axis(0)).map(|x| x.to_owned() + &x0);

I hope the first my code works. Is there any technical problem to implement Add for ArrayView?

@bluss
Copy link
Member

bluss commented Nov 15, 2016

x in x + &x0 is an array view, which is an "A", not an "&A". So &x + &x0 should work.

@termoshtt
Copy link
Member Author

Oh...
I now understand my mistake and what document says.
The above error message means ArrayView cannot be consumed. It is hard to read :<

Thanks for your help!

@bluss
Copy link
Member

bluss commented Nov 15, 2016

It's a bit unfortunate that one needs to pass &ArrayView in many places where just passing a view by value should be enough. It's partly due to missing support for custom view types in Rust. Feel free to report any issues you find with ndarray.

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

No branches or pull requests

2 participants