[SciPy-user] [newb] how to create arrays

Robert Kern robert.kern@gmail....
Thu Jan 3 06:08:19 CST 2008

Neal Becker wrote:
> Robert Kern wrote:
>> Anne Archibald wrote:
>>> On 02/01/2008, Neal Becker <ndbecker2@gmail.com> wrote:
>>>> How would I create a vector of complex random variables?
>>>> I'm thinking the best way is to create a complex vector, then
>>>> assign to the real and imag parts (using e.g., random.standard_normal).
>>>> I don't see any way to create an uninitialized array.  I guess I'd have
>>>> to
>>>> use zeros?  Is there any way to avoid the wasted time of initializing
>>>> just to write over it?
>>> You can create an uninitialized array if you want to. But, from your
>>> question, you may well be thinking about your problem in the wrong
>>> way. If all you want to do is store a whole bunch of values you
>>> compute in a loop, use a python list.
>> In this case, he wouldn't be using a loop.
>>   from numpy import *
>>   z = empty([10], dtype=complex)
>>   z.real[:] = random.standard_normal(10)
>>   z.imag[:] = random.standard_normal(10)
> This is what I had in mind.
> I guess there isn't something that could do this in a single step, something
> like:
> z = random.standard_normal(10) + i * random.standard_normal(10)


  z = random.standard_normal(10) + 1j * random.standard_normal(10)

There's a minor tradeoff wrt the temporary; if your arrays are large enough for
you to be concerned about the overhead of zeros() over empty(), you might be
concerned about it as well.

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 SciPy-user mailing list