[Numpy-discussion] Best way to insert C code in numpy code
Tue Sep 22 09:45:31 CDT 2009
Xavier Gnata skrev:
> I have a large 2D numpy array as input and a 1D array as output.
> In between, I would like to use C code.
> C is requirement because it has to be fast and because the algorithm
> cannot be written in a numpy oriented way :( (no way...really).
There are certain algorithms that cannot be vectorized, particularly
those that are recursive/iterative. One example is MCMC methods such as
the Gibbs sampler. You can get around it by running multiple Markov
chains in parallel, and vectorizing this parallelism with NumPy. But you
cannot vectorize one long chain. Vectorizing with NumPy only applies to
data parallel problems.
But then there is a nice tool you should not about: Cython in pure
Python mode. You just add some annotations to the Python code, and the
.py file can be compiled to efficient C.
This is quite similar in spirit to the optional static typing that makes
certain implementations of Common Lisp (CMUCL, SBCL, Franz) so insanely
More information about the NumPy-Discussion