[Numpy-discussion] Conversion functions between Numeric and numarray arrays?

Francesc Alted falted at openlc.org
Tue Jan 21 11:24:08 EST 2003


A Dimarts 21 Gener 2003 17:26, vàreu escriure:
> Francesc Alted wrote:
> >Anybody is aware of any function (either in C or Python or a mixture of
> >both) to easily convert Numerical Python arrays from/to numarray arrays?
>
> I think you should look at numarray.fromlist() and NumArray.tolist().  I
> think fromlist() will work on a nested sequence object,  and hence a
> Numeric array.

Yeah, I knew that, but I was looking for something more optimal.

>
> >I mean, I would like to use such a funtion that, without having to copy
> >element by element all the data, be able to copy the data buffer (or even
> >use the same if possible at all) from one object to the other.
>
> I have not looked at this yet;   it's a very good question.  Note that
> going from numarray to Numeric there are issues with making the buffer
> well-behaved.

I think this should be not too difficult to achieve and I'll try to explain
why.

When going from numarray to Numeric, numarray already have NA_InputArray
C-API function that returns a well-behaved array. But strictly speaking, we
don't even need a well-behaved array (this is a too restrictive condition)
as both Numeric and numarray support discontiguous data. Even the byteorder
should be not a problem, because, as Numeric itself has no such a property,
we can create a Numeric array that is in native order as the result and
byteswap the numarray object (if needed) before doing the conversion.

So, non-alignment remains as the only issue that may cause a buffer copy
during numarray ==> Numeric conversion. Is that correct?. If yes, it is
possible to do a workaround about that, i.e. we can still get a Numeric from
a numarray without copying the data in case of numarray misaligned objects?.

Regarding to going in the other sense (ie. Numeric ==> numarray), as
numarray supports discontiguity, misalignment and byteswapped data, this
conversion should not imply a data buffer copy at all. 

Once we have a pointer to the data buffer, it is only a matter of
wrapping a Numeric or numarray object around it getting this info from the
original object, and returning the new object as a result.

All in all, this conversion *seems* to be not a too difficult task.

Making such a conversion functions (in C, but also having Python
counterparts) available might represent to open the door to a co-existence
of Numeric and numarray objects in the same program, and that would easy the
numarray deployment in existing Numeric software.

Comments?

-- 
Francesc Alted




More information about the NumPy-Discussion mailing list