[Numpy-discussion] difference numpy/matlab

Stuart Brorson sdb@cloud9....
Tue Jan 29 10:52:23 CST 2008


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.html

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/
>


More information about the Numpy-discussion mailing list