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

Support zero-length axis in .map_axis/_mut() #579

Closed
jturner314 opened this issue Jan 7, 2019 · 8 comments
Closed

Support zero-length axis in .map_axis/_mut() #579

jturner314 opened this issue Jan 7, 2019 · 8 comments

Comments

@jturner314
Copy link
Member

jturner314 commented Jan 7, 2019

The implementation of .map_axis/_mut() panics when the specified axis is zero-length. Ideally, we'd just apply mapping to empty views instead.

Example:

extern crate ndarray;

use ndarray::prelude::*;

fn main() {
    let a = Array2::<f32>::zeros((0, 2));
    a.map_axis(Axis(0), |_| {});
}
@andrei-papou
Copy link
Contributor

Hi @jturner314 . I can work on this one if you don't mind.

@jturner314
Copy link
Member Author

@andrei-papou Sure, please feel free.

@andrei-papou
Copy link
Contributor

@jturner314 could you please point me to an example of creating an empty array? Thanks a lot.

@jturner314
Copy link
Member Author

jturner314 commented Mar 27, 2019

The simplest way to create a 1-dimensional, zero-length view in map_axis would be unsafe { ArrayView::new_(std::ptr::NonNull::<A>::dangling().as_ptr(), Ix1(0), Ix1(0)) }. See this commentary.

Edit: If that doesn't compile, you might need a cast: unsafe { ArrayView::new_(std::ptr::NonNull::<A>::dangling().as_ptr() as *const A, Ix1(0), Ix1(0)) }.

@andrei-papou
Copy link
Contributor

@jturner314 thanks. please take a look at the PR #612

@jturner314
Copy link
Member Author

Fwiw, I realized that a cleaner (safe) way to create a zero-length one-dimensional view is ArrayView1::from(&[]).

@jblondin
Copy link

PR was merged. Is anything still remaining here, or can this issue be closed?

@jturner314
Copy link
Member Author

Fixed by #612. Thanks for following up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants