Skip to content
Snippets Groups Projects
Commit 765fd539 authored by David Weir's avatar David Weir
Browse files

Added README.md based on email sent to collaborators

parent 06acbd9b
No related branches found
No related tags found
No related merge requests found
Long story short, LowLevelCallable.from_cython() needs to be pointed at a compiled, C-style function to work. You can do that with C, Cython, or Numba. Of these, the Cython method seems the easiest, but is strangely the least documented.
Here are the files I have sent:
- test.pyx is a Cython file, which contains a C-style function called integrand(). We want to turn this into C, compile it, and then later on use it to instantiate a scipy.LowLevelCallable instance so scipy.integrate can use than rather than working with interpreted python (which is slow).
- test.pxd is a Cython "header" file (I don't know what the correct name is). When (I think) cythonize() in setup.py detects this, it knows it's building a Cython file we want to call from C and it should behave itself accordingly, exporting symbols that a C linker can use if need be. Otherwise, Cython does not let c-style functions (those defined with cdef) be called from outside the Cython file, e.g. from other C code.
The corresponding python module then has the attribute __pyx_capi__, which is also needed for LowLevelCallable.from_cython() to work. I think this means that it has a 'parallel' C interface, as it were. There's another relevant example here: https://github.com/ashwinvis/cython_capi/tree/master/using_pxd
- setup.py is the usual distutils thing, but it knows it's building a Cython module because of the call to cythonize()
- run.py uses the integrand() function from test.pyx to do some numerical integrals.
To try this out you will need: python 3, cython
1. Run:
$ python3 setup.py build_ext --inplace
This builds the module from test.pyx (with symbols from test.pxd). Although running setup.py like this does the compilation for you, you can by the way see the C code generated in test.c, the function call starts something like:
static double __pyx_f_4test_integrand(CYTHON_UNUSED int __pyx_v_n, double *__pyx_v_args)
[CYTHON_UNUSED because the argument n is not used in integrand(), but the standard rubric for LowLevelCallable.from_cython() needs it]
But note that it really churns out doubles. This is really C at the end of the day.
2. To test:
$ python3 run.py
Should be 0: 0
Should be 4: 4
Should be 21.333...: 21.3333
Uses scipy.integrate.quad() to evaluate three numerical integrals. Note that I've tried to make it clear how to pass numerical coefficients from the python to the C code, so things don't need to be hardcoded.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment