Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ENH: need matlab compatible min and max for complex numbers #8151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jakirkham opened this issue Oct 12, 2016 · 16 comments
Open

ENH: need matlab compatible min and max for complex numbers #8151

jakirkham opened this issue Oct 12, 2016 · 16 comments

Comments

@jakirkham
Copy link
Contributor

If I take the minimum or maximum of complex numbers, I would expect that the magnitude is used. However, I find the real component is used instead. Here is a simple example. My expectation would be max and min are reversed in this case.

In [1]: import numpy

In [2]: a = numpy.array([1+1j, 1.2])

In [3]: numpy.abs(a)
Out[3]: array([ 1.41421356,  1.2       ])

In [4]: numpy.real(a)
Out[4]: array([ 1. ,  1.2])

In [5]: numpy.min(a)
Out[5]: (1+1j)

In [6]: numpy.max(a)
Out[6]: (1.2+0j)
@pv
Copy link
Member

pv commented Oct 12, 2016

It's lexicographic order, similarly as in sort(). I'm not sure I agree absolute value would be less surprising.

@mhvk
Copy link
Contributor

mhvk commented Oct 12, 2016

From http://math.stackexchange.com/questions/310931/comparing-complex-numbers#310941, I think this boils down to asking whether complex numbers can be ordered at all, with the answer being that, no, generally they cannot. So, the logical consequence would be not to allow finding a minimum at all. Indeed, this is the case in python proper:

min(1+1j, 1+2j)
TypeError: unorderable types: complex() < complex()

Indeed, comparison itself is not defined in python, but defined in numpy:

1+1j < 1+2j
# TypeError: unorderable types: complex() < complex()
np.array(1+1j) < np.array(1+2j)
# True

Anyway, not something one can change lightly.

@ahaldane
Copy link
Member

For what it's worth, Matlab uses the magnitude here. Pure-python and julia raise errors.

@pv
Copy link
Member

pv commented Oct 13, 2016 via email

@pv
Copy link
Member

pv commented Oct 13, 2016 via email

@charris charris closed this as completed Oct 13, 2016
@charris
Copy link
Member

charris commented Oct 13, 2016

Yep, the sort order is lexicographic.

@jakirkham
Copy link
Contributor Author

So my follow-up question was could we have min and max functions (or arguments to such functions that match the MATLAB behavior. As someone who works with scientist that are use to MATLAB, it is hard to sell them on Python when they find expectations like these are not met. Carrying around utility function like this on my end becomes silly and often redundant. Would it be possible to have some like this in numpy proper? I'm happy to ask this in a new issue if you would rather.

@charris
Copy link
Member

charris commented Oct 13, 2016

I don't think it would be too difficult to make such functions, but with different names than max/min. A PR would be welcome.

@jakirkham
Copy link
Contributor Author

Ok, could I ask that we reopen this issue so that we can keep track of it? I don't have time for such a PR now, but would be willing to do it in the near future.

@charris charris changed the title min and max surprising on complex numbers ENH: need matlab compatible min and max for complex numbers Oct 28, 2016
@charris charris reopened this Oct 28, 2016
@charris
Copy link
Member

charris commented Oct 28, 2016

OK, I reopened with a Wish List label.

@hmaarrfk
Copy link
Contributor

I don't know if there is still interest here. But I would be interested in such a function too.

Would it be called: np.max_abs?

Application note: In optics, it is sometimes useful to find the point of maximum intensity, but to propagate your field as a complex number.

@charris
Copy link
Member

charris commented Sep 20, 2018

Signed ints are a bit of a problem for abs, as the most negative values are invariant. If we stick to inexact types, no problem. Maybe always return an unsigned type, but then there are unexpected promotions. Maybe just warn when problem encountered?

@hmaarrfk
Copy link
Contributor

For integers, maybe it should be implemented as minmax? The the check for the most negative value becomes order 1

@charris
Copy link
Member

charris commented Sep 20, 2018

Not sure we are on the same wavelength here. The problem is that that signed twos complement integers have more negative values than positive ones, so there is no way to represent the absolute value of the most negative value without changing types.

In [1]: abs(np.array(-128, np.int8))
Out[1]: -128

So maxabs would work, but miss 128, as does abs.

Hmm, I suppose that wouldn't be so bad if it is documented, might even be useful as the result will always be positive.

@hmaarrfk
Copy link
Contributor

I get it. but max_abs could do something like
pseudocode

if dtype == signed int
    min, max = np.minmax(a)
    if min == -128
         warn
    else
         return maximum(abs(min), abs(max))

@leofang
Copy link
Contributor

leofang commented Sep 28, 2019

related: #2004

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants