[Numpy-discussion] Best way to insert C code in numpy code

René Dudfield renesd at gmail.com
Tue Sep 22 06:42:23 EDT 2009


On Mon, Sep 21, 2009 at 8:12 PM, David Warde-Farley <dwf at cs.toronto.edu> wrote:
> On 21-Sep-09, at 2:55 PM, Xavier Gnata wrote:
>
>> Should I read that to learn you cython and numpy interact?
>> Or is there another best documentation (with examples...)?
>
> You should have a look at the Bresenham algorithm thread you posted. I
> went to the trouble of converting some Python code for Bresenham's
> algorithm to Cython, and a pointer to the Cython+NumPy tutorial:
>
> http://wiki.cython.org/tutorials/numpy
>
> David

I don't know about the best way... but here are two approaches I like...

Another way is to make your C function then load it with ctypes(or
wrap it with something else) and pass it pointers with
array.ctype.data.  You can find the shape of the array in python, and
pass it to your C function.  The benefit is it's just C code, and you
can avoid the GIL too if you want.  Then if you keep your C code
separate from python stuff other people can use your C code in other
languages more easily.

cinpy is another one(similar to weave) which can be good for fast
prototyping... http://www.cs.tut.fi/~ask/cinpy/ or for changing the
code to fit your data.


cheers,



More information about the NumPy-Discussion mailing list