我正在处理C ++ 17中的一个问题,其中正在构建一个根求解器,该根求解器允许用户将用户定义的函数传递给根求解函数。下面显示了.cpp
文件的类示例,原型.hpp
文件中的原型。
// root.cpp
double RootSolver::newton(double guess, double right_side,
double (*func)(double),
double unc, int iter)
/**
Finds a specific root of a function using the Newton iteration
method
@param guess An initial guess for the value of the root
@param right_side The value of the right side of the
function.
@param func The function for which the root will be
determined
@param unc The uncertainty or tolerance in the accepted
solution. Defaulted to 0.001
@param iter The number of iterations to try before the
function fails. Defaulted to 150.
@return root
*/
{
double x1, x2, x3, y1, y2, slope;
x1 = guess;
x2 = x1 + 0.0000001;
for (int i = 0; i < iter; i++)
{
y1 = func(x1) - right_side;
y2 = func(x2) - right_side;
slope = (y2 - y1) / (x2 - x1);
x3 = x1 - (y1 / slope);
if (func(x3) - right_side <= unc and
func(x3) - right_side >= -unc) return x3;
x1 = x3;
x2 = x1 + 0.0000001;
}
exit_program(iter);
}
// ================================================================
// RootSolver PRIVATE FUNCTIONS
[[noreturn]] void RootSolver::exit_program(int iter)
{
std::string one("Function did not converge within ");
std::string two(" iterations");
std::cout << one << iter << two << std::endl;
exit (EXIT_FAILURE);
}
主文件如下:
double func1(double x);
double func2(double x, double a, double b);
int main() {
RootSolver q;
double guess = 2.0;
double right_side = 0.0;
// This function works fine
result = q.newton(guess, right_side, func1)
// - Not sure how to reformat RootSolver.newton so
I can pass it func1 as well as func2 so it can
accept the arguments a and b
return 0;
}
double func1(double x)
{
return pow(x, 6) - x - 1.0;
}
double func2(double x)
{
return pow(x, 6) - a * x - b * 1.0;
}
上面的代码对func1非常有用,因为x
是唯一的参数;但是,我不确定如何重新格式化RootSolver.newton
函数,因此它将使用func1
除了x
之外不带任何参数,并接受func2
和参数a
和b
。有谁知道我如何将参数传递给函数newton
,以使它不会为特定的输入函数进行硬编码?
答案 0 :(得分:2)
基于宽松的描述,听起来像是呼叫方Lambda解决了您的问题:
result = q.newton(guess, right_side, [](double x) {
return func2(x, 0, 0); // Replace 0s with values of a and b.
});
此lambda根据需要转换为double(*)(double)
。请注意,如果您需要捕获某些内容,这将不起作用,因为函数指针无法存储其他状态。有两种简单的方法可以解决这个问题。
制作一个模板(并将定义放在标题中):
template<typename F>
// requires std::is_invocable_r_v<double, F, double> // C++20 constraint option A
// requires requires(F f, double x) { f(x) -> double; } // C++20 constraint option B - can be extracted into a concept
double RootSolver::newton(double guess, double right_side,
F func,
double unc, int iter)
使用std::function
时会牺牲一些性能:
double RootSolver::newton(double guess, double right_side,
const std::function<double(double)>& func,
double unc, int iter)
答案 1 :(得分:2)
在这种情况下,您可以使用函数重载。
您可以在重载的版本中传递函数名称以及x,a和b作为参数,有点像这样(我现在只是考虑func,x,a和b,但您知道了):
1)重载了接受func1及其2参数的版本1
double newton(...<other parameters>..., double (*func)(double), double x)
2)重载了接受func2及其3参数的版本2
double newton(...<other parameters>..., double (*func)(double, double, double), double x, double a, double b)
现在,当您希望使用func1进行呼叫时,请使用:
newton(...., func1, x)
当您希望使用func2进行呼叫时,请使用:
newton(..., func2, x, a, b)