Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Impl NdIndex<IxDyn> for &Vec<Ix> and Vec<Ix>#1146

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
bderrett wants to merge1 commit intorust-ndarray:master
base:master
Choose a base branch
Loading
frombderrett:main

Conversation

bderrett
Copy link

@bderrettbderrett commentedJan 18, 2022
edited
Loading

This change attempts to improve the ergonomics of indexing dynamic dimension arrays.

Let's say we have:

letmut a =ArrayD::<f32>::zeros(vec![1,2]);let index =vec![0,0];

(Of course, in practice, the number of dims is dynamic.)

Then currently we can't doa[&index] = 1.0 ora[index] = 1.0. Instead we seem to need to do

let r:&[usize] =&index;a[r] =1.0;

This change makes the first two methods of indexing possible.

I'm new tondarray, so there may be a discussion on this somewhere that I haven't seen. The new impls were created by copying the impl of &[Ix] (for &Vec) and by modifying the impl of &[Ix] (for Vec).

I can add tests for this if it looks useful?

@jturner314
Copy link
Member

Thanks for the PR. I agree that it may be worth addingIndex implementations forVec<usize> and&Vec<usize>; there isn't much downside. Alternatively, we could add implementations forVec<usize> and&T where T: NdIndex<IxDyn>.

@bluss What do you think?

Fwiw, this currently works withndarray:

letmut a =ArrayD::<f32>::zeros(vec![1,2]);let index =vec![0,0];a[&*index] =1.0;

(Note the extra* to use theVec'sDeref implementation.)

Ifindex is&Vec<usize>, it's a bit more awkward:a[&**index] = 1.0 ora[index.as_slice()] = 1.0.

Regarding the implementation -- I'd suggest converting to&[usize] and using the existingNdIndex<IxDyn> for &'a [Ix] implementation. (Look atNdIndex<IxDyn> for &'a IxDyn for a similar case.)

@bderrett
Copy link
Author

Many thanks for the comments. The PR now adds implementations forVec<usize> and&T where T: NdIndex<IxDyn>.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@bderrett@jturner314

[8]ページ先頭

©2009-2025 Movatter.jp