kyopro-lib

This documentation is automatically generated by online-judge-tools/verification-helper

View on GitHub

:x: Golden section search (Convex downwards)
(Mylib/Algorithm/golden_section_search_downwards.cpp)

Operations

Requirements

Notes

Problems

References

Verified with

Code

#pragma once
#include <cmath>
#include <functional>

namespace haar_lib {
  template <typename T = double, typename Func = std::function<T(T)>>
  T golden_section_search_downwards(T lb, T ub, const Func &f, int LOOP_COUNT = 100) {
    static const T phi = (1.0 + std::sqrt(5)) / 2;

    T t1 = 0, t2 = 0;

    while (LOOP_COUNT--) {
      t1 = (lb * phi + ub) / (phi + 1.0);
      t2 = (lb + ub * phi) / (phi + 1.0);

      if (f(t1) < f(t2)) {
        ub = t2;
      } else {
        lb = t1;
      }
    }

    return lb;
  }
}  // namespace haar_lib
#line 2 "Mylib/Algorithm/golden_section_search_downwards.cpp"
#include <cmath>
#include <functional>

namespace haar_lib {
  template <typename T = double, typename Func = std::function<T(T)>>
  T golden_section_search_downwards(T lb, T ub, const Func &f, int LOOP_COUNT = 100) {
    static const T phi = (1.0 + std::sqrt(5)) / 2;

    T t1 = 0, t2 = 0;

    while (LOOP_COUNT--) {
      t1 = (lb * phi + ub) / (phi + 1.0);
      t2 = (lb + ub * phi) / (phi + 1.0);

      if (f(t1) < f(t2)) {
        ub = t2;
      } else {
        lb = t1;
      }
    }

    return lb;
  }
}  // namespace haar_lib
Back to top page