[Numpy-discussion] Complex arange

Sturla Molden sturla@molden...
Fri Feb 2 10:05:09 CST 2007


I think arange for complex numbers should work like meshgrid,
with the real and imaginary axis replacing the x and y axis. That would
mean something like this:

def complex_arange(start,end,stride):

   def iscomplex(x):
      if ((type(x)==complex) or (type(x)==complex64)
         or (type(x)==complex128)):
         return True
      else:
         return False

   if iscomplex(start) or iscomplex(end) or iscomplex(stride):
      start = complex(start)
      end = complex(end)
      stride = complex(stride)
      ar = arange(start.real, end.real, stride.real)
      ai = arange(start.imag ,end.imag, stride.imag)
      rr,ri = meshgrid(ar,ai)
      tmp = rr + 1j*ri
      if tmp.shape[0] == 1 or tmp.shape[1] == 1: tmp = tmp.flatten()
      return tmp
   else:
      return arange(start,end,stride)


I think this is a reasonable extension of arange to complex numbers. Here
complex_arange(1j, 5j, 1) throws a ZeroDivisionError as the stride for the
imaginary part i 0. Observe that complex_arange(1j, 5j, 1j) throws an
exception as well, as the extent of the real part is arange(0,0,0). But
complex_arange(0+1j,1+5j,1+1j) does exist, and so does
complex_arange(0+1j,0+5j,1+1j). But in the case of
complex_arange(0+1j,0+5j,1+1j) the return value is an empty array, as the
extent along the real axis is 0.

Regards,

Sturla Molden




More information about the Numpy-discussion mailing list