[Numpy-discussion] difference numpy/matlab

Robert Kern robert.kern at gmail.com
Tue Jan 29 12:55:16 EST 2008


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



More information about the NumPy-Discussion mailing list