-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Description
This issue is intended to roll-up several conversations about how PyTorch should compare complex values and how functions that logically rely on comparisons, like min, max, sort, and clamp, should work when given complex inputs. See #36374, which discussed complex min and max, and #33568, which discussed complex clamp. The challenge of comparing complex numbers is not limited to PyTorch, either, see numpy/numpy#15630 for NumPy's discussion of complex clip.
Comparing complex numbers is challenging because the complex numbers aren't part of any ordered field. In NumPy, they're typically compared lexicographically: comparing the real part and only comparing the imaginary part if the real parts are equal. C++ and Python, on the other hand, do not support comparison ops on complex numbers.
Let's use this issue to enumerate complex comparison options as well as their pros and cons.
The current options are:
- No default complex comparison
- Pros:
- Consistent with C++ and Python
- Behavior is always clear since the user must specify the type of comparison
- Cons:
- Divergent from NumPy, but a clear error
- Possibly inconvenient to always specify the comparison
- Pros:
- Lexicographic comparison
- Pros:
- Consistent with NumPy
- Cons:
- Clamp (clip) behavior seems strange: (3 - 100j) clamped below to (2 + 5j) is unchanged, clamped above to (2 + 5j) becomes (2 + 5j)
- Some users report wanting to compare complex by absolute value
- Pros:
- Absolute value comparison
- Pros:
- Some applications naturally compare complex numbers using their absolute values
- Cons:
- Divergent from NumPy, possibly a silent break
- Pros:
cc. @rgommers @dylanbespalko @ezyang @mruberry
cc @ezyang @anjali411 @dylanbespalko