使用boost库工具/ root进行编译时出错

时间:2014-02-07 16:36:26

标签: c++ boost compilation compiler-errors

我最近使用apt-get在ubuntu上下载了boost库。

我编写了以下代码,它定义了一个一元仿函数,然后尝试使用boost库找到它的根目录(我现在把它全部放在main.cpp中并稍后拆分):

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


class MyBinom {
public:
    MyBinom(int B, int N, int ans);

    double operator()(const double x) const;

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

MyBinom::MyBinom(int B, int N, int ans) : B(B), N(N), ans(ans) {}

double MyBinom::operator()(const double x) const {
    boost::math::binomial dist(N, x);
    double distans = boost::math::cdf(dist, B) - ans;
    return distans;
}


int main()
{
    typedef std::pair<double, double> Result;

    MyBinom myBinom95(75, 4167, 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.

    Result 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;
}

我确实知道我的一元运算符是有效的,因为我编译并运行了一个运行任意值为x的运算符的示例。

然而,上面的代码编译不正确,我是新手,我不知道为什么。

我编译:

$ g++ -Wall main.cpp -o main

但是我收到以下错误:

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:37:81:   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&)

我仍然习惯于编译错误,但似乎找不到函数:boost :: math :: tools :: toms748_solve?

这是一个错误,因为我没有正确构建和编译(我需要构建或链接的东西吗?)或者我的语法是垃圾吗?

- EDIT / ADDENUM -

代码现在按照自己的答案工作,输出为:

Lets take a look at the root
root bracketed: [ 1 , 1 ]
f(1)=0
f(1)=0
max_iter=2

然而,这与我在尝试用C ++完成它之前使用的原型R代码进行相同的计算是不一致的:

> binomcalc <- function(p, p0, N, B){pbinom(B,N,p)-p0}
> uniroot(binomcalc, c(0,1), p0=0.05, B=75, N=4167)
$root
[1] 0.02178983

$f.root
[1] -0.0009172035

$iter
[1] 12

$estim.prec
[1] 6.103516e-05

我想知道为什么他们不同?我需要调整容差或精度吗?

谢谢, 本。

1 个答案:

答案 0 :(得分:3)

我的编译器提及

\usr\include\boost\math\tools\toms748_solve.hpp|283 col 17| note:   template argument deduction/substitution failed:
\usr\include\boost\math\tools\toms748_solve.hpp|468 col 81| note:   deduced conflicting types for parameter 'const T' ('int' and 'double')

所以......很明显:)修复它:

Result r1 = boost::math::tools::toms748_solve(myBinom95, 0., 1., tol, max_iter); // use the toms solve algorithm.

输出:

Let's take a look at the root
root bracketed: [ 1 , 1 ]
f(1)=0
f(1)=0
max_iter=2

查看 Live On Coliru