Boost-Geometry-Utils
view release on metacpan or search on metacpan
src/boost/math/tools/toms748_solve.hpp view on Meta::CPAN
class eps_tolerance
{
public:
eps_tolerance(unsigned bits)
{
BOOST_MATH_STD_USING
eps = (std::max)(T(ldexp(1.0F, 1-bits)), T(4 * tools::epsilon<T>()));
}
bool operator()(const T& a, const T& b)
{
BOOST_MATH_STD_USING
return fabs(a - b) <= (eps * (std::min)(fabs(a), fabs(b)));
}
private:
T eps;
};
struct equal_floor
{
equal_floor(){}
template <class T>
bool operator()(const T& a, const T& b)
{
BOOST_MATH_STD_USING
return floor(a) == floor(b);
}
};
struct equal_ceil
{
equal_ceil(){}
template <class T>
bool operator()(const T& a, const T& b)
{
BOOST_MATH_STD_USING
return ceil(a) == ceil(b);
}
};
struct equal_nearest_integer
{
equal_nearest_integer(){}
template <class T>
bool operator()(const T& a, const T& b)
{
BOOST_MATH_STD_USING
return floor(a + 0.5f) == floor(b + 0.5f);
}
};
namespace detail{
template <class F, class T>
void bracket(F f, T& a, T& b, T c, T& fa, T& fb, T& d, T& fd)
{
//
// Given a point c inside the existing enclosing interval
// [a, b] sets a = c if f(c) == 0, otherwise finds the new
// enclosing interval: either [a, c] or [c, b] and sets
// d and fd to the point that has just been removed from
// the interval. In other words d is the third best guess
// to the root.
//
BOOST_MATH_STD_USING // For ADL of std math functions
T tol = tools::epsilon<T>() * 2;
//
// If the interval [a,b] is very small, or if c is too close
// to one end of the interval then we need to adjust the
// location of c accordingly:
//
if((b - a) < 2 * tol * a)
{
c = a + (b - a) / 2;
}
else if(c <= a + fabs(a) * tol)
{
c = a + fabs(a) * tol;
}
else if(c >= b - fabs(b) * tol)
{
c = b - fabs(a) * tol;
}
//
// OK, lets invoke f(c):
//
T fc = f(c);
//
// if we have a zero then we have an exact solution to the root:
//
if(fc == 0)
{
a = c;
fa = 0;
d = 0;
fd = 0;
return;
}
//
// Non-zero fc, update the interval:
//
if(boost::math::sign(fa) * boost::math::sign(fc) < 0)
{
d = b;
fd = fb;
b = c;
fb = fc;
}
else
{
d = a;
fd = fa;
a = c;
fa= fc;
}
}
template <class T>
inline T safe_div(T num, T denom, T r)
{
//
// return num / denom without overflow,
src/boost/math/tools/toms748_solve.hpp view on Meta::CPAN
// Bracket again, and check termination condition:
//
e = d;
fe = fd;
detail::bracket(f, a, b, c, fa, fb, d, fd);
BOOST_MATH_INSTRUMENT_CODE(" a = " << a << " b = " << b);
BOOST_MATH_INSTRUMENT_CODE(" tol = " << T((fabs(a) - fabs(b)) / fabs(a)));
if((0 == --count) || (fa == 0) || tol(a, b))
break;
//
// And finally... check to see if an additional bisection step is
// to be taken, we do this if we're not converging fast enough:
//
if((b - a) < mu * (b0 - a0))
continue;
//
// bracket again on a bisection:
//
e = d;
fe = fd;
detail::bracket(f, a, b, T(a + (b - a) / 2), fa, fb, d, fd);
--count;
BOOST_MATH_INSTRUMENT_CODE("Not converging: Taking a bisection!!!!");
BOOST_MATH_INSTRUMENT_CODE(" a = " << a << " b = " << b);
} // while loop
max_iter -= count;
if(fa == 0)
{
b = a;
}
else if(fb == 0)
{
a = b;
}
return std::make_pair(a, b);
}
template <class F, class T, class Tol>
inline std::pair<T, T> toms748_solve(F f, const T& ax, const T& bx, const T& fax, const T& fbx, Tol tol, boost::uintmax_t& max_iter)
{
return toms748_solve(f, ax, bx, fax, fbx, tol, max_iter, policies::policy<>());
}
template <class F, class T, class Tol, class Policy>
inline std::pair<T, T> toms748_solve(F f, const T& ax, const T& bx, Tol tol, boost::uintmax_t& max_iter, const Policy& pol)
{
max_iter -= 2;
std::pair<T, T> r = toms748_solve(f, ax, bx, f(ax), f(bx), tol, max_iter, pol);
max_iter += 2;
return r;
}
template <class F, class T, class Tol>
inline std::pair<T, T> toms748_solve(F f, const T& ax, const T& bx, Tol tol, boost::uintmax_t& max_iter)
{
return toms748_solve(f, ax, bx, tol, max_iter, policies::policy<>());
}
template <class F, class T, class Tol, class Policy>
std::pair<T, T> bracket_and_solve_root(F f, const T& guess, T factor, bool rising, Tol tol, boost::uintmax_t& max_iter, const Policy& pol)
{
BOOST_MATH_STD_USING
static const char* function = "boost::math::tools::bracket_and_solve_root<%1%>";
//
// Set up inital brackets:
//
T a = guess;
T b = a;
T fa = f(a);
T fb = fa;
//
// Set up invocation count:
//
boost::uintmax_t count = max_iter - 1;
if((fa < 0) == (guess < 0 ? !rising : rising))
{
//
// Zero is to the right of b, so walk upwards
// until we find it:
//
while((boost::math::sign)(fb) == (boost::math::sign)(fa))
{
if(count == 0)
policies::raise_evaluation_error(function, "Unable to bracket root, last nearest value was %1%", b, pol);
//
// Heuristic: every 20 iterations we double the growth factor in case the
// initial guess was *really* bad !
//
if((max_iter - count) % 20 == 0)
factor *= 2;
//
// Now go ahead and move our guess by "factor":
//
a = b;
fa = fb;
b *= factor;
fb = f(b);
--count;
BOOST_MATH_INSTRUMENT_CODE("a = " << a << " b = " << b << " fa = " << fa << " fb = " << fb << " count = " << count);
}
}
else
{
//
// Zero is to the left of a, so walk downwards
// until we find it:
//
while((boost::math::sign)(fb) == (boost::math::sign)(fa))
{
if(fabs(a) < tools::min_value<T>())
{
// Escape route just in case the answer is zero!
max_iter -= count;
max_iter += 1;
return a > 0 ? std::make_pair(T(0), T(a)) : std::make_pair(T(a), T(0));
}
if(count == 0)
policies::raise_evaluation_error(function, "Unable to bracket root, last nearest value was %1%", a, pol);
//
// Heuristic: every 20 iterations we double the growth factor in case the
// initial guess was *really* bad !
//
if((max_iter - count) % 20 == 0)
factor *= 2;
//
// Now go ahead and move are guess by "factor":
//
b = a;
fb = fa;
a /= factor;
fa = f(a);
--count;
BOOST_MATH_INSTRUMENT_CODE("a = " << a << " b = " << b << " fa = " << fa << " fb = " << fb << " count = " << count);
}
}
max_iter -= count;
max_iter += 1;
std::pair<T, T> r = toms748_solve(
f,
(a < 0 ? b : a),
(a < 0 ? a : b),
(a < 0 ? fb : fa),
(a < 0 ? fa : fb),
tol,
count,
pol);
max_iter += count;
BOOST_MATH_INSTRUMENT_CODE("max_iter = " << max_iter << " count = " << count);
return r;
}
template <class F, class T, class Tol>
inline std::pair<T, T> bracket_and_solve_root(F f, const T& guess, const T& factor, bool rising, Tol tol, boost::uintmax_t& max_iter)
{
return bracket_and_solve_root(f, guess, factor, rising, tol, max_iter, policies::policy<>());
}
} // namespace tools
} // namespace math
} // namespace boost
#endif // BOOST_MATH_TOOLS_SOLVE_ROOT_HPP
( run in 1.790 second using v1.01-cache-2.11-cpan-39bf76dae61 )