Mex function with function as input
Show older comments
Hi all,
I am trying to compile a C function from GSL, which takes in a function as an input. The function is called hcubature, and used for numerical integration.
I was wondering how I would do this? The defenitions are shown below:
typedef int (*integrand) (unsigned ndim, const double *x, void *, unsigned fdim, double *fval);
int hcubature(unsigned fdim, integrand f, void *fdata, unsigned dim, const double *xmin, const double *xmax, size_t maxEval, double reqAbsError, double reqRelError, error_norm norm, double *val, double *err);
Answers (1)
James Tursa
on 25 Oct 2013
I am not sure what your question is. Are you simply asking how to fill the 2nd argument in the hcubature call? If so, simply write a function with the appropriate integrand signature using any name you like, then put the name of that function in the 2nd argument spot in the call. E.g.,
int myintegrand(unsigned ndim, const double *x, void *vp, unsigned fdim, double *fval)
{
// body of function
}
Then in the hcubature call, assuming all of the other variables are appropriately typed as well:
i = hcubature(fdim, myintegrand, fdata, dim, xmin, xmax, maxEval, reqAbsError, reqRelError, norm, val, err);
The name of a function, when used in an expression (such as an argument in another function call) is converted to a pointer to that function, which is what your hcubature function wants.
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!