50template <concepts::differentiable_function Function>
60template <concepts::single_variate_differentiable_function Function>
75 std::is_same_v<variable_type, typename function_type::jacobian_type>);
92 last_change_ = std::numeric_limits<variable_type>::infinity();
96 function().evaluate_on(variable_);
99 value_norm_ = abs(function().value());
104 change_ = -function().value() / function().jacobian();
105 variable_ += change_;
107 function().evaluate_on(variable_);
111 last_change_ = abs(change_);
112 value_norm_ = abs(function().value());
117 return (iterations() > max_iterations_) ||
118 (last_change() < tol_last_change_) ||
119 (value_norm() < tol_value_norm_);
126 iteration_logger.template append<index_type>(
127 "Iter.", &this_type::iterations);
128 iteration_logger.template append<index_type>(
129 "Eval.", &this_type::evaluations);
130 iteration_logger.template append<variable_type>(
131 "Value", &this_type::value_norm);
132 iteration_logger.template append<variable_type>(
133 "Change", &this_type::last_change);
136 using base_type::function;
155 return function().value();
166 return function().jacobian();
213 "Maximum number of iterations must be a positive integer.");
214 max_iterations_ = val;
227 "Tolerance of last change of the variable must be a "
228 "non-negative value.");
229 tol_last_change_ = val;
242 "Tolerance of the norm of function value must be a "
243 "non-negative value.");
244 tol_value_norm_ = val;
274 static constexpr auto default_tol_last_change =
281 static constexpr auto default_tol_value_norm =
295template <concepts::multi_variate_differentiable_function Function>
317 Eigen::PartialPivLU<typename Function::jacobian_type>;
333 variable_ = variable;
334 last_change_ = std::numeric_limits<scalar_type>::infinity();
338 function().evaluate_on(variable_);
340 value_norm_ = function().value().norm();
345 jacobian_solver_.compute(function().jacobian());
346 change_ = -jacobian_solver_.solve(function().value());
347 variable_ += change_;
349 function().evaluate_on(variable_);
352 last_change_ = change_.norm();
353 value_norm_ = function().value().norm();
358 return (iterations() > max_iterations_) ||
359 (last_change() < tol_last_change_) ||
360 (value_norm() < tol_value_norm_);
367 iteration_logger.template append<index_type>(
368 "Iter.", &this_type::iterations);
369 iteration_logger.template append<index_type>(
370 "Eval.", &this_type::evaluations);
371 iteration_logger.template append<scalar_type>(
372 "Value", &this_type::value_norm);
373 iteration_logger.template append<scalar_type>(
374 "Change", &this_type::last_change);
377 using base_type::function;
396 return function().value();
407 return function().jacobian();
454 "Maximum number of iterations must be a positive integer.");
455 max_iterations_ = val;
468 "Tolerance of last change of the variable must be a "
469 "non-negative value.");
470 tol_last_change_ = val;
483 "Tolerance of the norm of function value must be a "
484 "non-negative value.");
485 tol_value_norm_ = val;
491 variable_type variable_{};
494 variable_type change_{};
512 static constexpr index_type default_max_iterations = 1000;
515 index_type max_iterations_{default_max_iterations};
518 static constexpr auto default_tol_last_change =
519 static_cast<scalar_type
>(1e-6);
525 static constexpr auto default_tol_value_norm =
526 static_cast<scalar_type
>(1e-6);
Class to write logs of iterations.
Class of tags of logs without memory management.
Base class of root-finding algorithms for functions.
typename function_type::variable_type variable_type
Class of Newton-Raphson method.
void iterate()
Iterate the algorithm once.
auto jacobian() const -> std::invoke_result_t< decltype(&function_type::jacobian), const function_type >
Get Jacobian matrix.
void init(const variable_type &variable)
Initialize.
newton_raphson(const function_type &function=function_type())
Constructor.
auto variable() const -> const variable_type &
Get current variable.
typename variable_type::Scalar scalar_type
Type of scalars in variables.
auto iterations() const noexcept -> index_type
Get the number of iterations.
auto tol_last_change(const variable_type &val) -> this_type &
Set tolerance of last change of the variable.
auto last_change() const noexcept -> variable_type
Get the last change of the variable.
auto evaluations() const noexcept -> index_type
Get the number of function evaluations.
auto value_norm() const noexcept -> scalar_type
Get the norm of function value.
auto tol_value_norm(const variable_type &val) -> this_type &
Set tolerance of the norm of function value.
void configure_iteration_logger(logging::iterations::iteration_logger< this_type > &iteration_logger) const
Configure an iteration logger.
auto last_change() const noexcept -> scalar_type
Get the last change of the variable.
auto value_norm() const noexcept -> variable_type
Get the norm of function value.
auto max_iterations(index_type val) -> this_type &
Set maximum number of iterations.
auto is_stop_criteria_satisfied() const -> bool
Determine if stopping criteria of the algorithm are satisfied.
Eigen::PartialPivLU< typename Function::jacobian_type > jacobian_solver_type
Type of solvers of Jacobian matrices.
auto tol_value_norm(const scalar_type &val) -> this_type &
Set tolerance of the norm of function value.
typename function_type::jacobian_type jacobian_type
Type of Jacobian matrices.
auto value() const -> std::invoke_result_t< decltype(&function_type::value), const function_type >
Get current value.
auto tol_last_change(const scalar_type &val) -> this_type &
Set tolerance of last change of the variable.
Class of Newton-Raphson method.
Definition of differentiable_function concept.
Definition of exceptions.
Definition of function_root_finder_base class.
Definition of index_type type.
Definition of iteration_logger class.
Definition of log_tag_view class.
Definition of macros for logging.
Definition of multi_variate_differentiable_function concept.
std::ptrdiff_t index_type
Type of indices in this library.
Namespace of root-finding algorithms.
constexpr auto newton_raphson_tag
Tag of newton_raphson.
Definition of NUM_COLLECT_PRECONDITION macro.
#define NUM_COLLECT_PRECONDITION(CONDITION,...)
Check whether a precondition is satisfied and throw an exception if not.
Definition of single_variate_differentiable_function concept.