Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document NdProducer::raw_dim #918

Merged
merged 1 commit into from
Feb 17, 2021

Conversation

jturner314
Copy link
Member

This is useful because, otherwise, users have to manually calculate the shape of the producer to e.g. allocate an array of the correct shape to zip with it.

This is useful because, otherwise, users have to manually calculate
the shape of the producer to e.g. allocate an array of the correct
shape to zip with it.
@bluss
Copy link
Member

bluss commented Feb 16, 2021

Users can use .map_collect to have an array of the right shape and layout. I'm also drafting something that makes it easy to do something like this: Array::zeros(a.shape_builder()) to get an array of the same shape and layout (coarsely defined - probably only selecting C/F).

@jturner314
Copy link
Member Author

jturner314 commented Feb 17, 2021

Users can use .map_collect to have an array of the right shape and layout.

Right; my earlier comment wasn't clear. I'm thinking specifically of a discussion with a user on #rust-sci who wanted to copy windows of an ArrayView1 into columns of a new Array2. Calling Zip::from(arr_1d.windows()).map_collect(...) would result in Array1<...>. If NdProducer::raw_dim is exposed, then the following works:

fn windows_to_cols<A>(arr: ArrayView1<'_, A>, window_len: usize) -> Array2<A>
where
    A: Clone + Zero,
{
    let windows = arr.windows(window_len);
    let mut out = Array2::zeros([window_len, windows.raw_dim()[0]].f());
    azip!(
        (mut col in out.axis_iter_mut(Axis(1)), window in windows)
        col.assign(&window)
    );
    out
}

I'm also drafting something that makes it easy to do something like this: Array::zeros(a.shape_builder()) to get an array of the same shape and layout (coarsely defined - probably only selecting C/F).

That would be convenient.

@jturner314 jturner314 merged commit 1bf4b2d into rust-ndarray:master Feb 17, 2021
@jturner314 jturner314 deleted the NdProducer_raw_dim branch February 17, 2021 06:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants