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
9#include "../autodiff/autodiff.h"
10#include "../core/iter_result.h"
11
12
13namespace theoretica {
14
15
29 template <
30 typename Vector = vec<real>,
31 typename ReturnVector = Vector,
32 typename DualObjectiveFunction,
33 autodiff::enable_vector_field<DualObjectiveFunction> = true
34 >
40 ) {
41
42 // Current position
44
45 // Number of iterations
46 unsigned int iter = 0;
47
48 // Extract the size of the vector type
49 constexpr size_t N = ReturnVector::size_argument;
50
51 // The current value of f(x)
54 );
55
56 // Jacobian matrix
58
59 while(f_x.sqr_norm() > square(tolerance) && iter <= max_iter) {
60
61 // Compute the function value and Jacobian
62 // using automatic differentiation
65 );
66
67 // Update the current best guess
68 x -= algebra::solve(J, f_x);
69 iter++;
70 }
71
72 if(iter > max_iter) {
73
74 TH_MATH_ERROR("multiroot_newton", iter, MathError::NoConvergence);
78 );
79 }
80
82 }
83
84}
85
86#endif
A generic matrix with a fixed number of rows and columns.
Definition mat.h:136
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:417
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
#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
Vector solve(const Matrix &A, const Vector &b)
Solve the linear system , finding using the best available algorithm.
Definition algebra.h:1996
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
Vector make_error()
Create a vector representing an error state, with all NaN values.
Definition algebra.h:103
iter_result< ReturnVector > multiroot_newton(DualObjectiveFunction 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:35
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.
dual2 square(dual2 x)
Return the square of a second order dual number.
Definition dual2_functions.h:23
@ MaxIterations
Maximum iterations exceeded.
A structure returned by iterative algorithms containing the computed value, convergence information,...
Definition iter_result.h:45