chemmisol 0.1
Loading...
Searching...
No Matches
Public Member Functions | List of all members
chemmisol::Newton< X, M, G > Class Template Reference

#include <newton.h>

Public Member Functions

 Newton (const X &x0, std::function< X(const X &)> f, std::function< M(const X &)> df)
 
X solve_eps (float epsilon) const
 
X solve_iter (std::size_t n) const
 

Detailed Description

template<typename X, typename M, X(&)(const X &) G = identity>
class chemmisol::Newton< X, M, G >

Implementation of the Newton-Raphson method. The algorithm is generalized to solve systems of equations of k variables and k functions.

The algorithm recursively refines the x value using the following formulae:

x_n+1 = G(x_n - df(x_n)^-1 * f(x_n))

where f denotes the function to solve so that f(x)=0, df denotes the Jacobian matrix of f, and G is a function that takes x as parameter and returns a vector of the same size.

By default, G is the identity(), what allows to implement the classical Newton-Raphson method.

When G is set to abs(), the so-called AbsoluteNewton method is implemented.

The convergence is guaranteed only when the initial value is "close enough" to the desired solution.

Template Parameters
XVector type of size n that defines the argument type and return value of the function f.
MMatrix type of size n*n used to represent the Jacobian matrix of f, as returned by df.
GFunction applied to current x at the end of each iteration of the algorithm.

Constructor & Destructor Documentation

◆ Newton()

template<typename X , typename M , X(&)(const X &) G = identity>
chemmisol::Newton< X, M, G >::Newton ( const X x0,
std::function< X(const X &)>  f,
std::function< M(const X &)>  df 
)
inline

Defines a Newton solver.

Parameters
x0Initial x value, that should be "close enough" to the solution to guarantee convergence.
fFunction to solve so that f(x)=0.
dfDerivative of f, that returns the values of the Jacobian matrix of f for a specified x.

Member Function Documentation

◆ solve_eps()

template<typename X , typename M , X(&)(const X &) G>
X chemmisol::Newton< X, M, G >::solve_eps ( float  epsilon) const

Runs the solver until f(x) < eps for n iterations and returns the found value of x such that f(x) < eps.

Warning
The algorithm is not guaranteed to converge if the initial x0 is not close enough to the solution.
Parameters
epsilonRequired precision.

◆ solve_iter()

template<typename X , typename M , X(&)(const X &) G>
X chemmisol::Newton< X, M, G >::solve_iter ( std::size_t  n) const

Runs the solver for n iterations and returns the found value of x such that f(x)=0.

Warning
The algorithm is not guaranteed to converge if the initial x0 is not close enough to the solution.

The documentation for this class was generated from the following file: