I noticed that: min([1+1j,-1+3j]) gives 1+1j in matlab (where for complex, min(abs) is used) but gives -1+3j in numpy (where lexicographic order is used) shouldn't this be mentioned somewhere in "Numpy for Matlab users" webpage? -- Lorenzo Bolla lbolla@gmail.com http://lorenzobolla.emurse.com/
lorenzo bolla wrote:
I noticed that:
min([1+1j,-1+3j])
gives 1+1j in matlab (where for complex, min(abs) is used) but gives -1+3j in numpy (where lexicographic order is used)
shouldn't this be mentioned somewhere in "Numpy for Matlab users" webpage?
It should be stated that they're both wrong.
I'd rather say "arbitrary". On 1/29/08, Neal Becker <ndbecker2@gmail.com> wrote:
lorenzo bolla wrote:
I noticed that:
min([1+1j,-1+3j])
gives 1+1j in matlab (where for complex, min(abs) is used) but gives -1+3j in numpy (where lexicographic order is used)
shouldn't this be mentioned somewhere in "Numpy for Matlab users" webpage?
It should be stated that they're both wrong.
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
-- Lorenzo Bolla lbolla@gmail.com http://lorenzobolla.emurse.com/
I have to agree with Lorenzo. There is no natural ordering of the complex numbers. Any way you order them is arbitrary. Accepting this, the question then becomes "what should NumPy do when the user tries to do order comparison operations on complex numbers. The problem is that NumPy is schizophrenic. Watch this: -------------------------- <session log> --------------------- In [20]: A = numpy.array([3+1j, 1+3j, -1-3j, -1+3j, -3-1j]) In [21]: B = A[numpy.random.permutation(5)] In [22]: In [22]: A Out[22]: array([ 3.+1.j, 1.+3.j, -1.-3.j, -1.+3.j, -3.-1.j]) In [23]: B Out[23]: array([-1.+3.j, 3.+1.j, -1.-3.j, 1.+3.j, -3.-1.j]) In [24]: numpy.greater(A, B) Out[24]: array([ True, False, False, False, False], dtype=bool) In [25]: numpy.maximum(A, B) Out[25]: array([ 3.+1.j, 3.+1.j, -1.-3.j, 1.+3.j, -3.-1.j]) In [26]: In [26]: 3+1j > -1+3j --------------------------------------------------------------------------- <type 'exceptions.TypeError'> Traceback (most recent call last) /tmp/test/web/<ipython console> in <module>() <type 'exceptions.TypeError'>: no ordering relation is defined for complex numbers ---------------------------- </session log> ---------------------- I can compare complex numbers living in arrays using vectorized functions. However, doing comparison ops on the same complex numbers individually throws an exception. I raised this question some months ago in this thread: http://projects.scipy.org/pipermail/numpy-discussion/2007-September/029289.h... Although I was initially confused about what NumPy was supposed to do, I eventually convinced myself that NumPy has contradictory behaviors depending upon exactly which comparison operation you were doing. Personally, I think NumPy should throw an exception whenever the user tries to do a greater-than or less-than type operation involving complex numbers. This would force the user to explicitly take the magnitude or the real & imaginary part of his number before doing any comparisons, thereby eliminating any confusion due to ambiguity. Just my $0.02. Cheers, Stuart Brorson Interactive Supercomputing, inc. 135 Beaver Street | Waltham | MA | 02452 | USA http://www.interactivesupercomputing.com/ On Tue, 29 Jan 2008, lorenzo bolla wrote:
I'd rather say "arbitrary".
On 1/29/08, Neal Becker <ndbecker2@gmail.com> wrote:
lorenzo bolla wrote:
I noticed that:
min([1+1j,-1+3j])
gives 1+1j in matlab (where for complex, min(abs) is used) but gives -1+3j in numpy (where lexicographic order is used)
shouldn't this be mentioned somewhere in "Numpy for Matlab users" webpage?
It should be stated that they're both wrong.
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
-- Lorenzo Bolla lbolla@gmail.com http://lorenzobolla.emurse.com/
Stuart Brorson wrote:
I have to agree with Lorenzo. There is no natural ordering of the complex numbers. Any way you order them is arbitrary.
Accepting this, the question then becomes "what should NumPy do when the user tries to do order comparison operations on complex numbers. The problem is that NumPy is schizophrenic. Watch this:
-------------------------- <session log> ---------------------
In [20]: A = numpy.array([3+1j, 1+3j, -1-3j, -1+3j, -3-1j])
In [21]: B = A[numpy.random.permutation(5)]
In [22]:
In [22]: A Out[22]: array([ 3.+1.j, 1.+3.j, -1.-3.j, -1.+3.j, -3.-1.j])
In [23]: B Out[23]: array([-1.+3.j, 3.+1.j, -1.-3.j, 1.+3.j, -3.-1.j])
In [24]: numpy.greater(A, B) Out[24]: array([ True, False, False, False, False], dtype=bool)
In [25]: numpy.maximum(A, B) Out[25]: array([ 3.+1.j, 3.+1.j, -1.-3.j, 1.+3.j, -3.-1.j])
In [26]:
In [26]: 3+1j > -1+3j --------------------------------------------------------------------------- <type 'exceptions.TypeError'> Traceback (most recent call last)
/tmp/test/web/<ipython console> in <module>()
<type 'exceptions.TypeError'>: no ordering relation is defined for complex numbers
---------------------------- </session log> ----------------------
No, numpy is entirely consistent. In[26] has Python's complex numbers, not numpy's. In [1]: from numpy import complex64 In [2]: complex64(3+1j) > complex64(-1+3j) Out[2]: True -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
participants (4)
-
lorenzo bolla -
Neal Becker -
Robert Kern -
Stuart Brorson