scipy/scipy — pull request #3262 by BrianNewsom (2014-01-31 19:14:05+00:00)
Title: Cwrapping

This PR implements my recent work on the backend of scipy.integrate, adding:
A lower level (C) method of handling multivariate functions so that they may be passed to the quadpack routines
C Wrappers for all double fortran quadpack routines supporting this function type
A simple additional wrapper that allows the wrappers to be incorporated into the existing SciPy integration, for testing purposes.

1.) Currently Scipy’s integration is inhibited in that it’s multivariate integration requires nested calls between python and fortran that are slow. The intent of my work (which is not fully completed) is to allow the movement of the innermost-loop integration entirely to compiled languages, which should be much faster.
The first step of my solution is the cwrapper.h file, which converts a multivariate function of the form f(int nargs, double args[nargs]) to a function of a single variable - the form expected by quadpack. This is handled nearly exactly as it is currently done in Python, storing the original function and any additional arguments to global variables, then using them from inside a generic “call” function passed into quadpack.

An example function of this type would be:
```
double f(int nargs, double args[nargs]){
args[0] * args[1] + args[2] * args[2]
}
```
Which would be equivalent to f(x) = x*y+z*z but now allows us to pass the number of extra arguments
(2) and their values in the args array so that they may be integrated in fortran.

2.) This file then additionally includes C wrappers for each of quadpack’s double routines that handle this evaluation. I added wrappers for every routine, for future use if necessary, though the current schema seems to only use abouImplement back end of faster multivariate integrationt 6 of them. These can be removed if superfluous. Between these two elements the foundation is laid for handling the n-dimensional integration at this lower level.

3.) Additionally, so that these wrappers may be tested and used, an additional wrapper has been added. This wrapper allows the currently handled python and ctypes functions to be evaluated in the f(nargs,args) form with another simple initialization and call. These functions are funcwrapper_init and funcwrapper, and these have been applied in the __quadpack.h file inside each routine so that the cwrapper is being applied as will be necessary with future additions. These functions should be removed, once SciPy’s integration module is adapted to use the new-style wrappers, as described earlier.

What does this mean for the end user?
As of now, nothing. No functionality has changed, quad works exactly the same way as before, passing all existing unit tests going through my wrappers. It can still handle ctypes or python functions of any number of variables, and this handling is still quite slow. Because of this, no additional documentation has been included.

Why does this PR matter, then?
This code adds a framework for doing exactly what I described earlier on, moving the handling of multiple variables down into a compiled language, which should provide a significant speed advantage to the power user, once a framework is written that allows ctypes or cython (or both) functions of this new format to be passed directly into the wrappers, without changing anything for the traditional user.

I am a first time contributor to SciPy but believe this will be an important contribution and am open to any advice, criticism, or comments on the code or process.