Skip to content

__lt__ and arange for complex numbers #11505

@crusaderky

Description

@crusaderky

I tried to find any kind of documentation on how numpy implements inequality comparison between complex numbers, but I found none.

It seems like __lt__ is implemented as (lhs.real, lhs.imag) < (rhs.real, rhs.imag), or in other words

if lhs.real < rhs.real:
    return True
if lhs.real > rhs.real:
    return False
return lhs.imag < rhs.imag

Tests that brought me to this conclusion:

>>> numpy.array(9+999j) < numpy.array(10+10j)
True
>>> numpy.array(11+1j) < numpy.array(10+10j)
False
>>> numpy.array(10+1j) < numpy.array(10+10j)
True
>>> numpy.array(10+11j) < numpy.array(10+10j)
False

Is this correct? If so it would at the bare minimum require some formal documentation, as another perfectly valid implementation could have been abs(lhs) < abs(rhs).

Also, arange behaves weirdly and I can't put any sense in it. I would have expected it to be coherent with __lt__, that is

i = start
while (i < stop if step > 0 else i > stop):
    yield i
    i += step

but it isn't:

>>> numpy.arange(0+1j, 4+1j, 1)
array([], dtype=complex128)
>>> numpy.arange(0+1j, 4+4j, 1)
array([0.+1.j, 1.+1.j, 2.+1.j])
>>> numpy.arange(0, 4j, 1j)
array([], dtype=complex128)
>>> numpy.arange(0, 8+4j, 1)
array([0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j])

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions