BUG: fix linspace returning NaN for equal infinite endpoints (gh-26699)#31620
Conversation
MaanasArora
left a comment
There was a problem hiding this comment.
Thanks! This solves the specific issue, but I'm wondering about the performance cost where this new code is not invoked. I'm not sure if the corner case is bad enough to need this (and a lerp ufunc would be much better as stated in the issue!)
| y /= div | ||
| if _mult_inplace: | ||
| y *= delta | ||
| with np.errstate(invalid='ignore'): |
There was a problem hiding this comment.
I think the RuntimeWarning which happens here with start=stop=inf is real, so maybe this isn't needed (even if we fix the actual output), or we should make it more focused to the error. In any case, we don't want to ignore errors for all the lines below!
There was a problem hiding this comment.
any(isnan) short-circuits on the first element so for the common finite-scalar path the overhead is negligible. I agree a lerp ufunc is the right long-term fix but this is the minimum correct approach at the py level for now
errstate now wraps only the np.subtract call and all downstream arithmetic is back to normal, whoops
There was a problem hiding this comment.
This seems like an antipattern to me. I am not sure it is threadsafe. So far we only use with errstate in helper functions like isclose and printing, I am not sure we want to start using it in core functions. Can we do the more specific if np.isinf() instead?
There was a problem hiding this comment.
This seems like an antipattern to me. I am not sure it is threadsafe. So far we only use
with errstatein helper functions likeiscloseand printing, I am not sure we want to start using it in core functions. Can we do the more specificif np.isinf()instead?
thank you! I changed it accordingly
1f3faaa to
877ea76
Compare
|
I noticed that the existing test for edit: also added complex infinity tests |
877ea76 to
bca1f4f
Compare
|
removed the extra errstate wrappers, linspace(inf, -inf, N) now warns from the multiply and add as it should. also dropped the isnan guard since where(start == stop, 0, delta) already returns delta unchanged when there's no NaN to fix because ig the check was redundant |
bca1f4f to
01169e2
Compare
01169e2 to
ec573e7
Compare
|
@MaanasArora :) |
|
Thanks, this seems to have the least performance overhead now, but I'll still defer to someone more familiar with this area to evaluate if this should go in (not very familiar with this code, sorry!) |
ec573e7 to
4d6c7be
Compare
|
Thanks @Ijtihed |
PR summary
Fixes gh-26699.
np.linspace(np.inf, np.inf, N) returned [nan nan ... nan inf] instead of [inf inf ... inf]. The root cause is delta = stop - start producing nan via inf - inf which propagates through all subsequent multiplications,only the last element was rescued by the y[-1] = stop assignment
Fix: replace any nan in delta with zero wherever start == stop. The nan == nan is False identity ensures NaN inputs are unaffected in this case
AI Disclosure
Used Claude to make sure I didn't miss anything