-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
MAINT: improve error message for isposinf and isneginf on complex values #11503
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
ab2bf66
to
fedb233
Compare
I'm not sure if these functions should be extended to complex dtypes.
Positive and negative infinity don't really exist in the C99 complex
number model that Numpy follows, in which there's only one complex
infinity.
|
Interesting. We should at least raise an error that makes it clear this is by design. @jor- how did you come across this code path, was it part of NumPy, an external library, or just exploring the limits of NumPy's complex handling? |
Positive and negative infinity can be values in complex NumPy arrays so I think it makes sense that isposinf and isneginf work also for complex arrays. @mattip: You mean how I found the bug? I use NumPy in one of my libraries to handle complex valued arrays. These arrays might also contain positive and negative infinity which need to be handled separately in my library. |
I think this model only makes sense if you provide the https://www.ideone.com/IpsC42 Perhaps that has a place in numpy? |
The thing about there being only one complex infinity is that e.g. the
arithmetic just assumes it:
```
>> a = 1.0; b = 0.0
>> np.float_(a) / np.float_(b)
inf
>> np.complex_(a) / np.complex_(b)
(inf+nanj)
>> np.complex_(np.inf) * np.complex_(np.inf)
(inf+nanj)
```
For the first one, isposinf would give True, but for the second it
would give False. Also, `isposinf(a) and isposinf(b)` would not imply
`isposinf(a*b)`. This stuff is iirc according to spec, so it's not a
bug.
|
In my opinion, inf is infinity and inf+nanj is not infinity, so That |
I don't think +/- infinity makes sense for complex values. |
Unfortunately, your opinion carries less weight than the C99 standard, which seems to disagree with you
Or at least, it dictates that |
Okay, I did not know about that. Thank you for pointing this out. You have convinced me. With the one infinity model for complex values, the functions What originally brought me to this problem was, that |
So the conclusion is that calling |
I changed the code so that an exception is raised now if The failures in the tests are due to connection errors. Can I restart the failing tests somehow? |
numpy/lib/ufunclike.py
Outdated
scalar input, or if first and second arguments have different shapes. | ||
Errors result if the second argument is also supplied when x is a scalar | ||
input, if first and second arguments have different shapes, or if the | ||
first argument as complex values |
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 -> has, here and in the second docstring
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.
changed
Tests that reach the exceptions are missing, you can use |
…lex values whether exception is raised
Codecov Report
@@ Coverage Diff @@
## master #11503 +/- ##
==========================================
+ Coverage 85.7% 85.7% +<.01%
==========================================
Files 327 327
Lines 82002 82018 +16
==========================================
+ Hits 70281 70297 +16
Misses 11721 11721
Continue to review full report at Codecov.
|
I added a test. The tests passed. Only one failed due to a failure at Travis. |
restarted the failing test, now all tests are green. |
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.
Can you justify choosing valueerror instead of typeerror?
The asanyarray breaks this function on duck-types... |
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.
To avoid the duck-type issue with np.asanyarray
and also ensure the functions do not become slower for the normal case where the inputs are not complex, I'd suggest using a try/except
, catching the exception raised by signbit
and then raising a more informative error message.
numpy/lib/ufunclike.py
Outdated
@@ -138,6 +139,10 @@ def isposinf(x, out=None): | |||
array([0, 0, 1]) | |||
|
|||
""" | |||
x = nx.asanyarray(x) |
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 @eric-wieser notes, this leads to a change of behaviour for objects that implement __array_ufunc__
but cannot be converted to arrays.
p.s. I changed the title of this PR to reflect the consensus that arose that these functions should not work on complex values. p.p.s. I think this is a very nice example of how review makes things better! Thanks for starting it, @jor-! |
…for complex values
… converted to arrays
@mhvk: I now use @eric-wieser: I changed the error raised to |
Thanks @jor- |
Thanks for the reviewing. |
Fixes a bug in
isposinf
andisneginf
for complex values (#11438) and adds additional tests for these functions with complex values.