On 20 May 2010 17:40, jah <[email protected]> wrote:
> Hi,
>
> I'm reading through numpy.pxd and trying to understand the following:
>
>    ctypedef class numpy.ndarray [object PyArrayObject]
>

> Probably, I have made some wrong conclusions and was hoping for an
> explanation.  Sorry if these are simple questions, but I wasn't able
> to find info on this in the docs (links are appreciated).
>
> 0) What does the above statement mean, in words?
>

1) There is an extension type called "ndarray" in module "numpy"

2) The object structure (i.e. a C struct that has extra slots
extending the core Python's PyObject structure) is called
PyArrayObject

3) the "ctypedef" bit means that generated C code will use plain
"PyArrayObject" for declaring structs, whild if you do cdef class
...., the generated C code will use "struct PyArrayObject". In the
specific case of numpy, look at the header "numpy/arrayobject.h", you
should see "typedef struct PyArrayObject { ...} PyArrayObject"... As
PyArrayObject end-up being a C typedef, in Cython side you have
declare with "ctypedef".

Pretty obvious, right? ;-)

> 1)  Previously, I thought I understood what the keyword "object"
> meant.  I believed it to be a synonmyn for PyObject*, but the above
> usage makes me question that conclusion.  So what is "object" really?
>
> 2) In the above, I am having trouble seeing what is the original type
> and the new type, as in "typedef oldtypename newtypename".  When I
> read the above code, it seems as if there is no "second argument".
> Help.
>


> Relatedly, I was wondering how best to use an object member. For instance,
>
> cdef Klass:
>   cdef object x
>   def __init__(self, np.ndarray[FTYPE_t, ndim=1, mode="c"] x):
>       self.x = x
>   def func(self):
>       cdef np.ndarray[FTYPE_t, ndim=1, mode="c"] x = self.x
>       # ...use x instead of self.x for fast access so that
>       # (self.x.shape[0] is operating on a Python tuple)
>
> When I examine the C code, I see that x (the __init__ method parameter) has
> type PyArrayObject* while self.x has PyObject*.  No problem. In func(),
> if I want to have fast access to self.x, is the recommended
> way to do this as I did above?  I wasn't sure if I should instead do
> something like:
>
>   def func(self):
>        cdef PyArrayObject* x = <PyArrayObject *>(self.x)
>        # ...use x instead of self.x for fast access
>

You could:

cdef Klass:
  cdef np.ndarray x

I think you still have to:

def func(self):
    cdef np.ndarray[FTYPE_t, ndim=1, mode="c"] x = self.x



-- 
Lisandro Dalcin
---------------
CIMEC (INTEC/CONICET-UNL)
Predio CONICET-Santa Fe
Colectora RN 168 Km 472, Paraje El Pozo
Tel: +54-342-4511594 (ext 1011)
Tel/Fax: +54-342-4511169
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to