data-apis / array-api

RFC document, tooling and other content related to the array API standard
https://data-apis.github.io/array-api/latest/
MIT License
205 stars 42 forks source link

Wording for comparison operators implies complex number support for inequality operators #737

Closed hameerabbasi closed 5 months ago

hameerabbasi commented 5 months ago

In https://data-apis.org/array-api/latest/API_specification/array_object.html#comparison-operators, it is stated:

Comparison operators should be defined for arrays having any data type.

Is that right? I don't think operator.{lt, gt, le, ge} should be defined for anything but real-valued datatypes. In particular, defining them for bool is weird and for complex* makes less sense (see, e.g., https://math.stackexchange.com/a/311341).

In addition, numpy.array_api doesn't support this either:

>>> xp.asarray(5 + 5j) < xp.asarray(-5 + 5j) 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/habbasi/miniforge3/envs/sparse-dev/lib/python3.11/site-packages/numpy/array_api/_array_object.py", line 631, in __lt__
    other = self._check_allowed_dtypes(other, "real numeric", "__lt__")
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/habbasi/miniforge3/envs/sparse-dev/lib/python3.11/site-packages/numpy/array_api/_array_object.py", line 142, in _check_allowed_dtypes
    raise TypeError(f"Only {dtype_category} dtypes are allowed in {op}")
TypeError: Only real numeric dtypes are allowed in __lt__

Note also that other operators state the datatypes for which there's actually a valid implementation.