So I need to write some code that performs partial integration. I have a numerical integrator schematically represented by
double integrate(double (*func)(double));
and suppose it works properly, i.e. for a function double f(double x)
, integrate(f)
gives the right result. What I have instead, however, are functions that look like
double f(double x, double y);
and I want to perform integration on only the first variable - so dynamically generate pointers to functions like g(x)=f(x,1)
or h(x)=f(x,2)
and pass them to integrate. I guess prototypically what I want is to define a function within another,
double compute(double y){
double g(double x){
return f(x, y);
}
return integrate(g);
}
which I know is not part of ANSI C even though GCC allows it (maybe I shouldn't care and just use it then). So, what would be the standard, C18 approved way to get this behavior? If the only possibility is to copy over the code of integrate
to the inside of compute
, I guess I'll just use the GCC extension even though my LSP constantly complains about it, but I'm interested in what the "proper" program would look like.