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 a lifetime-relaxed broadcast for ArrayView#1219

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
roblabla wants to merge2 commits intorust-ndarray:master
base:master
Choose a base branch
Loading
fromroblabla:broadcast-ref

Conversation

roblabla
Copy link

@roblablaroblabla commentedOct 10, 2022
edited
Loading

ArrayView::broadcast has a lifetime that depends on &self instead of its internal buffer. This prevents writing some types of functions in an allocation-free way. For instance, take the numpymeshgrid function: It could be implemented like so:

fnmeshgrid_2d<'a,'b>(coords_x:ArrayView1<'a,X>,coords_y:ArrayView1<'b,X>) ->(ArrayView2<'a,X>,ArrayView2<'b,X>){let x_len = coords_x.shape()[0];let y_len = coords_y.shape()[0];let coords_x_s = coords_x.into_shape((1, y_len)).unwrap();let coords_x_b = coords_x_s.broadcast((x_len, y_len)).unwrap();let coords_y_s = coords_y.into_shape((x_len,1)).unwrap();let coords_y_b = coords_y_s.broadcast((x_len, y_len)).unwrap();(coords_x_b, coords_y_b)}

Unfortunately, this doesn't work, becausecoords_x_b is bound to the lifetime ofcoord_x_s, instead of being bound to 'a.

This PR introduces a new function, broadcast_ref, that behaves exactly like broadcast, but the returned arrayview is bound to the lifetime of the internal storage instead.

@roblabla
Copy link
Author

Thebroadcast_ref implementation is copy pasted frombroadcast, which is kinda bad. The common code (in particular,upcast) should probably be moved to a common module, but I'm not sure where it should go.

@jturner314
Copy link
Member

Thanks for this PR! I agree that this method would be useful.

This is related to issue#1208, which discussed slicing. However, broadcasting is different, because we can't have a broadcasting equivalent of.slice_move() for arbitrary storage types. We have to have a method specific toArrayView, as in this PR.

I don't like the namebroadcast_ref much, but it's fine if we can't come up with anything better.

Thebroadcast_ref implementation is copy pasted frombroadcast, which is kinda bad. The common code (in particular,upcast) should probably be moved to a common module, but I'm not sure where it should go.

src/dimension/mod.rs would be a good place forupcast, since there are other similar functions in that file, e.g.do_slice. I think just movingupcast into that file would be sufficient; the rest of the code inbroadcast is small enough to just duplicate, IMO.

This will allow upcast to be reused in other functions, such as theupcoming ArrayView::broadcast_ref.
ArrayView::broadcast has a lifetime that depends on &self instead of itsinternal buffer. This prevents writing some types of functions in anallocation-free way. For instance, take the numpy `meshgrid` function:It could be implemented like so:```rustfn meshgrid_2d<'a, 'b>(coords_x: ArrayView1<'a, X>, coords_y: ArrayView1<'b, X>) -> (ArrayView2<'a, X>, ArrayView2<'b, X>) {    let x_len = coords_x.shape()[0];    let y_len = coords_y.shape()[0];    let coords_x_s = coords_x.into_shape((1, y_len)).unwrap();    let coords_x_b = coords_x_s.broadcast((x_len, y_len)).unwrap();    let coords_y_s = coords_y.into_shape((x_len, 1)).unwrap();    let coords_y_b = coords_y_s.broadcast((x_len, y_len)).unwrap();    (coords_x_b, coords_y_b)}```Unfortunately, this doesn't work, because `coords_x_b` is bound to thelifetime of `coord_x_s`, instead of being bound to 'a.This commit introduces a new function, broadcast_ref, that does justthat.
@jreniel
Copy link

Hello!
I was looking for an equivalent of numpy's meshgrid on ndarray. Because I couldn't find it, I came up with my own version, which should work in n-dimensions. It is currently hostedhere. I think this should belong into ndarray, and not in a separate crate, so I'd be more than happy, provided that you find my implementation satisfying, to see this code directly integrated into ndarray, instead of having a separate crate as it currently is.

@nilgoyette
Copy link
Collaborator

@jreniel Please open an issue if you want to discuss an implementation ofmeshgrid. It's not related to this PR.

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.

4 participants
@roblabla@jturner314@jreniel@nilgoyette

[8]ページ先頭

©2009-2025 Movatter.jp