66 Function function,
double lower,
double upper,
double guess,
const RootOptions& options = {})
68 if (!(lower <= upper) || !std::isfinite(lower) || !std::isfinite(upper)) {
75 if (options.check_bracket) {
76 const double at_lower =
static_cast<RootSample>(function(lo)).first;
77 const double at_upper =
static_cast<RootSample>(function(hi)).first;
78 if (!std::isfinite(at_lower) || !std::isfinite(at_upper)) {
81 if (at_lower > 0.0 || at_upper < 0.0) {
84 if (at_lower == 0.0 || at_upper == 0.0) {
89 double x = std::clamp(guess, lo, hi);
90 for (uint32_t iteration = 0; iteration < options.max_iterations; ++iteration) {
91 const auto [value, slope] =
static_cast<RootSample>(function(x));
93 if (!std::isfinite(value)) {
100 (value < 0.0 ? lo : hi) = x;
102 const bool usable = slope > 0.0 && std::isfinite(slope);
103 const double step = usable ? value / slope : 0.0;
104 if (usable && std::abs(step) <= options.tolerance * std::max(std::abs(x), options.floor)) {
108 double next = usable ? x - step : lo;
109 if (!usable || next <= lo || next >= hi) {
110 next = 0.5 * (lo + hi);
113 const double reach = options.tolerance * std::max(std::abs(next), options.floor);
114 if (std::abs(next - x) <= reach || hi - lo <= reach) {
RootResult bracketed_newton(Function function, double lower, double upper, double guess, const RootOptions &options={})
Root of an increasing function by Newton steps kept inside a bracket.