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 and Enrico Bertolazzi. *
3 * *
4 * The Optimist project is distributed under the BSD 2-Clause License. *
5 * *
6 * Davide Stocco Enrico Bertolazzi *
7 * University of Trento University of Trento *
8 * davide.stocco@unitn.it enrico.bertolazzi@unitn.it *
9\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10
11#pragma once
12
13#ifndef OPTIMIST_ROOTFINDER_HH
14#define OPTIMIST_ROOTFINDER_HH
15
17
18namespace Optimist {
19
23 namespace RootFinder {
24
25 /*\
26 | ____ _ _____ _ _
27 | | _ \ ___ ___ | |_| ___(_)_ __ __| | ___ _ __
28 | | |_) / _ \ / _ \| __| |_ | | '_ \ / _` |/ _ \ '__|
29 | | _ < (_) | (_) | |_| _| | | | | | (_| | __/ |
30 | |_| \_\___/ \___/ \__|_| |_|_| |_|\__,_|\___|_|
31 |
32 \*/
33
42 template <typename T, typename DerivedSolver>
45 class RootFinder : public SolverBase<T, T, DerivedSolver> {
46 public:
47 friend class SolverBase<T, T, DerivedSolver>;
48
49 static constexpr bool IsRootFinder{true};
50 static constexpr bool IsOptimizer{false};
51
52 // Input and output types
53 using Scalar = typename TypeTrait<T>::Scalar;
54 using Input = T;
55 using Output = T;
56
57 // Derivative types
60
62
63
67
72 constexpr std::string name() const {
73 return static_cast<const DerivedSolver *>(this)->name_impl();
74 }
75
83
91
97 void max_jacobian_evaluations(const Integer t_jacobian_evaluations) {
98 this->max_first_derivative_evaluations(t_jacobian_evaluations);
99 }
100
106 return this->first_derivative_evaluations();
107 }
108
116
122 void max_hessian_evaluations(const Integer t_hessian_evaluations) {
123 this->max_first_derivative_evaluations(t_hessian_evaluations);
124 }
125
126 protected:
135 template <typename JacobianLambda>
136 bool evaluate_jacobian(JacobianLambda &&jacobian,
137 const Input &x,
138 FirstDerivative &out) {
139 return this->evaluate_first_derivative(
140 std::forward<JacobianLambda>(jacobian),
141 x,
142 out);
143 }
144
153 template <typename HessianLambda>
154 bool evaluate_hessian(HessianLambda &&hessian,
155 const Input &x,
156 SecondDerivative &out) {
157 return this->evaluate_second_derivative(
158 std::forward<HessianLambda>(hessian),
159 x,
160 out);
161 }
162
163 public:
173 template <typename FunctionLambda>
174 bool solve(FunctionLambda &&function, const Input &x_ini, Output &x_sol) {
175#define CMD "Optimist::RootFinder::solve(...): "
176
177 static_assert(DerivedSolver::RequiresFunction,
178 CMD "the solver requires a function.");
179 return static_cast<DerivedSolver *>(this)->solve_impl(
180 std::forward<FunctionLambda>(function),
181 x_ini,
182 x_sol);
183
184#undef CMD
185 }
186
197 template <typename FunctionLambda, typename JacobianLambda>
198 bool solve(FunctionLambda &&function,
199 JacobianLambda &&jacobian,
200 const Input &x_ini,
201 Output &x_sol) {
202#define CMD "Optimist::RootFinder::solve(...): "
203
204 static_assert(DerivedSolver::RequiresFunction,
205 CMD "the solver requires a function.");
206 static_assert(DerivedSolver::RequiresFirstDerivative,
207 CMD "the solver requires the first derivative.");
208 return static_cast<DerivedSolver *>(this)->solve_impl(
209 std::forward<FunctionLambda>(function),
210 std::forward<JacobianLambda>(jacobian),
211 x_ini,
212 x_sol);
213
214#undef CMD
215 }
216
230 template <typename FunctionLambda,
231 typename JacobianLambda,
232 typename HessianLambda>
233 bool solve(FunctionLambda &&function,
234 JacobianLambda &&jacobian,
235 HessianLambda &&hessian,
236 const Input &x_ini,
237 Output &x_sol) {
238#define CMD "Optimist::RootFinder::solve(...): "
239
240 static_assert(DerivedSolver::RequiresFunction,
241 CMD "the solver requires the function.");
242 static_assert(DerivedSolver::RequiresFirstDerivative,
243 CMD "the solver requires the first derivative.");
244 static_assert(DerivedSolver::RequiresSecondDerivative,
245 CMD "the solver requires the second derivative.");
246 return static_cast<DerivedSolver *>(this)->solve_impl(
247 std::forward<FunctionLambda>(function),
248 std::forward<JacobianLambda>(jacobian),
249 std::forward<HessianLambda>(hessian),
250 x_ini,
251 x_sol);
252
253#undef CMD
254 }
255
256 }; // class RootFinder
257
258 } // namespace RootFinder
259
260} // namespace Optimist
261
262#endif // OPTIMIST_ROOTFINDER_HH
#define OPTIMIST_BASIC_CONSTANTS(Scalar)
Definition Optimist.hh:70
#define CMD
Class container for the multi-dimensional root finder.
Definition RootFinder.hh:45
void max_hessian_evaluations(const Integer t_hessian_evaluations)
Definition RootFinder.hh:122
Integer max_jacobian_evaluations() const
Definition RootFinder.hh:88
bool evaluate_hessian(HessianLambda &&hessian, const Input &x, SecondDerivative &out)
Definition RootFinder.hh:154
bool solve(FunctionLambda &&function, JacobianLambda &&jacobian, HessianLambda &&hessian, const Input &x_ini, Output &x_sol)
Definition RootFinder.hh:233
T Input
Definition RootFinder.hh:54
bool solve(FunctionLambda &&function, const Input &x_ini, Output &x_sol)
Definition RootFinder.hh:174
Integer max_hessian_evaluations() const
Definition RootFinder.hh:113
Integer hessian_evaluations() const
Definition RootFinder.hh:105
T Output
Definition RootFinder.hh:55
static constexpr bool IsRootFinder
Definition RootFinder.hh:49
typename TypeTrait< T >::Scalar Scalar
Definition RootFinder.hh:53
Integer jacobian_evaluations() const
Definition RootFinder.hh:80
constexpr std::string name() const
Definition RootFinder.hh:72
RootFinder()
Definition RootFinder.hh:66
static constexpr bool IsOptimizer
Definition RootFinder.hh:50
bool evaluate_jacobian(JacobianLambda &&jacobian, const Input &x, FirstDerivative &out)
Definition RootFinder.hh:136
void max_jacobian_evaluations(const Integer t_jacobian_evaluations)
Definition RootFinder.hh:97
bool solve(FunctionLambda &&function, JacobianLambda &&jacobian, const Input &x_ini, Output &x_sol)
Definition RootFinder.hh:198
Integer first_derivative_evaluations() const
Definition SolverBase.hh:379
std::conditional_t< InputTrait::IsEigen||OutputTrait::IsEigen, std::conditional_t< InputTrait::IsSparse||OutputTrait::IsSparse, Eigen::SparseMatrix< Scalar >, Eigen::Matrix< Scalar, OutputTrait::Dimension, InputTrait::Dimension > >, Scalar > FirstDerivative
Definition SolverBase.hh:80
bool evaluate_first_derivative(FirstDerivativeLambda &&function, const T &x, FirstDerivative &out)
Definition SolverBase.hh:1061
bool evaluate_second_derivative(SecondDerivativeLambda &&function, const T &x, SecondDerivative &out)
Definition SolverBase.hh:1082
Integer max_first_derivative_evaluations() const
Definition SolverBase.hh:387
std::conditional_t< InputTrait::IsEigen||OutputTrait::IsEigen, std::conditional_t< InputTrait::IsSparse||OutputTrait::IsSparse, std::vector< Eigen::SparseMatrix< Scalar > >, std::vector< Eigen::Matrix< Scalar, OutputTrait::Dimension, InputTrait::Dimension > > >, Scalar > SecondDerivative
Definition SolverBase.hh:88
SolverBase()
Definition SolverBase.hh:142
Namespace for the Optimist library.
Definition Optimist.hh:90
OPTIMIST_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition Optimist.hh:98
Definition Optimist.hh:114