Re: Function that requires a function as an argument (brent_find_minima)?
Markus S wrote:
On 2009-07-06 19:05:24 +0200, Victor Bazarov <v.Abazarov@comAcast.net>
said:
Markus S wrote:
I try to use a function that minimizes another function (namely from
the Boost library: brent_find_minima). When I provide the function to
be minized at the very top level (ie, in the .cpp file) and call
brent_find_minima from the main block, it works fine but when I
create a class and define the function to be minimized in there (and
also call brent_find_minima in another function in that class), I get
this error during compilation:
error: argument of type double (Testclass::)(double) does not match
double (Testclass::*)(double)
I had invoked brent_find_minima with this line (which works when
called in main);
std::pair<double, double> result =
boost::math::tools::brent_find_minima(TrickyF, min, max, bits);
I started with this example:
http://net.pku.edu.cn/~webg/src/paradise/thirdparty/installed/include/boost-1_35/boost/math/tools/minima.hpp
FAQ
5.8.
The sample code was essentially in that linked file but to be verbose
here comes a minimal set of files, compiled with bjam (Jamroot file at
the bottom) using the boost 1.39 library both on Mac OS X 10.5.7 (Intel)
and on Debian (4.0, I think). The error message is the same on both
systems. Everything compiles and runs fine until I make the call to
brent_find_minima in Testclass.cc active (ie, uncomment it).
There is a difference between a function and a non-static member
function, especially AFA conversion to a function pointer is concerned.
A stand-alone function can be converted to a pointer-to-function
transparently. A member function has to be wrapped and the hidden first
argument (the object for which the member function is called) has to be
provided somehow. See 'bind1st' and 'memfun' templates.
Testbrent.hpp:
-------------------
#include <boost/math/tools/minima.hpp>
#include "Testclass.h"
using namespace std;
double TrickyFunction(double x)
{
return (x-5.0);
}
int main()
{
double min = 0.0001;
double max = 1.0;
int bits = 50;
std::pair<double, double> result =
boost::math::tools::brent_find_minima(TrickyFunction, min, max, bits);
return 0;
}
Testclass.h:
----------------
#ifndef Testclass_h
#define Testclass_h Testclass_h
using namespace std;
class Testclass
{
public:
Testclass();
~Testclass();
double TrickyF(double x);
void CallTrickyF();
};
#endif
Testclass.cc:
-----------------
#include "Testclass.h"
#include <boost/math/tools/minima.hpp>
Testclass::Testclass() {}
Testclass::~Testclass() {}
double Testclass::TrickyF(double x)
{
return (x-5.0);
}
void Testclass::CallTrickyF()
{
double temp = 5.0;
double min = 0.0001;
double max = 1.0;
int bits = 50;
// std::pair<double, double> result =
boost::math::tools::brent_find_minima(TrickyF, min, max, bits);
}
Jamroot:
------------
exe testbrent : TestBrent.cpp Testclass.cc
: <include>.
: <include>/usr/local/include/boost-1_39/
;
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask