[Numpy-discussion] ndarray of object dtype
Ioan Ferencik
ioan.ferencik at tkk.fi
Wed Oct 6 02:27:29 EDT 2010
Thanks Robert,
that worked just great.
I was wandering whether anyone has experience with calling Fortran 90
functions that return derived types and cast them to C/Python objects.
I have found excellent reference in a couple of websites, however all
examples deal with returning at best 2D arrays of fundamental/basic
types. When it comes to derived types/structures I have following
dilemma:
should I use Fortran subroutines, allocate memory in C and pass the
structures as arguments or should I use functions, allocate memory in
Fortran and return the data to C/Python.
I should mention I have little experience in C/Fortran.
All thoughts are welcome.
Quoting Robert Kern <robert.kern at gmail.com>:
> On Mon, Oct 4, 2010 at 00:41, Ioan Ferencik <ioan.ferencik at tkk.fi> wrote:
>> Hello list,
>>
>> I am trying to pass elements of a custom defined with C API.
>> I have successfully wrapped a type around a C struct.
>> I intent to create a list with these objects and process it using
>> numpy C API.
>> So i create an array:
>>
>> array = (PyArrayObject *)PyArray_ContiguousFromObject(input,
>> PyArray_OBJECT, 0, 0);
>>
>> to my understanding each el. in this array is a pointer to my type so
>> a cast to this type
>> should work.
>>
>>
>> this is my custom type
>>
>> typedef struct val{
>> PyObject_HEAD
>> float q;
>> float wl;
>> int cssid;
>> int br;
>> }hm1dval;
>>
>> I am passing only one element for testing purposes.
>>
>> in python
>> a = [hm1d1.hm1dval() for i in range(0,1)]
>> for c in a:
>> c.set_values(q=3.0, wl=2.5, cssid=6, br=7)
>>
>>
>> So following code should be valid in C:
>>
>> hm1dval s* = PyArray_DATA(array);
>>
>> but the members are 0 after casting in spite they were set previously.
>
> The elements of a dtype=object array are PyObject* pointers. In C, an
> array of pointers would look like this:
>
> hm1dval **s = PyArray_DATA(array);
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless
> enigma that is made terrible by our own mad attempt to interpret it as
> though it had an underlying truth."
> -- Umberto Eco
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
Ioan Ferencik
PhD student
Aalto University
School of Science and Technology
Faculty Of Civil and Env. Engineering
Lahti Center
Tel: +358505122707
More information about the NumPy-Discussion
mailing list