Theoretica
Scientific Computing
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
10#include "../core/iter_result.h"
11
12
13namespace theoretica {
14
15
29#ifdef THEORETICA_HAS_CPP20
30 template <
31 VectorType Vector,
32 VectorType ReturnVector = Vector,
33 autodiff::ADVectorField VectorField
34 >
35#else
36 template <
37 typename Vector = vec<real>,
38 typename ReturnVector = Vector,
39 typename VectorField,
40 autodiff::enable_vector_field<VectorField> = true
41 >
42#endif
44 VectorField f,
45 Vector guess,
47 unsigned int max_iter = OPTIMIZATION_MINGRAD_ITER
48 ) {
49
50 // Current position
51 ReturnVector x = guess;
52
53 // Number of iterations
54 unsigned int iter = 0;
55
56 // Extract the size of the vector type
57 constexpr size_t N = ReturnVector::size_argument;
58
59 // The current value of f(x)
60 ReturnVector f_x = multidual<N>::extract_real(
62 );
63
64 // Jacobian matrix
66
67 while(f_x.sqr_norm() > square(tolerance) && iter <= max_iter) {
68
69 // Compute the function value and Jacobian
70 // using automatic differentiation
72 f(multidual<N>::make_argument(x)), f_x, J
73 );
74
75 // Update the current best guess
76 x -= algebra::solve(J, f_x);
77 iter++;
78 }
79
80 if(iter > max_iter) {
81
82 TH_MATH_ERROR("multiroot_newton", iter, MathError::NoConvergence);
86 );
87 }
88
89 return iter_result<ReturnVector>(x, iter, algebra::norm(f_x));
90 }
91
92}
93
94#endif
Differential operators using automatic differentiation.
A generic matrix with a fixed number of rows and columns.
Definition mat.h:136
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:417
static vec< multidual< N >, N > make_argument(const Vector &x)
Construct an N-dimensional vector of multidual numbers to be passed as argument to a multidual functi...
Definition multidual.h:401
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:450
A statically allocated N-dimensional vector with elements of the given type.
Definition vec.h:92
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compilation op...
Definition error.h:219
Structured results for iterative algorithms, containing information about convergence.
Vector solve(const Matrix &A, const Vector &b)
Solve the linear system , finding using the best available algorithm.
Definition algebra.h:1993
Vector & vec_error(Vector &v)
Overwrite the given vector with the error vector with NaN values.
Definition algebra.h:58
auto norm(const Vector &v)
Returns the Euclidean/Hermitian norm of the given vector.
Definition algebra.h:395
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:207
iter_result< ReturnVector > multiroot_newton(VectorField f, Vector guess, 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:43
constexpr real OPTIMIZATION_MINGRAD_TOLERANCE
Default tolerance for gradient descent minimization.
Definition constants.h:333
constexpr unsigned int OPTIMIZATION_MINGRAD_ITER
Maximum number of iterations for gradient descent minimization.
Definition constants.h:336
@ NoConvergence
Algorithm did not converge.
Definition error.h:31
dual2 square(dual2 x)
Return the square of a second order dual number.
Definition dual2_functions.h:23
@ MaxIterations
Maximum iterations exceeded.
Definition iter_result.h:25
A structure returned by iterative algorithms containing the computed value, convergence information,...
Definition iter_result.h:45