• Jason Rhinelander's avatar
    algorithms: add a constrained_{max,min}imum_search · 33e11b3a
    Jason Rhinelander authored
    This lets you numerically find the largest or smallest value satisfying
    some condition.
    
    For example, to determine the highest price that has at least `x` sales:
    
        auto quantity_demanded = [](double p) { return (int)(99 - 2*p); };
        int x = 10;
        double min = 0.0, max = 100.0;
        auto r = eris::constrained_maximum_search(
            [&](double p) { return quantity_demanded(p) >= x; },
            min, max, 0 /* maximum precision */);
        std::cout << std::setprecision(16) << "p = " << r.arg << "\n";
        // p = 44.5
    
    With less precision for `tol_rel` (e.g. the default, 1e-10) you'll
    generally get some number slightly smaller than 44.5.
    33e11b3a
algorithms.hpp 21.8 KB