Can a unary functor have member variables?

I want to use the two parts of the boost library to complete the task, and I'm not sure how to do this.

I have a line of code for a function, it gives me an answer, given the values ​​of N, x and B:

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

We denote this as ans = f (B, N, x).

I want to find the root of this, i.e. the value of x in which the function, for example. y = f (x), y = 0.

My code in the form y = f (B, N, x) includes a small rearrangement to negate ans:

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

so far so good. Now I know in Boost that the algorithms in the root header will help us find the root, so when I read it, I see that TOMS Algorithm 748 will accept a unary functor as the main argument. The following is an example:

#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 * cos(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 , - ,

- :

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

, ?

- EDIT -

, , :

#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 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:

$ 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&)

, .

+3
1

:

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 , , "" ( , ), ( ).

+2

All Articles