-
Notifications
You must be signed in to change notification settings - Fork 307
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
add mapv_into_any(), resolves #1031 #1040
Conversation
Thank you for granting permission to run git workflows on this pull request. It looks like clippy on the beta channel rejects a large portion of the ndarray codebase, but it appears that this pull request has passed all checks. |
It just looks like clippy found new lints in a new release |
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.
Looks solid to me, should be useful
Please use issue closing keywords in the pr description so that the linked issue is closed when this is merged https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue |
Fixed commit comment. Edit: Looks like you'll still need to manually link to issue #1031, sorry. I don't appear to have permission. |
FWIW, we already use this kind of ad-hoc specialization in ndarray with the linalg code, so it feels familiar to us. |
(milestone - not known if we will have 0.15.4 or 0.16 atm). Thanks! |
First attempt at pull request for #1031.
Since Rust does not yet have specialization, mapping between generic numerical types in ndarray is challenging when the types might be different. For example, we might want to perform on operation on an array of real numbers to get an array that might be real or might be complex. We can do this now with
ArrayBase::mapv()
, but if it turns out that both the input and output are the same type (e.g. real numbers) thenmapv()
will perform unnecessary allocation of a new array compared tomapv_into()
.This PR proposes a generic method
ArrayBase::mapv_into_any()
whose signature accepts a closure mapping between different typesFnMut(A) -> B
but which uses memory-efficientmapv_into()
whenA
andB
are the same type. The type comparison is performed at runtime. This is the most memory-efficient way to map between arrays of possibly different types using stable Rust.The use of
unsafe
is discussed on the Rust language forum.