- Notifications
You must be signed in to change notification settings - Fork338
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
base:master
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
The |
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 I don't like the name
|
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 commentedFeb 2, 2024
Hello! |
@jreniel Please open an issue if you want to discuss an implementation of |
Uh oh!
There was an error while loading.Please reload this page.
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 numpy
meshgrid
function: It could be implemented like so:Unfortunately, this doesn't work, because
coords_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.