Skip to content

ENH: np.angle: Remove unnecessary multiplication, and allow subclasses to pass through #11637

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

Merged
merged 5 commits into from
Jul 31, 2018

Conversation

eric-wieser
Copy link
Member

Copy link
Contributor

@tylerjereddy tylerjereddy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a net gain in efficiency for angle. One suggestion might be a unit test that ensures that when z is a subclass of ndarray (i.e., np.matrix) that it remains as such after angle is called on it. Maybe overkill, but nice to prevent regression after this improvement.

z = asarray(z)
if (issubclass(z.dtype.type, _nx.complexfloating)):
z = asanyarray(z)
if issubclass(z.dtype.type, _nx.complexfloating):
zimag = z.imag
zreal = z.real
else:
zimag = 0
zreal = z
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps worth noting that the code path where z isn't a subclass of complexfloating doesn't have test coverage, although that's probably not an issue with changes made here

@tylerjereddy
Copy link
Contributor

Actually, not sure you could easily test for that -- even the returned object is described as ndarray or scalar--is there a case where the subclass passthrough actually provides enhanced functionality?

@eric-wieser
Copy link
Member Author

Test added for the parts that changed in this patch.

Copy link
Contributor

@tylerjereddy tylerjereddy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks solid now. Would you say it is pretty unlikely that users depended on angle returning the ndarray base class only beforehand?

Not sure if the docstring for Returns should get a small update to reflect the increased flexibility in return type, perhaps in another small future PR.

@eric-wieser
Copy link
Member Author

Added a comment about that


a = arctan2(zimag, zreal)
if deg:
a *= 180/pi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe np.rad2deg? Probably no faster.

@charris charris merged commit e30cc8e into numpy:master Jul 31, 2018
@charris
Copy link
Member

charris commented Jul 31, 2018

Thanks Eric.

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

Successfully merging this pull request may close these issues.

3 participants