Reputation: 335
I have written a bisection solver with a function name as argument and that function has only one argument:
double Bisection(double(*func)(double), double xLower, double xUpper, double xEPS = 1e-9, double yEPS = 1e-9);
Now I want to apply this solver in a Beta inverse cumulative probability function where the CDF is already known. But this CDF has three argument where the last two arguments are known:
double CDF(double x, double alpha, double beta)
How should I use this CDF as an argument of the solver by reducing the number of arguments?
Upvotes: 0
Views: 46
Reputation: 249133
The first step is to replace your C-style function pointer with a C++ std::function:
double Bisection(std::function<double(double)> func, double xLower, double xUpper, double xEPS = 1e-9, double yEPS = 1e-9);
The implementation will be the same as before. Now you can use std::bind()
to call it:
double alpha, beta, xLower, xUpper; // TODO: assign
Bisection(std::bind(CDF, _1, alpha, beta), xLower, xUpper);
What std::bind()
does there is to convert your three-argument CDF function into a one-argument (_1
) function by binding the second and third arguments to variables.
You can't use std::bind()
with your original C-style function pointer, as discussed here: convert std::bind to function pointer
Upvotes: 1