SOPT
Sparse OPTimisation
bisection_method.cc
Go to the documentation of this file.
1 #include <catch2/catch_all.hpp>
2 #include <random>
3 
4 #include <Eigen/Dense>
5 
7 #include "sopt/types.h"
8 
12 
13 constexpr Scalar a = -0.5;
14 constexpr Scalar b = 1.;
15 constexpr Scalar tol = 1e-4;
16 TEST_CASE("Bisection x^3") {
17  using namespace sopt;
18  sopt::logging::set_level("debug");
19  std::function<Scalar(Scalar)> const func = [](const Scalar &x) -> Scalar { return x * x * x; };
20  constexpr Scalar x0 = 0;
21  const Scalar x0_est = bisection_method(func(x0), func, a, b, tol);
22  CHECK(std::abs(x0_est - x0) <= tol);
23 }
24 TEST_CASE("Bisection f(x) = x") {
25  using namespace sopt;
26  std::function<Scalar(Scalar)> const func = [](const Scalar &x) -> Scalar { return x; };
27  constexpr Scalar x0 = 0.23235104239409;
28  const Scalar x0_est = bisection_method(func(x0), func, a, b, tol);
29  CHECK(std::abs(x0_est - x0) <= tol);
30 }
31 TEST_CASE("Bisection exp()") {
32  using namespace sopt;
33  std::function<Scalar(Scalar)> const func = [](const Scalar &x) -> Scalar { return std::exp(-x); };
34  constexpr Scalar x0 = 1;
35  const Scalar x0_est = bisection_method(func(x0), func, a, b, tol);
36  CHECK(std::abs(x0_est - x0) <= tol);
37 }
TEST_CASE("Bisection x^3")
sopt::Vector< Scalar > t_Vector
constexpr Scalar tol
constexpr Scalar b
sopt::t_real Scalar
constexpr Scalar a
sopt::Matrix< Scalar > t_Matrix
void set_level(const std::string &level)
Method to set the logging level of the default Log object.
Definition: logging.h:154
double t_real
Root of the type hierarchy for real numbers.
Definition: types.h:17
std::enable_if< std::is_same< t_real, K >::value, K >::type bisection_method(const K &function_value, const std::function< K(K)> &func, const K &a, const K &b, const t_real &rel_convergence=1e-4)
Find root to a function within an interval.
Eigen::Matrix< T, Eigen::Dynamic, 1 > Vector
A vector of a given type.
Definition: types.h:24
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > Matrix
A matrix of a given type.
Definition: types.h:29