Skip to content

Comparison ops for Complex Tensors #36444

@anjali411

Description

@anjali411

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
  • 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
  • Absolute value comparison
    • Pros:
      • Some applications naturally compare complex numbers using their absolute values
    • Cons:
      • Divergent from NumPy, possibly a silent break

cc. @rgommers @dylanbespalko @ezyang @mruberry
cc @ezyang @anjali411 @dylanbespalko

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: complexRelated to complex number support in PyTorchmodule: numpyRelated to numpy support, and also numpy compatibility of our operatorstriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions