I guess there is a good reason for this, a function from the Boost
library (which finds the minima of given function) has the following
synopsis:
template <class F, class T>
std::pair<T, T> brent_find_minima(F f, T min, T max, int bits);
Calling it the following way works (inside main, with a top-level
function TrickyFunc):
double min = 1;
double max = 100;
int bits = 50;
std::pair<double, double> result = brent_find_minima(TrickyFunc, min,
max, bits);
Calling it with the arguments directly given, compiles and runs 'fine'
but fails to iterate and just returns the upper boundary (max).
std::pair<double, double> result = brent_find_minima(TrickyFunc, 1, 100, bits);
Below is a compilable example (found on the net).
Thanks,
Markus
#include <boost/math/tools/minima.hpp>
#include <iostream>
#include <cmath>
using namespace std;
// Standard C function
double TrickyFunction(double x)
{
return exp(x) + 0.01/x;
}
int main()
{
// Tricky function
double min = 0.0001;
double max = 1.0;
int bits = 50;
boost::uintmax_t maxIter = 1000;
std::pair<double, double> result =
boost::math::tools::brent_find_minima(TrickyFunction, min, max,
bits);//, maxIter);
cout << "Abscissa, value f(x) Tricky function: " << result.first << ",
" << result.second << endl;
return 0;
}
Shouldn't compile really, unless there's something I don't know about std::pair.
But anyway, express the arguments as 1.0 and 100.0.
You need the same numeric type as for the result values.
Cheers & hth.,
- Alf
You don't know about the templated copy-conversion constructor? Please
see [lib.pairs]/1. Now, why it would "just" return "the upper
boundary", I am not sure (don't know what 'brent...' function's effects
are supposed to be).
I created this:
#include <utility>
#include <iostream>
template <class F, class T>
std::pair<T, T> brent_find_minima(F f, T min, T max, int bits)
{
return std::make_pair(min, max);
}
void TrickyFunc()
{
}
int main()
{
int bits = 0;
std::pair<double, double> result =
brent_find_minima(TrickyFunc, 1, 100, bits);
std::cout << "Result is < " << result.first << " , ";
std::cout << result.second << " >\n";
}
, try it.
>
> But anyway, express the arguments as 1.0 and 100.0.
>
> You need the same numeric type as for the result values.
>
>
> Cheers & hth.,
>
> - Alf
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks. Well nothing surprises me anymore. :-)
[snip]
>>
>> But anyway, express the arguments as 1.0 and 100.0.
>>
>> You need the same numeric type as for the result values.
Cheers,
- Alf