Re: [Cython] access to numpy functions?

Robert Bradshaw <[email protected]>
Newsgroups gmane.comp.python.cython.devel,gmane.comp.python.pyrex
Message-ID <[email protected]>
On May 1, 2008, at 7:39 AM, Dag Sverre Seljebotn wrote:

>
>> API?  If I have two objects in Cython/Pyrex, like:
>>
>>
>> c_numpy.ndarray x,y
>>
>> and I want to do a dot product, or add the two arrays, is there
>> access to the numpy C-version of this?  I know I could write my own
>> for the dot product, but I'd rather not reinvent the wheel when
>> possible.
>>
>
> First off, you can use the Python functions directly, like this  
> (Cython
> syntax):
>
> ---
> import numpy
> cimport c_numpy
>
> cdef c_numpy.ndarray a = ..., b = ...
> cdef c_numpy.ndarray c = numpy.dot(a, b)
> ---
>
> As for calling the NumPy C functions directly: The a place to start is
> looking seems to be core/src/multiarraymodule.c. (You may want to  
> ask in
> the numpy mailing list as well though. I'm not very familiar with  
> NumPy
> internals.)
>
> My first impression is that the implementations (say,
> PyArray_InnerProduct for instance) seem to be a) very generic (any
> number of dimensions/strides etc. and b) coded using the Python/C API
> directly, so there is no "inner layer" to communicate with. I.e.,
> arguments to functions are Python objects.
>
> So you're probably just as well off by calling the functions  
> through the
> Python API like stated above.
>
> If that is not fast enough, there doesn't seem to be much to be gained
> by "direct calls" to the C functions; and a new implementation in  
> Cython
> seems in order.

And fortunately this is another GSoC project.

> The main reason is that to make this faster you will
> want to make use of additional information -- if you know the  
> number of
> dimensions (or even their size) and datatype at compile-time, you can
> make some optimizations that functions in the NumPy library will never
> be able to do. (For most if not all cases I'm guessing that we're
> talking very small gains if any gains here; though if you tend to
> multiply really small arrays a lot (like doing coordinate
> transformation...) then I suppose you must write your own code.)
>
> -- 
> Dag Sverre
>
> _______________________________________________
> Cython-dev mailing list
> Cython-dev-F/[email protected]
> http://codespeak.net/mailman/listinfo/cython-dev
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.