[Numpy-discussion] pre-initialized arrays
Stuart Brorson
sdb@cloud9....
Fri Jan 4 16:10:16 CST 2008
>> I realize NumPy != Matlab, but I'd wager that most users
>> would think that this is the natural behavior.
>
> I would not find it "natural" that elements of my float
> array could be assigned complex values.
OK, then NumPy should throw an exception if you try to make the
assignemnt.
I tried it out. NumPy does the right thing in this case:
In [10]: A = numpy.zeros([3, 3])
In [11]: A[1, 1] = 1+2j
---------------------------------------------------------------------------
<type 'exceptions.TypeError'> Traceback (most recent call
last)
/fs/home/sdb/<ipython console> in <module>()
<type 'exceptions.TypeError'>: can't convert complex to float; use
abs(z)
However, not in this case:
In [12]: B = 10*numpy.random.rand(3, 3) + 1j*numpy.random.rand(3, 3)
In [13]: B[1, 1]
Out[13]: (7.15013107181+0.383220559014j)
In [14]: A[1, 1] = B[1, 1]
In [15]:
So the bug is that assignment doesn't do value checking in every
case.
> How could it be
> a fixed chunk of memory and do such things, unless it would
> always waste enough memory to hold the biggest possible
> subsequent data type?
Fair question. Matlab does a realloc if you assign complex values to
an initially real array. It can take a long time if your matrices are
large.
Cheers,
Stuart Brorson
Interactive Supercomputing, inc.
135 Beaver Street | Waltham | MA | 02452 | USA
http://www.interactivesupercomputing.com/
More information about the Numpy-discussion
mailing list