Theoretica
A C++ numerical and automatic mathematical library
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 
12 namespace theoretica {
13 
14 
25  template<unsigned int N>
28  vec<real, N> guess = vec<real, N>(0),
30  unsigned int max_iter = OPTIMIZATION_MINGRAD_ITER) {
31 
32  // Current position
33  vec<real, N> x = guess;
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
51  f(multidual<N>::make_argument(x)), f_x, J
52  );
53 
54  // Update the current best guess
55  x = x - J.inverse() * 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
A generic matrix with a fixed number of rows and columns.
Definition: mat.h:132
mat< Type, N, K > inverse() const
Compute the inverse of a generic square matrix.
Definition: mat.h:561
Multidual number algebra for functions of the form .
Definition: multidual.h:26
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
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
A statically allocated N-dimensional vector with elements of the given type.
Definition: vec.h:88
Type sqr_norm() const
Compute the square norm of the vector (v * v)
Definition: vec.h:298
#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
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:188
constexpr real OPTIMIZATION_MINGRAD_TOLERANCE
Default tolerance for gradient descent minimization.
Definition: constants.h:308
constexpr unsigned int OPTIMIZATION_MINGRAD_ITER
Maximum number of iterations for gradient descent minimization.
Definition: constants.h:311
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
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