6#ifndef THEORETICA_MULTI_EXTREMA_H
7#define THEORETICA_MULTI_EXTREMA_H
31#ifdef THEORETICA_HAS_CPP20
34 VectorType ReturnVector = Vector,
35 autodiff::ADScalarField ObjectiveFunction
40 typename ReturnVector = Vector,
41 typename ObjectiveFunction,
42 autodiff::enable_scalar_field<ObjectiveFunction> =
true
53 ReturnVector x = guess;
61 unsigned int iter = 0;
69 }
while(grad.norm() > tolerance && iter <= max_iter);
92#ifdef THEORETICA_HAS_CPP20
95 VectorType ReturnVector = Vector,
96 autodiff::ADScalarField ObjectiveFunction
100 typename Vector = vec<real>,
101 typename ReturnVector = Vector,
102 typename ObjectiveFunction,
103 autodiff::enable_scalar_field<ObjectiveFunction> =
true
114 using ArgType =
typename _internal::func_helper<ObjectiveFunction>::first_arg_type;
117 [f](ArgType x) {
return -f(x); },
135#ifdef THEORETICA_HAS_CPP20
138 VectorType ReturnVector = Vector,
139 autodiff::ADScalarField ObjectiveFunction
143 typename Vector = vec<real>,
144 typename ReturnVector = Vector,
145 typename ObjectiveFunction,
146 autodiff::enable_scalar_field<ObjectiveFunction> =
true
156 ReturnVector x = guess;
158 unsigned int iter = 0;
159 constexpr size_t N = ReturnVector::size_argument;
168 [f, x, grad](
real gamma){
174 if (result.converged())
175 gamma = result.value;
180 }
while(grad.norm() > tolerance && iter < max_iter);
182 if(iter > max_iter) {
202#ifdef THEORETICA_HAS_CPP20
205 VectorType ReturnVector = Vector,
206 autodiff::ADScalarField ObjectiveFunction
210 typename Vector = vec<real>,
211 typename ReturnVector = Vector,
212 typename ObjectiveFunction,
213 autodiff::enable_scalar_field<ObjectiveFunction> =
true
222 using ArgType =
typename _internal::func_helper<ObjectiveFunction>::first_arg_type;
225 [f](ArgType x) {
return -f(x); },
226 guess, tolerance, max_iter
240#ifdef THEORETICA_HAS_CPP20
243 VectorType ReturnVector = Vector,
244 autodiff::ADScalarField ObjectiveFunction
248 typename Vector = vec<real>,
249 typename ReturnVector = Vector,
250 typename ObjectiveFunction,
251 autodiff::enable_scalar_field<ObjectiveFunction> =
true
255 ObjectiveFunction f, Vector guess,
271#ifdef THEORETICA_HAS_CPP20
274 VectorType ReturnVector = Vector,
275 autodiff::ADScalarField ObjectiveFunction
279 typename Vector = vec<real>,
280 typename ReturnVector = Vector,
281 typename ObjectiveFunction,
282 autodiff::enable_scalar_field<ObjectiveFunction> =
true
286 ObjectiveFunction f, Vector guess,
Differential operators using automatic differentiation.
static vec< multidual< N >, N > make_argument(const Vector &x)
Construct an N-dimensional vector of multidual numbers to be passed as argument to a multidual functi...
Definition multidual.h:401
A statically allocated N-dimensional vector with elements of the given type.
Definition vec.h:92
Mathematical constants and default algorithm parameters.
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compilation op...
Definition error.h:219
Extrema approximation of real functions.
Vector & vec_error(Vector &v)
Overwrite the given vector with the error vector with NaN values.
Definition algebra.h:58
auto gradient(Function f, const Vector &x)
Compute the gradient for a given of a scalar field of the form using automatic differentiation.
Definition autodiff.h:126
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
ReturnVector multi_minimize_grad(ObjectiveFunction f, Vector guess, real gamma=OPTIMIZATION_MINGRAD_GAMMA, real tolerance=OPTIMIZATION_MINGRAD_TOLERANCE, unsigned int max_iter=OPTIMIZATION_MINGRAD_ITER)
Find a local minimum of the given multivariate function using fixed-step gradient descent.
Definition multi_extrema.h:45
double real
A real number, defined as a floating point type.
Definition constants.h:207
constexpr real OPTIMIZATION_MINGRAD_TOLERANCE
Default tolerance for gradient descent minimization.
Definition constants.h:333
ReturnVector multi_maximize_lingrad(ObjectiveFunction f, Vector guess, real tolerance=OPTIMIZATION_MINGRAD_TOLERANCE, unsigned int max_iter=OPTIMIZATION_MINGRAD_ITER)
Find a local maximum of the given multivariate function using gradient descent with linear search.
Definition multi_extrema.h:216
ReturnVector multi_minimize_lingrad(ObjectiveFunction f, Vector guess, real tolerance=OPTIMIZATION_MINGRAD_TOLERANCE, unsigned int max_iter=OPTIMIZATION_MINGRAD_ITER)
Find a local minimum of the given multivariate function using gradient descent with linear search.
Definition multi_extrema.h:149
constexpr unsigned int OPTIMIZATION_MINGRAD_ITER
Maximum number of iterations for gradient descent minimization.
Definition constants.h:336
@ InvalidArgument
Invalid argument.
Definition error.h:32
@ NoConvergence
Algorithm did not converge.
Definition error.h:31
ReturnVector multi_maximize_grad(ObjectiveFunction f, Vector guess, real gamma=OPTIMIZATION_MINGRAD_GAMMA, real tolerance=OPTIMIZATION_MINGRAD_TOLERANCE, unsigned int max_iter=OPTIMIZATION_MINGRAD_ITER)
Find a local maximum of the given multivariate function using fixed-step gradient descent.
Definition multi_extrema.h:106
ReturnVector multi_minimize(ObjectiveFunction f, Vector guess, real tolerance=OPTIMIZATION_MINGRAD_TOLERANCE)
Use the best available algorithm to find a local minimum of the given multivariate function.
Definition multi_extrema.h:254
iter_result< real > minimize_golden(RealFunction f, real a, real b, real tolerance=OPTIMIZATION_TOL, unsigned int max_iter=OPTIMIZATION_GOLDENSECTION_ITER)
Approximate a function minimum using the Golden Section search algorithm.
Definition extrema.h:74
constexpr real OPTIMIZATION_MINGRAD_GAMMA
Default step size for gradient descent minimization.
Definition constants.h:330
ReturnVector multi_maximize(ObjectiveFunction f, Vector guess, real tolerance=OPTIMIZATION_MINGRAD_TOLERANCE)
Use the best available algorithm to find a local maximum of the given multivariate function.
Definition multi_extrema.h:285