NumPy should warn (eventually raise an error?) on comparisons between complex numbers
See original GitHub issuePython (version 3, at least) raises an error, but NumPy uses lexicographic order (see https://github.com/numpy/numpy/issues/11505 for discussion).
We should clean this up to match Python. This behavior is fine for sorting, but I don’t think it makes sense for comparisons between numbers. I think most comparisons between complex numbers are probably latent bugs (as was the case where I discovered this).
My suggestion would be to start by issuing a DeprecationWarning, and then maybe make it a true error at some point in the distance future.
Reproducing code example:
>>> x = 1 + 1j
>>> y = 2 + 1j
>>> x < y
TypeError: '<' not supported between instances of 'complex' and 'complex'
>>> np.array(x) < np.array(y)
True
Numpy/Python version information:
1.18.2 3.6.9 (default, Nov 7 2019, 10:44:02) [GCC 8.3.0]
Issue Analytics
- State:
- Created 3 years ago
- Reactions:5
- Comments:14 (13 by maintainers)
Top Results From Across the Web
FutureWarning: elementwise comparison failed; returning ...
There is a disagreement between Numpy and native python on what should happen when you compare a strings to numpy's numeric types.
Read more >NumPy 1.21.0 Release Notes
Since comparisons normally only return boolean arrays, providing any other dtype will always raise an error in the future and give a DeprecationWarning...
Read more >10. Release Notes — Numba 0.26.0 documentation
This will be the last version to support Python 2.6, Python 3.3 and Numpy 1.6. ... let “division by zero” and other math...
Read more >utils.py
Return number of jiffies (1/100ths of a second) that this process has been ... from numpy.lib import iscomplexobj, real, imag # Handle complex...
Read more >NumPy Tutorial: Your First Steps Into Data Science in Python
To get the most out of this NumPy tutorial, you should be familiar with writing ... differences from a basic Python REPL, including...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I could also imagine
np.sort(c_arr, key=(c_arr.real, c_arr.imag))
as sugar fornp.take_along_axis(c_arr, np.lexsort((c_arr.real, c_arr.imag), axis), axis)
I think it would also be fair to require setting a special option in
np.sort
to handle complex values, but I can imagine that might get more push-back.