Theoretica
A C++ numerical and automatic mathematical library
Loading...
Searching...
No Matches
multi_roots.h
Go to the documentation of this file.
1
5
6#ifndef THEORETICA_MULTI_ROOTS_H
7#define THEORETICA_MULTI_ROOTS_H
8
9#include "../autodiff/autodiff.h"
10
11
12namespace theoretica {
13
14
25 template<unsigned int N>
30 unsigned int max_iter = OPTIMIZATION_MINGRAD_ITER) {
31
32 // Current position
34
35 // Number of iterations
36 unsigned int iter = 0;
37
38 // The current value of f(x)
41 );
42
43 // Jacobian matrix
45
46 while(f_x.sqr_norm() > square(tolerance) && iter <= max_iter) {
47
48 // Compute the function value and Jacobian
49 // using automatic differentiation
52 );
53
54 // Update the current best guess
55 x -= algebra::solve(J, f_x);
56 iter++;
57 }
58
59 if(iter > max_iter) {
60 TH_MATH_ERROR("multi_root_newton", iter, NO_ALGO_CONVERGENCE);
61 return vec<real, N>(nan());
62 }
63
64 return x;
65 }
66
67}
68
69#endif
Multidual number algebra for functions of the form .
Definition multidual.h:26
static vec< real, N > extract_real(const vec< multidual< N >, N > &v)
Extract the real vector from a vector of multidual numbers as a vec<real, N>
Definition multidual.h:273
static void extract(const vec< multidual< N >, N > &v, vec< real, N > &x, mat< real, N, N > &J)
Extract the real vector and dual matrix from a vector of multidual numbers as a vec<real,...
Definition multidual.h:306
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition error.h:219
Vector solve(const Matrix &A, const Vector &b)
Solve the linear system , finding using the best available algorithm.
Definition algebra.h:1867
vec< dreal_t< N >, N > dvec_t
Vector type for multivariate automatic differentiation (read "differential vector").
Definition autodiff_types.h:122
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
double real
A real number, defined as a floating point type.
Definition constants.h:198
vec< real, N > multiroot_newton(autodiff::dvec_t< N >(*f)(autodiff::dvec_t< N >), vec< real, N > guess=vec< real, N >(0), real tolerance=OPTIMIZATION_MINGRAD_TOLERANCE, unsigned int max_iter=OPTIMIZATION_MINGRAD_ITER)
Approximate the root of a multivariate function using Newton's method with pure Jacobian.
Definition multi_roots.h:26
std::remove_reference_t< decltype(std::declval< Structure >()[0])> vector_element_t
Extract the type of a vector (or any indexable container) from its operator[].
Definition core_traits.h:134
constexpr real OPTIMIZATION_MINGRAD_TOLERANCE
Default tolerance for gradient descent minimization.
Definition constants.h:324
constexpr unsigned int OPTIMIZATION_MINGRAD_ITER
Maximum number of iterations for gradient descent minimization.
Definition constants.h:327
dual2 square(dual2 x)
Return the square of a second order dual number.
Definition dual2_functions.h:23
real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:54