From: Francesc A. <fa...@op...> - 2003-01-21 19:23:27
|
A Dimarts 21 Gener 2003 17:26, v=E0reu escriure: > Francesc Alted wrote: > >Anybody is aware of any function (either in C or Python or a mixture o= f > >both) to easily convert Numerical Python arrays from/to numarray array= s? > > 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 cop= y > >element by element all the data, be able to copy the data buffer (or e= ven > >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 expla= in 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 byteord= er should be not a problem, because, as Numeric itself has no such a propert= y, 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 =3D=3D> Numeric conversion. Is that correct?. If yes, it = is possible to do a workaround about that, i.e. we can still get a Numeric f= rom a numarray without copying the data in case of numarray misaligned object= s?. Regarding to going in the other sense (ie. Numeric =3D=3D> numarray), as numarray supports discontiguity, misalignment and byteswapped data, this conversion should not imply a data buffer copy at all.=20 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 th= e 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-existenc= e of Numeric and numarray objects in the same program, and that would easy = the numarray deployment in existing Numeric software. Comments? --=20 Francesc Alted |