-
-
Notifications
You must be signed in to change notification settings - Fork 11k
ENH: Add np.asanycontiguousarray #7217
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
Conversation
Numpy has np.ascontiguousarray() and np.asfortranarray(), but doesn't have a function to return an array of either contiguity depending on the input, making a copy only when necessary.
Probably should post it to the NumPy mailing list if you haven't already. If you have, maybe a link to the mailing list entry would be nice and vice versa. |
if is_f_contiguous: | ||
return array(a, dtype, copy=False, order='F', ndmin=1) | ||
else: | ||
return array(a, dtype, copy=False, order='C', ndmin=1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, I like the idea too much, but that is not a deal breaker (also the any
has a bit of a name clash with asanyarray
). But I think you can simplify this to a single line function:
return array(a, dtype, copy=False, order="A", ndmin=1)
(not that I like the ndmin=1
either, what is so bad about 0-D arrays? But I guess the ascontiguousarray functions do that too). The whole f_contiguous stuff should be handled by "A" is my guess.
Needs release note. |
If @seberg is right that this is just |
I'm also -0.5 for this. We need to limit the growth of small functions at some point. |
Unfortunatly, I am not right. "A" typically works like that, so I |
Can we fix A? |
If it is not really used much, I guess it is an option. It is |
Well, "A" typically means "any layout", so I'm not sure why changing it to "any contiguous layout" in a single API would make things easier for the user. |
(I certainly wouldn't guess that calling |
Another option would be to add another order value, like |
None of the options sound very convincing to me on first sight :(. "A" stands for any, that is true, but it seems (almost?) only used in the "fortran if not C (and actually fortran)" sense, though sometimes in terms of iteration order and not memory layout (we have "K" as a weaker "A"). "ForC" or variations of this, may make some sense since we use it in |
This is looking like a reasonable addition. Does anyone object? @pitrou Needs mention in 1.12.0 release notes. |
I'm still -0 I guess since it isn't clear to me how this could have broad utility for multiple users (what is the use case, BTW?), and because it isn't clear that adding a new function is better than adding a new option to |
The use case we have in mind is writing wrappers for third-party libraries such as BLAS, which often accept either C- or F-ordered arrays, but not non-contiguous arrays. |
☔ The latest upstream changes (presumably #7539) made this pull request unmergeable. Please resolve the merge conflicts. |
I think the name conflict with |
A better way to add the desired functionality would be via adding a kwarg to |
Good point, giving that an order keyword argument should be pretty uncontroversial. @pitrou maybe that would be a solution to continue here? |
Numpy has np.ascontiguousarray() and np.asfortranarray(), but doesn't have
a function to return an array of either contiguity depending on the input,
making a copy only when necessary.