add mapv_into_any(), resolves #1031 #1040
Merged
+53
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.