By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

Pyrex and numarray

P: n/a
Hi

I have tried to include numarray in Pyrex but I get allways
this error:

Traceback (most recent call last):
File "gears.py", line 9, in ?
import gl
File "/home/marco/projects/test_pyrex/gl.pyx", line 40, in gl
ctypedef class numarray.NumArray [object PyArrayObject]:
ValueError: numarray.NumArray does not appear to be the correct type object

The code is:

cdef extern from "numarray/numarray.h":
struct PyArray_Descr:
int type_num, elsize
char type
ctypedef class numarray.NumArray [object PyArrayObject]:
cdef char *data
cdef int nd
cdef int *dimensions, *strides
cdef object base
cdef PyArray_Descr *descr
cdef int flags
cdef object _data
cdef object _shadows
cdef long int nstrides
cdef long byteoffset
cdef long bytestride
cdef long itemsize
cdef char byteorder
cdef char _aligned
cdef char _contiguous
cdef char *wptr
def materialfv(GLenum face, GLenum pname, NumArray params):
"""
Set the material.

materialfv(enum face, enum pname, params)

params - should be a tuple

"""
assert (len(params) == 4 and pname in \
(AMBIENT, DIFFUSE, SPECULAR, EMISSION, AMBIENT_AND_DIFFUSE))\
or (len(params) == 1 and pname == SHININESS) \
or (len(param) == 1 and pname == COLOR_INDEXES),\
"arguments are not right"
str_params = params.tostring()
print str_params
cdef float* flat_params
flat_params = <float*>str_params
glMaterialfv(face, pname, flat_params)

I write a little OpenGL wrapper of my own(I know PyOpenGL but it doesn't fit
to me). Now I try to get the numarray to this function and cast it to a
float pointer(ugly hack, will be cleaned later).

regards

Marco

Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Mon, 19 Jan 2004 19:33:28 +0100, Marco Bubke wrote:
Hi

I have tried to include numarray in Pyrex but I get allways this error:

Traceback (most recent call last):
File "gears.py", line 9, in ?
import gl
File "/home/marco/projects/test_pyrex/gl.pyx", line 40, in gl
ctypedef class numarray.NumArray [object PyArrayObject]:
ValueError: numarray.NumArray does not appear to be the correct type
object


Yes, i had that problem. Here is what I'm using now:

ctypedef class numarray._numarray._numarray [object PyArrayObject]:
# Compatibility with Numeric
cdef char *data
cdef int nd
#cdef int dimensions[MAXDIM], strides[MAXDIM]
cdef int *dimensions, *strides
cdef object base
cdef PyArray_Descr *descr
cdef int flags
# New attributes for numarray objects
cdef object _data # object must meet buffer API
cdef object _shadows # ill-behaved original array.
cdef int nstrides # elements in strides array
cdef long byteoffset # offset into buffer where array data begins
cdef long bytestride # basic seperation of elements in bytes
cdef long itemsize # length of 1 element in bytes
cdef char byteorder # NUM_BIG_ENDIAN, NUM_LITTLE_ENDIAN
cdef char _aligned # test override flag
cdef char _contiguous # test override flag

cdef extern from "numarray/libnumarray.h":
void import_libnumarray()
object NA_NewAll(
int ndim, maybelong* shape, NumarrayType type, void* buffer,
maybelong byteoffset, maybelong bytestride, int byteorder, int aligned, int writeable )
object NA_NewAllStrides(
int ndim, maybelong* shape, maybelong* strides, NumarrayType type, void* buffer,
int byteorder, int byteoffset, int aligned, int writeable )
object NA_New( void* buffer, NumarrayType type, int ndim,... )
object NA_Empty( int ndim, maybelong* shape, NumarrayType type )
object NA_NewArray( void* buffer, NumarrayType type, int ndim, ... )
object NA_vNewArray( void* buffer, NumarrayType type, int ndim, maybelong *shape )

object NA_InputArray (object, NumarrayType, int)
object NA_OutputArray (object, NumarrayType, int)
object NA_IoArray (object, NumarrayType, int)
object PyArray_FromDims(int nd, int *d, int type)
object NA_updateDataPtr(object)
object NA_getPythonScalar(object, long)
object NA_setFromPythonScalar(object, int, object)
#object NA_getPythonScalar(PyArrayObject, long)
#object NA_setFromPythonScalar(PyArrayObject, int, PyArrayObject)

object PyArray_ContiguousFromObject(object op, int type,
int min_dim, int max_dim)
#cdef void*NA_OFFSETDATA(object)
void*NA_OFFSETDATA(_numarray)
int NA_nameToTypeNo (char*)
Simon.

--
Simon Burton, B.Sc.
Licensed PO Box A66
ANU Canberra 2601
Australia
Ph. 02 6249 6940
http://arrowtheory.com

Jul 18 '05 #2

P: n/a
thx

but I need to extract the array to a float*.

I do NA_OFFSETDATA(NA_InputArray(odata, tFloat64, C_ARRAY)) but this isn't
working because Pyrex says he can't storage it. There is a unknown size. I
only need the pointer tp a C array of floats. Thats all.

ragards

Marco
Jul 18 '05 #3

P: n/a
On Tue, 20 Jan 2004 13:48:42 +0100, Marco Bubke wrote:
thx

but I need to extract the array to a float*.

I do NA_OFFSETDATA(NA_InputArray(odata, tFloat64, C_ARRAY)) but this isn't
working because Pyrex says he can't storage it. There is a unknown size. I
only need the pointer tp a C array of floats. Thats all.

ragards

Marco


Use a variable?

cdef float* data
array = NA_InputArray( array, natype, C_ARRAY )
data = <float *>NA_OFFSETDATA(array)

Simon.

Jul 18 '05 #4

P: n/a
In article <bu**********@graf.cs.uni-magdeburg.de>, Marco Bubke wrote:
[snip]

I tried using the set-up with Pyrex, but I'm having trouble
referencing some of the symbols. (I actually put all of it inside the
cdef extern declaration -- maybe I shouldn't have? I couldn't make it
compile otherwise...)

I manage to reference some of the symbols (such as maybelong) but
others trip me up... If I try to define a variable of type
PyArray_Descr gcc crashes, and I'm not sure why. The only thing that
isn't a warning is

na_wrap.c:43: storage size of `__pyx_v_x' isn't known

If I run import_libnumarray(), Python can't find the symbol. (It does
find libnumarray.so, though.) The same happens when I try to use
NA_InputArray:

symbol NA_InputArray: referenced symbol not found

Why, oh why? :]

--
Magnus Lie Hetland "The mind is not a vessel to be filled,
http://hetland.org but a fire to be lighted." [Plutarch]
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.