gpt4 book ai didi

c++ - 一元仿函数可以有成员变量吗?

转载 作者:太空狗 更新时间:2023-10-29 21:43:54 26 4
gpt4 key购买 nike

我想使用两个 boost 库来执行一个任务,但我不确定如何进行。

我有一行函数代码,它给了我一个给定 N、x 和 B 值的答案:

double ans = cdf(boost::math::binomial mybinom(N, x), B);

让我们将其表示为 ans = f(B,N,x)。

我想找到这个的根,即 x 的值,函数例如y = f(x),导致 y = 0。

我的代码,形式为 y = f(B,N,x) 涉及一个小的重新排列以否定 ans:

double myfun = function(B, N, x, ans){
double output = (cdf(boost::math::binomial mybinom(N, x), B) - ans);
return output;
}

到目前为止一切顺利。现在我知道在 Boost 中根 header 中的算法将帮助我们找到根,所以在阅读时我看到 TOMS 算法 748 将接受一元仿函数作为主要参数 'f'。示例如下:

#include <iostream>
#include <sstream>
#include <string>


#include <boost/math/tools/roots.hpp>

class Test {
public:
double operator()(const double x) {
return x * cos(x);
}
};

// see:
// http://www.boost.org/doc/libs/1_47_0/libs/math/doc/sf_and_dist/html/math_toolkit/toolkit/internals1/roots2.html

int main(int argc, char** argv) {
Test t;
typedef std::pair<double, double> Result;
boost::uintmax_t max_iter=500;
boost::math::tools::eps_tolerance<double> tol(30);

Result r1 = boost::math::tools::toms748_solve(t, 1.0, 2.0, tol, max_iter);
std::cout << "root bracketed: [ " << r1.first << " , " << r1.second << " ]" << std::endl;
std::cout << "f("<< r1.first << ")=" << t(r1.first) << std::endl;
std::cout << "f("<< r1.second << ")=" << t(r1.second) << std::endl;
std::cout << "max_iter=" << max_iter << std::endl;
return 0;
}

好的,现在我遇到了一个问题。我看到的每个例子(对于这个和类似的算法)都需要一个一元仿函数。并显示一个只有 x 作为参数的函数,例如x * 余弦(x)。然而你看到我的函数有参数 B、N、x 和 ans。那么我如何从 f(B,N,x,ans) 得到一个一元仿函数,即 f(x),以便 boost 算法可以接受它作为输入来找到根?

我想到的一件事是也许(我在这里不知道,因为直到现在我才听说过一元仿函数),据我所知,一元仿函数是类,类可以有函数和变量,所以可以设计一个一元仿函数类,所以在创建时,你提供 B 和 N 以及 ans,它们的值然后是成员变量,然后是 double operator()(const double x){} 中的函数一元仿函数的 可以使用 B 和 N 以及 ans,因为它们是成员变量,不需要在运算符中提供,因此运算符是一元的。因此,就我而言,我在创建对象时指定了 B 和 N 变量,但就 boost 算法和一元运算符而言,它们是成员变量,只是函数体内已经可用的值?

我想是这样的:

class Test {
public:
double operator()(const double x) {
return (cdf(boost::math::binomial mybinom(N, x), B) - ans);
}
private:
int B;
int N;
double ans;
};

我是在正确的路线上还是在错误的树上咆哮?

--编辑--

这是我目前为尝试解决此问题而生成的代码:

#include <iostream>
#include <boost/math/distributions/binomial.hpp>
#include <boost/math/tools/roots.hpp>



class MyBinom
{
public:
MyBinom(int n, int b, double p);
double operator()(const double x);

private:
int N;
int B;
double P;
};


MyBinom::MyBinom(int n, int b, double p)
: N(n), B(b), P(p)
{/* Body intentionally empty */}


double MyBinom::operator()(const double x)
{
boost::math::binomial myBinomial(N, x);
return (cdf(myBinomial, B) - P);
}



int main()
{
MyBinom myBinom95(4715, 75, 0.95); // Create the Class with the unary operator.

boost::uintmax_t max_iter=500; // Set max iterations.

boost::math::tools::eps_tolerance<double> tol(30); //Set the eps tolerance.

std::pair<double, double> r1 = boost::math::tools::toms748_solve(myBinom95, 0, 1, tol, max_iter); // use the toms solve algorithm.

std::cout << "Let's take a look at the root" << std::endl;
std::cout << "root bracketed: [ " << r1.first << " , " << r1.second << " ]" << std::endl;
std::cout << "f("<< r1.first << ")=" << myBinom95(r1.first) << std::endl;
std::cout << "f("<< r1.second << ")=" << myBinom95(r1.second) << std::endl;
std::cout << "max_iter=" << max_iter << std::endl;

return 0;
}

但是它不编译 - 与 boost header 有关:

$ g++ main.cpp -o test
In file included from /usr/include/boost/math/tools/roots.hpp:32:0,
from /usr/include/boost/math/special_functions/detail/igamma_inverse.hpp:16,
from /usr/include/boost/math/special_functions/gamma.hpp:1528,
from /usr/include/boost/math/special_functions/beta.hpp:15,
from /usr/include/boost/math/distributions/binomial.hpp:83,
from main.cpp:2:
/usr/include/boost/math/tools/toms748_solve.hpp: In function 'std::pair<T, T> boost::math::tools::toms748_solve(F, const T&, const T&, Tol, uintmax_t&, const Policy&) [with F = MyBinom, T = int, Tol = boost::math::tools::eps_tolerance<double>, Policy = boost::math::policies::policy<boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy>, uintmax_t = long unsigned int]':
/usr/include/boost/math/tools/toms748_solve.hpp:475:71: instantiated from 'std::pair<T, T> boost::math::tools::toms748_solve(F, const T&, const T&, Tol, uintmax_t&) [with F = MyBinom, T = int, Tol = boost::math::tools::eps_tolerance<double>, uintmax_t = long unsigned int]'
main.cpp:41:100: instantiated from here
/usr/include/boost/math/tools/toms748_solve.hpp:467:81: error: no matching function for call to 'toms748_solve(MyBinom&, const int&, const int&, double, double, boost::math::tools::eps_tolerance<double>&, uintmax_t&, const boost::math::policies::policy<boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy>&)'
/usr/include/boost/math/tools/toms748_solve.hpp:467:81: note: candidates are:
/usr/include/boost/math/tools/toms748_solve.hpp:283:17: note: template<class F, class T, class Tol, class Policy> std::pair<T, T> boost::math::tools::toms748_solve(F, const T&, const T&, const T&, const T&, Tol, uintmax_t&, const Policy&)
/usr/include/boost/math/tools/toms748_solve.hpp:458:24: note: template<class F, class T, class Tol> std::pair<T, T> boost::math::tools::toms748_solve(F, const T&, const T&, const T&, const T&, Tol, uintmax_t&)
/usr/include/boost/math/tools/toms748_solve.hpp:464:24: note: template<class F, class T, class Tol, class Policy> std::pair<T, T> boost::math::tools::toms748_solve(F, const T&, const T&, Tol, uintmax_t&, const Policy&)
/usr/include/boost/math/tools/toms748_solve.hpp:473:24: note: template<class F, class T, class Tol> std::pair<T, T> boost::math::tools::toms748_solve(F, const T&, const T&, Tol, uintmax_t&)

谢谢,本。

最佳答案

以下是正确的:

class Test {
public:
Test(int B, int N, int ans) : B(B), N(N), ans(ans) {}

double operator()(const double x) const {
return (cdf(boost::math::binomial mybinom(N, x), B) - ans);
}
private:
int B;
int N;
double ans;
};

请注意,大多数情况下,Functor 是被复制的,所以如果你想检索一些“可变”信息(比如你的 functor 被调用了多少次),你必须传递一个额外的指针(一个更简单的计数器是不够的) .

关于c++ - 一元仿函数可以有成员变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21616446/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com