question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can we have short-circuit equivalents to `allclose`, `array_equal`, `isfinite` and the like in numpy?

See original GitHub issue

I did some (admittedly very dirty) benchmark of scipy.linalg.cho_solve(), and what surprised me was the magnitude of overhead due to check_finite=True, which is on by default. My notes can be read here.

The finiteness-check is useful, if not plain necessary for some scenarios, but can we do better? In many user cases such as the scipy.linalg functions, all that matters is that some element of an array is NaN or infinity. Now, what it has to do, is to call numpy.asarray_chkfinite, which in turn does something amounting to numpy.isfinite(a).all(). But this does not short-circuit; isfinite() checks each single element and returns a Boolean array. This whole test can be expensive, but can it be made less expensive in the best case?

This issue is also present in the test of closeness and equality, in core/numeric.py. Neither allclose() nor array_equal() actually short-circuits when doing the real check. They only short-circuit in the all() function/method call, which is already too late. These two functions can be especially deceiving. Spoiled by Python’s built-in all(), the user may think these functions do the short-circuit evaluation. The docs never mention they don’t.

So the request is… Do you think it’s worthwhile to add some short-circuit Boolean functions for the above tests (finiteness/equality/closeness)?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
sebergcommented, Feb 24, 2021

I am going to close this. There are two possibilities:

  1. Specialized gufuncs for performance optimiation to replace some chained operation: gh-18483
  2. A chaining operation map_reduce like addition to the ufunc machinery (or addition build on top of it): gh-11622 (and other issues like it)

Both probably make sense for certain functions.

0reactions
jakirkhamcommented, Jul 26, 2018

So it looks like PR ( https://github.com/numpy/numpy/pull/6980 ) went in, but was then reverted. Unclear as to whether it was readded later or not. Does anyone know?

Read more comments on GitHub >

github_iconTop Results From Across the Web

numpy.allclose — NumPy v1.24 Manual
Returns True if two arrays are element-wise equal within a tolerance. The tolerance values are positive, typically very small numbers. The relative difference...
Read more >
What is the equivalent of numpy.allclose for ... - Stack Overflow
In short a.astype(float) should work with your arrays. None of this works with the compound dtype of a structured array. You could though ......
Read more >
NumPy equality testing: multiple ways to compare arrays
In this video I describe multiple ways how one can compare NumPy arrays and check their equality.00:00​​ Intro00:29​​ Example start00:47​ ...
Read more >
[Numpy-discussion] all elements equal - Google Groups
it finds an element that is not equal to it. And you could check for closeness. Or: I[8] np.allclose(a, a[0]) O[8] False I[9]...
Read more >
numpy.allclose() in Python - GeeksforGeeks
numpy.allclose() function is used to find if two arrays are element-wise equal within a tolerance. The tolerance values are positive, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found