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.

NumPy should warn (eventually raise an error?) on comparisons between complex numbers

See original GitHub issue

Python (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:open
  • Created 3 years ago
  • Reactions:5
  • Comments:14 (13 by maintainers)

github_iconTop GitHub Comments

4reactions
shoyercommented, Apr 15, 2020

I could also imagine np.sort(c_arr, key=(c_arr.real, c_arr.imag)) as sugar for np.take_along_axis(c_arr, np.lexsort((c_arr.real, c_arr.imag), axis), axis)

1reaction
shoyercommented, Apr 15, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

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