help-gsl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-gsl] using gsl callback routine from c++ / pointer to member f


From: Andrew W. Steiner
Subject: Re: [Help-gsl] using gsl callback routine from c++ / pointer to member function
Date: Wed, 13 Feb 2013 08:06:43 -0800

Hello,

     I'll put in a short plug for O2scl (o2scl.sourceforge.net) which
rewrites the GSL minimizers so that the function is specified through
a template type. In this way, one can use std::function and has the
full flexibility of the newer C++ lambda function extensions. You
may benefit from the discussion and examples at
http://o2scl.sourceforge.net/o2scl/html/funct_section.html
O2scl also has some separate minimizers for non-differentiable
functions.

     Of course a static member function works, so you can
always create an intermediary class. GSL calls the static
member function in the intermediary and the intermediary calls
the function you had originally wanted to minimize.

Take care,
Andrew

On Wed, Feb 13, 2013 at 12:26 AM, Hershel Safer <address@hidden> wrote:
> Hi folks,
>
> 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.
>
> Details:
>
> Part 1
>
> My code has the function
>     double myClass::myF(const gsl_vector * x, void * params) { code to
> calculate f(X) }
>
> GSL uses a struct gsl_multimin_function to incorporate relevant information
> about the function to be minimized, including a pointer to myF(). I
> instantiate this struct using:
>     gsl_multimin_function funcStruct;
>
> Within the struct gsl_multimin_function, GSL declares the pointer to my
> f(x) function as:
>     double (* f) (const gsl_vector * x, void * params);
>
> When initializing the GSL minimization, I tell GSL where to find my
> function by writing (following the example in the GSL manual, chapter 35):
>     funcStruct.f = &myClass::myF;
>
> The compiler complains about the conversion between pointer types:
>     error: cannot convert double (myClass::*)(const gsl_vector*, void*) to
> double (*)(const gsl_vector*, void*) in assignment
>
> The question is how to tell GSL how to call myF().
>
> Part 2
>
> One suggestion that I found from a Google search is to use a typedef:
>     typedef double (myClass::* FPTR) (const gsl_vector*, void*);
>     FPTR fptr = &myClass::myF;
>     funcStruct.f = fptr;
>
> But the compiler is smart enough to recognize that this is the same as the
> straightforward approach, and it gives a similar error message to the above.
>
> Part 3
>
> Another suggestion was to make myF() static, ugly though that may be. I
> tried that, and got an error message that the minimization routine could
> not be found. Oddly, this was a runtime error. The linker did not complain,
> so presumably it found the nmsimplex2 routine, but somehow the routine
> could not be found at runtime.
>
>     In the code: const gsl_multimin_fminimizer_type *T =
> gsl_multimin_fminimizer_nmsimplex2
>
>     Runtime error: Unrecognized symbol gsl_multimin_fminimizer_nmsimplex2
>
> So: Has anybody gotten GSL callbacks to work in C++? If so, please tell me
> how.
>
> Thank you so much,
> Hershel Safer
>
>
>
> --
> Hershel Safer, Ph.D.
> address@hidden | +972-54-463-1977 | skype: hsafer



reply via email to

[Prev in Thread] Current Thread [Next in Thread]