Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
RootFinder.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2 * Copyright (c) 2025, Davide Stocco, Mattia Piazza and Enrico Bertolazzi. *
3 * *
4 * The Optimist project is distributed under the BSD 2-Clause License. *
5 * *
6 * Davide Stocco Mattia Piazza Enrico Bertolazzi *
7 * University of Trento University of Trento University of Trento *
8 * davide.stocco@unitn.it mattia.piazza@unitn.it enrico.bertolazzi@unitn.it *
9\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10
11#pragma once
12
13#ifndef OPTIMIST_ROOT_FINDER_HH
14#define OPTIMIST_ROOT_FINDER_HH
15
16#include "Solver.hh"
17
18namespace Optimist
19{
20
24 namespace RootFinder
25 {
26
27 /*\
28 | ____ _ _____ _ _
29 | | _ \ ___ ___ | |_| ___(_)_ __ __| | ___ _ __
30 | | |_) / _ \ / _ \| __| |_ | | '_ \ / _` |/ _ \ '__|
31 | | _ < (_) | (_) | |_| _| | | | | | (_| | __/ |
32 | |_| \_\___/ \___/ \__|_| |_|_| |_|\__,_|\___|_|
33 |
34 \*/
35
45 template <typename Real, Integer N, typename DerivedSolver>
46 class RootFinder : public Solver<Real, N, N, DerivedSolver>
47 {
48 public:
49 // Fancy static assertions (just for fun, don't take it too seriously)
50 static_assert(N != static_cast<Integer>(0),
51 "Are you sure you want to solve a zero-dimensional system of equations?");
52 static_assert(N != static_cast<Integer>(1),
53 "C'mon, let's not kid ourselves. Use a scalar solver...");
54
56
57 static constexpr bool is_rootfinder{true};
58 static constexpr bool is_optimizer{false};
59
60 static constexpr bool requires_function{DerivedSolver::requires_function};
61 static constexpr bool requires_first_derivative{DerivedSolver::requires_first_derivative};
62 static constexpr bool requires_second_derivative{DerivedSolver::requires_second_derivative};
63
65
66 using Solver<Real, N, N, DerivedSolver>::solve;
67
68 // I/O types
70
71 // Derivative types
74
75 // Function types
79
84
89 std::string name() const {return static_cast<const DerivedSolver *>(this)->name_impl();}
90
96
102
107 void max_jacobian_evaluations(Integer t_jacobian_evaluations)
108 {
109 this->max_first_derivative_evaluations(t_jacobian_evaluations);
110 }
111
117
123
128 void max_hessian_evaluations(Integer t_hessian_evaluations)
129 {
130 this->max_first_derivative_evaluations(t_hessian_evaluations);
131 }
132
133 protected:
140 void evaluate_jacobian(JacobianWrapper jacobian, const Vector & x, Matrix & out)
141 {
142 this->evaluate_first_derivative(jacobian, x, out);
143 }
144
151 void evaluate_hessian(HessianWrapper hessian, const Vector & x, Matrix & out)
152 {
153 this->evaluate_second_derivative(hessian, x, out);
154 }
155
156 public:
164 bool solve(FunctionWrapper function, Vector const & x_ini, Vector & x_sol)
165 {
166 #define CMD "Optimist::RootFinder::solve(...): "
167
168 static_assert(DerivedSolver::requires_function,
169 CMD "the solver requires a function.");
170 return static_cast<DerivedSolver *>(this)->solve_impl(function, x_ini, x_sol);
171
172 #undef CMD
173 }
174
183 bool solve(FunctionWrapper function, JacobianWrapper jacobian, Vector const & x_ini, Vector & x_sol)
184 {
185 #define CMD "Optimist::RootFinder::solve(...): "
186
187 static_assert(DerivedSolver::requires_function,
188 CMD "the solver requires a function.");
189 static_assert(DerivedSolver::requires_first_derivative,
190 CMD "the solver requires the first derivative.");
191 return static_cast<DerivedSolver *>(this)->solve_impl(function, jacobian, x_ini, x_sol);
192
193 #undef CMD
194 }
195
206 const & x_ini, Vector & x_sol)
207 {
208 #define CMD "Optimist::RootFinder::solve(...): "
209
210 static_assert(DerivedSolver::requires_function,
211 CMD "the solver requires the function.");
212 static_assert(DerivedSolver::requires_first_derivative,
213 CMD "the solver requires the first derivative.");
214 static_assert(DerivedSolver::requires_second_derivative,
215 CMD "the solver requires the second derivative.");
216 return static_cast<DerivedSolver *>(this)->solve_impl(function, jacobian, hessian, x_ini, x_sol);
217
218 #undef CMD
219 }
220
221 }; // class RootFinder
222
223 } // namespace RootFinder
224
225} // namespace Optimist
226
227#endif // OPTIMIST_ROOT_FINDER_HH
#define OPTIMIST_BASIC_CONSTANTS(Real)
Definition Optimist.hh:70
#define CMD
Class container for the multi-dimensional root finder.
Definition RootFinder.hh:47
static constexpr bool is_optimizer
Definition RootFinder.hh:58
Integer max_hessian_evaluations() const
Definition RootFinder.hh:122
static constexpr bool requires_function
Definition RootFinder.hh:60
RootFinder()
Definition RootFinder.hh:83
typename Solver< Real, N, N, DerivedSolver >::FirstDerivativeWrapper JacobianWrapper
Definition RootFinder.hh:77
typename Solver< Real, N, N, DerivedSolver >::InputType Vector
Definition RootFinder.hh:69
std::string name() const
Definition RootFinder.hh:89
void max_hessian_evaluations(Integer t_hessian_evaluations)
Definition RootFinder.hh:128
Integer max_jacobian_evaluations() const
Definition RootFinder.hh:101
void max_jacobian_evaluations(Integer t_jacobian_evaluations)
Definition RootFinder.hh:107
void evaluate_jacobian(JacobianWrapper jacobian, const Vector &x, Matrix &out)
Definition RootFinder.hh:140
typename Solver< Real, N, N, DerivedSolver >::SecondDerivativeType Tensor
Definition RootFinder.hh:73
static constexpr bool is_rootfinder
Definition RootFinder.hh:57
typename Solver< Real, N, N, DerivedSolver >::FunctionWrapper FunctionWrapper
Definition RootFinder.hh:76
static constexpr bool requires_second_derivative
Definition RootFinder.hh:62
static constexpr bool requires_first_derivative
Definition RootFinder.hh:61
void evaluate_hessian(HessianWrapper hessian, const Vector &x, Matrix &out)
Definition RootFinder.hh:151
typename Solver< Real, N, N, DerivedSolver >::SecondDerivativeWrapper HessianWrapper
Definition RootFinder.hh:78
Integer hessian_evaluations() const
Definition RootFinder.hh:116
bool solve(FunctionWrapper function, Vector const &x_ini, Vector &x_sol)
Definition RootFinder.hh:164
bool solve(FunctionWrapper function, JacobianWrapper jacobian, Vector const &x_ini, Vector &x_sol)
Definition RootFinder.hh:183
typename Solver< Real, N, N, DerivedSolver >::FirstDerivativeType Matrix
Definition RootFinder.hh:72
bool solve(FunctionWrapper function, JacobianWrapper jacobian, HessianWrapper hessian, Vector const &x_ini, Vector &x_sol)
Definition RootFinder.hh:205
Integer jacobian_evaluations() const
Definition RootFinder.hh:95
Class container for the generic root-finding/optimization problem solver.
Definition Solver.hh:43
Integer first_derivative_evaluations() const
Definition Solver.hh:266
typename std::conditional_t< SolInDim==1, Real, Eigen::Vector< Real, SolInDim > > InputType
Definition Solver.hh:53
typename std::function< void(const InputType &, FirstDerivativeType &)> FirstDerivativeWrapper
Definition Solver.hh:67
std::conditional_t< SolInDim==1 &&SolOutDim==1, Real, std::conditional_t< SolInDim==1||SolOutDim==1, Eigen::Matrix< Real, SolInDim, SolInDim >, std::vector< Eigen::Matrix< Real, SolInDim, SolInDim > > > > SecondDerivativeType
Definition Solver.hh:61
Integer max_first_derivative_evaluations() const
Definition Solver.hh:272
typename std::function< void(const InputType &, SecondDerivativeType &)> SecondDerivativeWrapper
Definition Solver.hh:68
typename std::function< void(const InputType &, OutputType &)> FunctionWrapper
Definition Solver.hh:66
void evaluate_first_derivative(FirstDerivativeWrapper function, const InputType &x, FirstDerivativeType &out)
Definition Solver.hh:724
std::conditional_t< SolInDim==1 &&SolOutDim==1, Real, Eigen::Matrix< Real, SolOutDim, SolInDim > > FirstDerivativeType
Definition Solver.hh:60
void evaluate_second_derivative(SecondDerivativeWrapper function, const InputType &x, SecondDerivativeType &out)
Definition Solver.hh:738
Namespace for the Optimist library.
Definition Optimist.hh:87
OPTIMIST_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition Optimist.hh:95