8000 Document NdProducer::raw_dim by jturner314 · Pull Request #918 · rust-ndarray/ndarray · GitHub
[go: up one dir, main page]

Skip to content

Document NdProducer::raw_dim#918

Merged
jturner314 merged 1 commit intorust-ndarray:masterfrom
jturner314:NdProducer_raw_dim
Feb 17, 2021
Merged

Document NdProducer::raw_dim#918
jturner314 merged 1 commit intorust-ndarray:masterfrom
jturner314:NdProducer_raw_dim

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