devernay / cminpack

A C/C++ rewrite of the MINPACK software (originally in FORTRAN) for solving nonlinear equations and nonlinear least squares problems
http://devernay.free.fr/hacks/cminpack/
145 stars 63 forks source link

Can we use template to pass not only function pointer, but lambda, functor or something else? #74

Open LittleDogXi opened 2 months ago

LittleDogXi commented 2 months ago

In cminpack, two types of function pointer are used. For example, as for hybrd, we could pass a function pointer to it, which is like void(*fun)(const int *n, const real *x, real *fvec, int *iflag) or int (*fun)(void *p, int n, const real *x, real *fvec, int iflag). The void *p in the latter type is used to pass additional parameters without using global variables. But if we want to use lambda, functor, or something else, we cannot make it. So, why not use some templates, so that we are not only able to pass a static type function pointer to hybrd, but also lambda, functor or other stuff.

devernay commented 2 months ago

The underlying code is C, and I'm not sure how to call a lambda from C. This goes beyond my C++expertise. If you find a way to make it work without breaking the C API, please submit a PR