[Numpy-discussion] making two 2D real arrays into one complex array

Robert Kern robert.kern@gmail....
Tue Apr 10 15:09:00 CDT 2007


mark wrote:
> Hello List -
> 
> Any reason why the following doesn't work?
> 
>>>> x,y = meshgrid(linspace(-1,1,10),linspace(-1,1,10))
>>>> z = complex(x,y)
> Traceback (most recent call last):
>   File "<pyshell#225>", line 1, in ?
>     z = complex(x,y)
> TypeError: only length-1 arrays can be converted to Python scalars
> 
> 
> I know I can make an empty complex z array with the size of x, then
> set z.real equal to x and z.imag equal to y. But is there any reason
> why the above shouldn't work?

Yes. complex() is a builtin function for Python. It only takes Python ints and
floats. It knows nothing about numpy.

> It would be easy to implement in numpy,
> wouldn't it? If so, I would *really* like it.

I don't think there's much need for it to be provided in numpy. It's trivial
enough that you can define it yourself wherever you need it.

def complexarray(x, y):
    return x + 1j*y

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