Mex function with function as input
显示 更早的评论
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);
回答(1 个)
James Tursa
2013-10-25
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!