Theoretica
A C++ numerical and automatic mathematical library
extrema.h
Go to the documentation of this file.
1 
5 
6 #ifndef THEORETICA_EXTREMA_H
7 #define THEORETICA_EXTREMA_H
8 
9 #include "../core/constants.h"
10 #include "./roots.h"
11 
12 
13 namespace theoretica {
14 
15 
23  template<typename RealFunction>
24  inline real maximize_goldensection(RealFunction f, real a, real b) {
25 
26  if(a > b) {
27  TH_MATH_ERROR("maximize_goldensection", b, INVALID_ARGUMENT);
28  return nan();
29  }
30 
31  real x1 = a;
32  real x2 = b;
33  real x3 = b - (b - a) / PHI;
34  real x4 = a + (b - a) / PHI;
35 
36  unsigned int iter = 0;
37 
38  while(abs(x2 - x1) > OPTIMIZATION_TOL && iter <= OPTIMIZATION_GOLDENSECTION_ITER) {
39 
40  if(f(x3) > f(x4)) {
41  x2 = x4;
42  } else {
43  x1 = x3;
44  }
45 
46  x3 = x2 - (x2 - x1) / PHI;
47  x4 = x1 + (x2 - x1) / PHI;
48 
49  iter++;
50  }
51 
53  TH_MATH_ERROR("maximize_goldensection", iter, NO_ALGO_CONVERGENCE);
54  return nan();
55  }
56 
57  return (x2 + x1) / 2.0;
58  }
59 
60 
68  template<typename RealFunction>
69  inline real minimize_goldensection(RealFunction f, real a, real b) {
70 
71  if(a > b) {
72  TH_MATH_ERROR("minimize_goldensection", b, INVALID_ARGUMENT);
73  return nan();
74  }
75 
76  real x1 = a;
77  real x2 = b;
78  real x3 = b - (b - a) / PHI;
79  real x4 = a + (b - a) / PHI;
80 
81  unsigned int iter = 0;
82 
83  while(abs(x2 - x1) > OPTIMIZATION_TOL && iter <= OPTIMIZATION_GOLDENSECTION_ITER) {
84 
85  if(f(x3) < f(x4)) {
86  x2 = x4;
87  } else {
88  x1 = x3;
89  }
90 
91  x3 = x2 - (x2 - x1) / PHI;
92  x4 = x1 + (x2 - x1) / PHI;
93 
94  iter++;
95  }
96 
98  TH_MATH_ERROR("minimize_goldensection", iter, NO_ALGO_CONVERGENCE);
99  return nan();
100  }
101 
102  return (x2 + x1) / 2.0;
103  }
104 
105 
115  template<typename RealFunction>
117  RealFunction f, RealFunction Df, RealFunction D2f,
118  real guess = 0) {
119 
120  real z = root_newton(Df, D2f, guess);
121 
122  if(D2f(z) > 0) {
123  TH_MATH_ERROR("maximize_newton", z, NO_ALGO_CONVERGENCE);
124  return nan();
125  }
126 
127  return z;
128  }
129 
130 
140  template<typename RealFunction>
142  RealFunction f, RealFunction Df,
143  RealFunction D2f, real guess = 0) {
144 
145  real z = root_newton(Df, D2f, guess);
146 
147  if(D2f(z) < 0) {
148  TH_MATH_ERROR("minimize_newton", z, NO_ALGO_CONVERGENCE);
149  return nan();
150  }
151 
152  return z;
153  }
154 
155 
165  template<typename RealFunction>
167  RealFunction f, RealFunction Df,
168  real a, real b) {
169 
170  real z = root_bisection(Df, a, b);
171 
172  if(deriv_central(Df, z) > 0) {
173  TH_MATH_ERROR("maximize_bisection", z, NO_ALGO_CONVERGENCE);
174  return nan();
175  }
176 
177  return z;
178  }
179 
180 
190  template<typename RealFunction>
191  inline real minimize_bisection(RealFunction f, RealFunction Df, real a, real b) {
192 
193  real z = root_bisection(Df, a, b);
194 
195  if(deriv_central(Df, z) < 0) {
196  TH_MATH_ERROR("minimize_bisection", z, NO_ALGO_CONVERGENCE);
197  return z;
198  }
199 
200  return z;
201  }
202 
203 }
204 
205 #endif
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition: error.h:219
Main namespace of the library which contains all functions and objects.
Definition: algebra.h:27
double real
A real number, defined as a floating point type.
Definition: constants.h:188
real minimize_bisection(RealFunction f, RealFunction Df, real a, real b)
Approximate a function minimum inside an interval given the function and its first derivative using b...
Definition: extrema.h:191
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition: dual2_functions.h:183
real maximize_bisection(RealFunction f, RealFunction Df, real a, real b)
Approximate a function maximum inside an interval given the function and its first derivative using b...
Definition: extrema.h:166
constexpr real OPTIMIZATION_TOL
Approximation tolerance for root finding.
Definition: constants.h:278
constexpr unsigned int OPTIMIZATION_GOLDENSECTION_ITER
Maximum number of iterations for the golden section search algorithm.
Definition: constants.h:284
real root_newton(RealFunction f, RealFunction Df, real guess=0)
Approximate a root of an arbitrary function using Newton's method.
Definition: roots.h:100
real deriv_central(RealFunction f, real x, real h=CALCULUS_DERIV_STEP)
Approximate the first derivative of a real function using the central method.
Definition: deriv.h:52
real maximize_goldensection(RealFunction f, real a, real b)
Approximate a function maximum using the Golden Section search algorithm.
Definition: extrema.h:24
real minimize_goldensection(RealFunction f, real a, real b)
Approximate a function minimum using the Golden Section search algorithm.
Definition: extrema.h:69
real maximize_newton(RealFunction f, RealFunction Df, RealFunction D2f, real guess=0)
Approximate a function maximum given the function and the first two derivatives using Newton-Raphson'...
Definition: extrema.h:116
constexpr real PHI
The Phi (Golden Section) mathematical constant.
Definition: constants.h:200
real nan()
Return a quiet NaN number in floating point representation.
Definition: error.h:54
real minimize_newton(RealFunction f, RealFunction Df, RealFunction D2f, real guess=0)
Approximate a function minimum given the function and the first two derivatives using Newton-Raphson'...
Definition: extrema.h:141
real root_bisection(RealFunction f, real a, real b, real tolerance=OPTIMIZATION_TOL)
Approximate a root of an arbitrary function using bisection inside a compact interval [a,...
Definition: roots.h:56
Root approximation of real functions.