Skip to content

Binary operation for ArrayView #237

Closed

Description

@termoshtt

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?

Activity

  1. bluss commented on Nov 15, 2016

    @bluss
    Member

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

  2. termoshtt commented on Nov 15, 2016

    @termoshtt
    MemberAuthor

    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!

  3. bluss commented on Nov 15, 2016

    @bluss
    Member

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions