| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
|
The broadcast_ref implementation is copy pasted from broadcast, 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. |
Sorry, something went wrong.
|
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 to ArrayView, as in this PR. I don't like the name broadcast_ref much, but it's fine if we can't come up with anything better.
src/dimension/mod.rs would be a good place for upcast, since there are other similar functions in that file, e.g. do_slice. I think just moving upcast into that file would be sufficient; the rest of the code in broadcast is small enough to just duplicate, IMO. |
Sorry, something went wrong.
This will allow upcast to be reused in other functions, such as the upcoming ArrayView::broadcast_ref.
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:
```rust
fn 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 the
lifetime of `coord_x_s`, instead of being bound to 'a.
This commit introduces a new function, broadcast_ref, that does just
that.
|
Hello! |
Sorry, something went wrong.
|
@jreniel Please open an issue if you want to discuss an implementation of meshgrid. It's not related to this PR. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
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 of coord_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.