I'm trying to use GSL's multimin routine from C++, but the same issue
arises with integration and any functionality that uses a callback function
to calculate a user function f(x). The calls to GSL and to the callback
function are in a class. I am using g++ 4.1.2 and GSL 1.13.

l define a function myF() to compute f(x), and pass GSL a pointer to myF().
The problem is that the compiler won't convert from "pointer to myF ()" to
the way that GSL declares the pointer. This is because C++ treats a
"pointer to member function" differently from how it treats a pointer to a
function that is not part of a class. I understand the C++ issue; I hope
that somebody on this list knows how to get around it.

Hi,

Another standard suggestion is the following

class gsl_function_pp : public gsl_function
{
    public:
gsl_function_pp(boost::function<double(double)> const& func) : _func(func)
    {
        function=&gsl_function_pp::invoke;
        params=this;
    }

    private:
    boost::function<double(double)> _func;

    static double invoke(double x, void *params)
    {
        return static_cast<gsl_function_pp*>(params)->_func(x);
    }
};

When I need to invoke member functions with parameters,

Class::integrand(double x, double par1)
{...}

I use something like this

gsl_function_pp Fp( boost::bind(&Class::integrand, this, z, _2 ) );

gsl_function *F = static_cast<gsl_function*>(&Fp);

Best
Vinicius

Reply via email to