Sign in to follow this  
davidcoleman

Boost - brent_find_minima problem

Recommended Posts

davidcoleman    134
This works (although with predefined values)
void CtestDlg::OnBnClickedButton1()
{
	doSomething();
}

double sinc(double x) {
	static const double PI = 3.14159265358979323846264338327950288419716939937510;
	return (x == 0.0) ? 1.0 : sin( PI * x ) / ( PI * x );
}

double x(double t) {
	static double vals[6] = { 5.0, 14.0, 60.0, 111.0, 100.0, 37.0 };
	double result = 0;
	for (int i = 0; i < 6; i++) result += vals[i] * sinc(t-i);
	return -result;
}

typedef std::pair<double, double> Range;

void CtestDlg::doSomething()
{
std::pair<double, double> mm = boost::math::tools::brent_find_minima(x, 0.0, 6.0, 64);
}



But if I make those functions class members and try to pass values like this
void CtestDlg::OnBnClickedButton1()
{
	vals[0]=5.0;
	vals[1]=14.0;
	vals[2]=60.0;
	vals[3]=111.0;
	vals[4]=100.0;
	vals[5]=37.0;
	doSomething();
}

typedef std::pair<double, double> Range;

void CtestDlg::doSomething()
{
std::pair<double, double> mm = boost::math::tools::brent_find_minima(x, 0.0, 6.0, 64);
}

double CtestDlg::sinc(double x) {
	static const double PI = 3.14159265358979323846264338327950288419716939937510;
	return (x == 0.0) ? 1.0 : sin( PI * x ) / ( PI * x );
}

double CtestDlg::x(double t) {
	//static double vals[6] = { 5.0, 14.0, 60.0, 111.0, 100.0, 37.0 }; -THIS CANCLED OUT
	double result = 0;
	for (int i = 0; i < 6; i++) result += vals[i] * sinc(t-i);
	return -result;
}



I get errors: Error 1 error C3867: 'CtestDlg::x': function call missing argument list; use '&CtestDlg::x' to create a pointer to member Error 2 error C2780: 'std::pair<T,T> boost::math::tools::brent_find_minima(F,T,T,int,boost::uintmax_t &)' : expects 5 arguments - 4 provided and if I do type there &CtestDlg::x instead of just x I get error in boost itself Error 1 error C2064: term does not evaluate to a function taking 1 arguments c:\program files\boost\boost_1_39\boost\math\tools\minima.hpp How can I fix it? (The program uses this values to define Y of the curve and mm is the x value where the curve has maximum)

Share this post


Link to post
Share on other sites
demonkoryu    980
The problem is that a member function must be converted to a functor before being able to be passed as a variable. The keyword here is binding (std::bind1st() etc.).

I found a forum topic elsewhere that has an answer to exactly your question: http://www.velocityreviews.com/forums/t690259-function-that-requires-a-function-as-an-argument-brent_find_minima.html

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this