-
Notifications
You must be signed in to change notification settings - Fork 53
Add complex number support to astype
#445
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
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.
As long as we don't raise the annoying warning when casting complex to real, I am fine with merging this PR.
@leofang Agreed. I don't think there is any appetite for requiring implementations to display a warning (ala NumPy). |
Was about to say this should raise a warning 😂 That said, I kind of wonder if this is a good idea as opposed to simply forcing users to do What about saying |
Based on discussion in today's meeting it sounds like a warning/error is inconsistent with the implied |
Quoting myself from #463 (comment):
|
This PR has now been updated to reflect the current workgroup consensus to disallow casting from a complex floating-point dtype to a real-valued dtype. |
As discussed in the 3 November 2022 meeting, no objections were raised. This PR should be ready for merge. @leofang would you mind taking one last look and then approving? |
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.
LGTM, seems ready to go in. @leofang I'll give it a few days in case you want to have another look
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.
Sorry I dropped the ball, LGTM!
Thanks, @leofang! In it goes! |
This PR:
adds complex number support to
astype
. Asastype
generally supports copying an array to any specified data type irrespective of type promotion rules, no exception to general behavior is made for complex number arrays.Accordingly, following C, NumPy, and others, when casting a complex floating-point array to a real-valued data type, the imaginary component must be discarded and only the real component cast to the desired data type.
Notes
real(x)
before callingastype
. However, asastype
is more generally used to force a type conversion, prohibiting casting complex-valued arrays to real-valued arrays did not seem ideal, nor consistent with, say, casting a floating-point array to an integral array (i.e., we allow casting a floating-point array to an integral array and don't requireastype(floor(x))
). Hence, this PR chooses to follow existing precedent and require that the imaginary component be discarded when performing the conversion.Updates