Skip to content

Documentation improvements for new users #587

Description

@xd009642

So I'm a new user to ndarray and was recommended to open an issue with any roadblocks or issues I have using ndarray as areas where the docs or examples could be improved to make it easier for people to use.

Currently my one issue is trying to multiply two views of an array together saving the result in a new array. I'm taking a window of an array, multiplying it by a provided ArrayView3 but I just get some variant of

Note: an implementation of `std::ops::Mul` might be missing for `ndarray::ArrayBase<ndarray::OwnedRepr<T>, ndarray::dimension::dim::Dim<[usize; 3]>>`

Or

Note: an implementation of `std::ops::Mul` might be missing for `ndarray::ArrayBase<ndarray::ViewRepr<&T>, ndarray::dimension::dim::Dim<[usize; 3]>>`

I thought from the docs here that multiplying references to the Window and the ArrayView3 would work. But it doesn't and I haven't found any docs or examples that have helped me figure this out yet.

For reference the code is here:

https://github.com/xd009642/ndarray-vision/blob/eba3eff041be6d88f4605261138893d1a3d90c33/src/core/image.rs#L46

Activity

  1. jturner314 commented on Feb 15, 2019

    @jturner314
    Member

    There are a few issues:

    1. T and U need more trait bounds (Mul on T and Clone on U).
    2. When multiplying two variables of type ArrayView together, a & must be explicitly written for each operand. The reason is that there are a few implementations of Mul for various combinations of owned arrays and references to arrays. For example, if the left operand is an owned array, then you can use this implementation or this one, neither of which needs to allocate a new array for the result. (They update the owned array in-place and return it.) When you only have two views, however, you need to use this implementation, which takes two references to arrays. See also Add examples of binary arithmetic operators #589.
    3. The implementation of Mul<&'a ArrayBase<S2, E>> for &'a ArrayBase<S, D> (the one you need to use) currently doesn't allow arrays with elements of different types (T and U in the example you provided). I've created Allow ops on arrays with elems of different types #588 to fix this.

    These changes make the project compile. Note that I've temporarily added a patch for ndarray to incorporate #588 until it's merged and a new version of ndarray is released. Alternatively, if you want to use the most recent release of ndarray without this patch, you could eliminate the U type parameter and use T where U is now.

    By the way, welcome to ndarray!

  2. xd009642 commented on Feb 15, 2019

    @xd009642
    ContributorAuthor

    Ah thanks! The T and U thing was one of my attempts to fix the issue! I've gone back to just T now as I originally wrote.

    The example in #588 does help though I'd potentially add a note just to tell people that to use an arithmetic operator their type T should implement it. It does feel like a bit of a silly mistake now I see it but it honestly hadn't occurred to me I was thinking it might have been down to the ViewRepr vs OwnedRepr or the fact T didn't impl one of the Data traits.

    And thanks I'm looking forward to getting deeper into ndarray 😄. Since my current issue has been resolved you can close this if you want. I'll open new issues if I encounter any other troubles

  3. added
    docsIssues relating primarily to documentation or API visibility.
    on Feb 25, 2019
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

    docsIssues relating primarily to documentation or API visibility.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions