Using non-static member function pointer to define function

I want to use the gnu scientific library for minimization, but I want to wrap it in an adaptor so that I can easily use a different implementation without changing my code if I want to. The interface should take an argument double (*myFunction)(double* args), but the library requires an argument of the form double (* gsl_function)(gsl_vector* newArgs). I tried to take the input function pointer and use it to define a member function of the form double (* gsl_function)(gsl_vector* newArgs), but this fails because double optim::(* gsl_function)(gsl_vector* newArgs) is not the same as the expected double (* gsl_function)(gsl_vector* newArgs). My code is below.



///////////////////////

#include <gsl_multimin.h>

class optimizer{
public:
int numArgs;
double* argVec;
optimizer(double(*inFunction)(double* x),int numArgs_,double* startPoint);
double (*theFunction)(double* x);
double theGSLFunction (const gsl_vector *v, void* params);
private:
double* par;
const gsl_multimin_fminimizer_type *T;
gsl_multimin_fminimizer *s;
gsl_multimin_function minex_func;
size_t iter;
int status;
double size;
gsl_vector *ss, *x;
};


optimizer::optimizer(double (*inFunction)(double* x), int numArgs_,double* startPoint){
numArgs = numArgs_;
argVec = new double[numArgs_];
theFunction = inFunction;
T = gsl_multimin_fminimizer_nmsimplex2;
s = NULL;
iter = 0;
/* Starting point */
x = gsl_vector_alloc (numArgs_);
for(int i=0; i< numArgs_; i++){
gsl_vector_set (x, i, startPoint[i]);
}

/* Set initial step sizes to 1 */
ss = gsl_vector_alloc (numArgs_);
gsl_vector_set_all (ss, 1.0);

/* Initialize method and iterate */
minex_func.n = numArgs_;
minex_func.f = theGSLFunction;
minex_func.params = par;
}

double optimizer::theGSLFunction (const gsl_vector *v, void* params){
for(int i=0;i < numArgs; i++){
argVec[i]=gsl_vector_get(v, i);
}
double y = theFunction(argVec);
return y;

}
Topic archived. No new replies allowed.