Dominik Dominik - 2 months ago 13
Python Question

Calling Fortran routines in scipy.special in a function jitted with numba

Is there a way to directly or indirectly call the Fortran routines that can be found here https://github.com/scipy/scipy/tree/master/scipy/special/cdflib and that are used by

scipy.stats
from a function that is supposed to by compiled by
numba
in
nopython
mode?

Concretely, because
scipy.stats.norm.cdf()
is somehow pretty slow, I am right now directly using
scipy.special.ndtr
, which is called by the former. However, I'm doing this in a loop, and my intend is to speed it up using
numba
.

Answer

I would take a look at rvlib, which uses Numba and CFFI to call RMath, which is the standalone C library that R uses to calculate statistical distributions. The functions it provides should be callable by Numba in nopython mode. Take a look at the README for an example of the function that is equivalent to scipy.stats.norm.cdf()

If you're still interested in wrapping cdflib yourself, I would recommend using CFFI. You'll have to build a C-interface for the functions you want. You might find this blog post that I wrote helpful in getting started:

https://www.continuum.io/blog/developer-blog/calling-c-libraries-numba-using-cffi

Comments