[Numpy-discussion] numarray-->Numeric conversion?

Todd Miller jmiller at stsci.edu
Mon Sep 13 06:53:06 CDT 2004

On Mon, 2004-09-13 at 05:08, Francesc Alted wrote:
> Hi,
> I've been thinking in ways to convert from/to Numeric to numarray objects in
> a non-expensive way. For the Numeric --> numarray there is an very easy way
> to do that:
> In [45]: num=Numeric.arange(10, typecode="i")
> In [46]: na=numarray.array(buffer(num), typecode=num.typecode(),
>                            shape=num.shape)

One thing to note is that buffer() returns a readonly buffer object. 
There's a function,  numarray.memory.writeable_buffer(), which although
misspelled,  returns a read-write buffer.

> In [47]: na
> Out[47]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
> that creates a new numarray object without a memory copy
> In [48]: num[2]=3
> In [49]: num
> Out[49]: array([0, 1, 3, 3, 4, 5, 6, 7, 8, 9],'i')
> In [50]: na
> Out[50]: array([0, 1, 3, 3, 4, 5, 6, 7, 8, 9])
> i.e. both num (Numeric) and na (numarray) arrays shares the same memory. If
> you delete one reference:
> In [51]: del num
> the other is still accessible
> In [52]: na
> Out[52]: array([0, 1, 3, 3, 4, 5, 6, 7, 8, 9])
> However, it seems that it is not so easy to go the other way, that is
> convert a numarray object to a Numeric without a memory copy. It seems that
> Numeric.array constructor get the shape from the sequence object that is
> passed, while numarray approach seems more sofisticated in the sense that if
> it detects that the first argument is a buffer sequence, you must specify
> the shape on the constructor.
> Anyone knows if building a Numeric from a buffer (specifying both type and
> shape) is possible (I mean, at Python level) at all?. 

I don't see how to do it.   It seems like it would be easy to add a
frombuffer() function which sets a->base to the buffer object and
a->flags so that a->data isn't owned.  a->data would of course point to
the buffer data.  I think normally a->base points to another Numeric
array,  but it appears to me that it would still work.

I see two messy areas:

1.  Misbehaved numarrays.  Those that are byte swapped or misaligned
can't be used to construct Numeric arrays without copying.

2.  Readonly numarrays likewise couldn't be used to construct Numeric
arrays without copying or adding something akin to an "immutable" bit to
a->flags and then using it as a guard code where data is modified.

It'd be great if someone saw an easier or existing way to do this.  As
it stands it looks to me like a small extension function is all that is


More information about the Numpy-discussion mailing list