In my opinion, with the caveat that anyone that asks for the sign of a complex number gets what they deserve, this seems about as useful a definition as any.

On Fri, Dec 22, 2023 at 8:23 AM <mhvk@astro.utoronto.ca> wrote:
Hi All,

A long-standing, small wart in numpy has been that the definition of sign for complex numbers is really useless (`np.sign(z)` gives the sign of the real component, unless that is zero, in which case it gives the sign of the imaginary component, in both cases as a complex number with zero imaginary part). Useless enough, in fact, that in the Array API it is suggested [1] that sign should return `z / |z|` (a definition consistent with those of reals, giving the direction in the complex plane).

The question then becomes what to do. My suggestion - see https://github.com/numpy/numpy/pull/25441 - is to adapt the Array API definition for numpy 2.0, with the logic that if we don't change it in 2.0, when will we?

Implementing it, I found no real test failures except one for `np.geomspace`, where it turned out that to correct the failure, the new definition substantially simplified the implementation.

Furthermore, with the redefinition, it has become possible to extend ``np.copysign(x1, x2)`` to complex numbers, since it can now generally return ``|x1| * sign(x2)`` with the sign as defined above (with no special treatment for zero).

Anyway, to me the main question would be whether this would break any workflows (though it is hard to see how it could, given that the previous definition was really rather useless...).

Thanks,
https://github.com/data-apis/array-api/pull/556,
Marten

[1] https://data-apis.org/array-api/latest/API_specification/generated/array_api.sign.html (and https://github.com/data-apis/array-api/pull/556, which has links to previous discussion)
_______________________________________________
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-leave@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: ndbecker2@gmail.com


--
Those who don't understand recursion are doomed to repeat it