From: Francesc A. <fa...@ca...> - 2006-10-17 16:07:38
|
Hi, I'm looking for an easy way to access the data area of the numpy scalars no= =20 matter its type. I've seen that numpy/arrayscalars.h define a structure fo= r=20 each scalar type, so I'd guess that it will not be possible to find a gener= al=20 way for accessing the data buffer for each type. So, I've decided to look f= or=20 a workaround and I've devised a couple of possibilities: 1.- Promote the scalar type to a ndarray object and use the regular ndarray= C=20 structure to access the data buffer. 2.- Fetch the buffer in scalartype.data and use the buffer protocol in orde= r=20 to access the pointer to data in memory. However, I lack experience in buff= er=20 protocol, so suggestions for achieving this are welcome. If there is some other trivial way that I haven't devised (specially if usa= ble=20 from pyrex), please tell me about. TIA, =2D-=20 >0,0< Francesc Altet =A0 =A0 http://www.carabos.com/ V V C=E1rabos Coop. V. =A0=A0Enjoy Data "-" |
From: Travis O. <oli...@ee...> - 2006-10-17 16:22:21
|
Francesc Altet wrote: >Hi, > >I'm looking for an easy way to access the data area of the numpy scalars no >matter its type. I've seen that numpy/arrayscalars.h define a structure for >each scalar type, so I'd guess that it will not be possible to find a general >way for accessing the data buffer for each type. So, I've decided to look for >a workaround and I've devised a couple of possibilities: > > This problem showed up in writing NumPy several times. One solution might be to use PyArray_ScalarAsCtype(PyObject *scalar, void *ctypeptr) which will copy the data into the area pointed to by ctypeptr (unless you have a "flexible scalar" in which case only a pointer to the data-area will be copied). >2.- Fetch the buffer in scalartype.data and use the buffer protocol in order >to access the pointer to data in memory. However, I lack experience in buffer >protocol, so suggestions for achieving this are welcome. > > This will also work. A read-only buffer protocol is exported by all the scalars. scalar.data will return a buffer object. Or you can use the Python C-API const char *buffer; Py_ssize_t buflen; PyObject_AsReadBuffer(scalar, (const void **)&buffer, &buflen) to retrieve a pointer to the data in buffer and the size of the data in buflen. -Travis |
From: Francesc A. <fa...@ca...> - 2006-10-17 16:36:13
|
A Dimarts 17 Octubre 2006 18:22, Travis Oliphant va escriure: > >2.- Fetch the buffer in scalartype.data and use the buffer protocol in > > order to access the pointer to data in memory. However, I lack experien= ce > > in buffer protocol, so suggestions for achieving this are welcome. > > This will also work. A read-only buffer protocol is exported by all > the scalars. > > scalar.data will return a buffer object. > > Or you can use the Python C-API > > const char *buffer; > Py_ssize_t buflen; > > PyObject_AsReadBuffer(scalar, (const void **)&buffer, &buflen) Oh, this one seems pretty easy, and as a plus, you don't have to book memor= y=20 for copying the data area, so I'll use it. Thanks, =2D-=20 >0,0< Francesc Altet =A0 =A0 http://www.carabos.com/ V V C=E1rabos Coop. V. =A0=A0Enjoy Data "-" |
From: Lisandro D. <da...@gm...> - 2006-10-17 18:03:44
|
On 10/17/06, Travis Oliphant <oli...@ee...> wrote: > Or you can use the Python C-API > > const char *buffer; > Py_ssize_t buflen; > > PyObject_AsReadBuffer(scalar, (const void **)&buffer, &buflen) > > to retrieve a pointer to the data in buffer and the size of the data in > buflen. > Travis. Have numpy something similar to a mutable scalar, or the only way is using an array whit only one element? --=20 Lisandro Dalc=EDn --------------- Centro Internacional de M=E9todos Computacionales en Ingenier=EDa (CIMEC) Instituto de Desarrollo Tecnol=F3gico para la Industria Qu=EDmica (INTEC) Consejo Nacional de Investigaciones Cient=EDficas y T=E9cnicas (CONICET) PTLC - G=FCemes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 |
From: Travis O. <oli...@ee...> - 2006-10-17 19:19:46
|
Lisandro Dalcin wrote: >On 10/17/06, Travis Oliphant <oli...@ee...> wrote: > > >>Or you can use the Python C-API >> >>const char *buffer; >>Py_ssize_t buflen; >> >>PyObject_AsReadBuffer(scalar, (const void **)&buffer, &buflen) >> >>to retrieve a pointer to the data in buffer and the size of the data in >>buflen. >> >> >> > >Travis. Have numpy something similar to a mutable scalar, or the only >way is using an array whit only one element? > > 0-dim arrays are still mutable scalars. -Travis |