13#ifndef OPTIMIST_ROOTFINDER_NEWTON_HH
14#define OPTIMIST_ROOTFINDER_NEWTON_HH
19 namespace RootFinder {
37 template <
typename Vector>
38 requires TypeTrait<Vector>::IsEigen &&
39 (!TypeTrait<Vector>::IsFixed || TypeTrait<Vector>::Dimension > 0)
50 std::conditional_t<VectorTrait::IsSparse,
51 Eigen::SparseLU<FirstDerivative>,
52 Eigen::FullPivLU<FirstDerivative>>;
85 template <
typename FunctionLambda,
typename JacobianLambda>
87 JacobianLambda &&jacobian,
90#define CMD "Optimist::RootFinder::Newton::solve(...): "
101 bool damped, success;
102 Scalar residuals, step_norm;
103 Vector x_old, x_new, function_old, function_new, step_old, step_new;
113 CMD "function evaluation failed at the initial point.");
124 CMD "jacobian evaluation failed at iteration "
126 this->m_lu.compute(jacobian_old);
127 if constexpr (VectorTrait::IsSparse) {
129 CMD "jacobian factorization failed.");
130 step_old = -this->m_lu.solve(function_old).eval();
133 CMD "singular Jacobian detected.");
134 step_old = -this->m_lu.solve(function_old);
138 residuals = function_old.norm();
139 step_norm = step_old.norm();
141 this->
info(residuals);
143 if (residuals < tolerance_residuals ||
144 step_norm < tolerance_step_norm) {
151 damped = this->
damp(std::forward<FunctionLambda>(function),
160 "Optimist::RootFinder::Newton::solve(...): damping failed.");
163 x_new = x_old + step_old;
169 CMD "function evaluation failed at iteration "
175 function_old = function_new;
#define OPTIMIST_BASIC_CONSTANTS(Scalar)
Definition Optimist.hh:70
#define OPTIMIST_ASSERT_WARNING(COND, MSG)
Definition Optimist.hh:62
#define OPTIMIST_ASSERT(COND, MSG)
Definition Optimist.hh:48
TypeTrait< Vector > VectorTrait
Definition Newton.hh:46
static constexpr bool RequiresFirstDerivative
Definition Newton.hh:43
typename TypeTrait< Vector >::Scalar Scalar
Definition Newton.hh:47
Factorization m_lu
Definition Newton.hh:57
bool solve_impl(FunctionLambda &&function, JacobianLambda &&jacobian, const Vector &x_ini, Vector &x_sol)
Definition Newton.hh:86
constexpr std::string name_impl() const
Definition Newton.hh:69
static constexpr bool RequiresSecondDerivative
Definition Newton.hh:44
Newton()
Definition Newton.hh:63
std::conditional_t< VectorTrait::IsSparse, Eigen::SparseLU< FirstDerivative >, Eigen::FullPivLU< FirstDerivative > > Factorization
Definition Newton.hh:49
static constexpr bool RequiresFunction
Definition Newton.hh:42
RootFinder()
Definition RootFinder.hh:66
bool evaluate_jacobian(JacobianLambda &&jacobian, const Input &x, FirstDerivative &out)
Definition RootFinder.hh:136
bool damp(FunctionLambda &&function, const Vector &x_old, const Vector &function_old, const Vector &step_old, Vector &x_new, Vector &function_new, Vector &step_new)
Definition SolverBase.hh:1109
void info(Scalar residuals, const std::string ¬es="-")
Definition SolverBase.hh:1232
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_function(FunctionLambda &&function, const Vector &x, Vector &out)
Definition SolverBase.hh:1040
bool m_damped
Definition SolverBase.hh:131
Integer m_max_iterations
Definition SolverBase.hh:121
Scalar m_tolerance
Definition SolverBase.hh:128
void header()
Definition SolverBase.hh:1168
void reset_counters()
Definition SolverBase.hh:1022
Integer m_iterations
Definition SolverBase.hh:120
bool m_verbose
Definition SolverBase.hh:130
bool m_converged
Definition SolverBase.hh:136
void bottom()
Definition SolverBase.hh:1205
Namespace for the Optimist library.
Definition Optimist.hh:90
Definition Optimist.hh:114