R
RHS
I'm just starting to use C++, and don't want to have to rewrite all of
the old C routines that I've been using for 20+ years. One of these is
a general purpose function minimizer, which expects to be passed a
pointer to the function to be minimized,
double func(double p[])
How do I use this to minimize a function defined within a class in C+
+? To be specific, suppose I define the following class, which returns
the sum of squared differences between an input vector and a (private)
vector called trueParms:
class max_func {
public:
max_func(vector <double> trueParms_) : trueParms(trueParms_) {};
double operator()(double p[]);
private:
vector <double> trueParms;
};
double max_func:perator () (double p[])
{
unsigned int i;
double temp;
double ss;
for (i=0, ss=0.0; i< trueParms.size(); i++) {
temp = p - trueParms;
ss += temp * temp;
}
return ss;
};
In the main code, if I have a vector v containing the values 1.0, 2.0,
3.0, then
max_func ff(v);
now defines a function ff(x) which returns (x[0] - 1)^2 + (x[1] - 2)
^2+ (x[2] - 3)^2
How do I create a function pointer, say ffpointer, in my C++ code that
refers to ff, and which I can pass to my C minimizer?
Thanks for any suggestions.
Richard Stanton
the old C routines that I've been using for 20+ years. One of these is
a general purpose function minimizer, which expects to be passed a
pointer to the function to be minimized,
double func(double p[])
How do I use this to minimize a function defined within a class in C+
+? To be specific, suppose I define the following class, which returns
the sum of squared differences between an input vector and a (private)
vector called trueParms:
class max_func {
public:
max_func(vector <double> trueParms_) : trueParms(trueParms_) {};
double operator()(double p[]);
private:
vector <double> trueParms;
};
double max_func:perator () (double p[])
{
unsigned int i;
double temp;
double ss;
for (i=0, ss=0.0; i< trueParms.size(); i++) {
temp = p - trueParms;
ss += temp * temp;
}
return ss;
};
In the main code, if I have a vector v containing the values 1.0, 2.0,
3.0, then
max_func ff(v);
now defines a function ff(x) which returns (x[0] - 1)^2 + (x[1] - 2)
^2+ (x[2] - 3)^2
How do I create a function pointer, say ffpointer, in my C++ code that
refers to ff, and which I can pass to my C minimizer?
Thanks for any suggestions.
Richard Stanton