philsupertramp/game-math
Loading...
Searching...
No Matches
newton.h
Go to the documentation of this file.
1
12#pragma once
13#include "../utils.h"
14#include "gaussSeidel.h"
15#include <functional>
16
18using Jacobian = std::function<Matrix<double>(const Matrix<double>&)>;
20using LinearEquation = std::function<Matrix<double>(const Matrix<double>&)>;
21
31std::pair<Matrix<double>, int>
32newton(const LinearEquation& f, const Jacobian& Df, const Matrix<double>& x0, double TOL, int maxIter) {
33 auto m = x0.rows();
34 if(m < 1) {
35 // error
36 }
37 auto x = x0;
38
39 int iter = 0;
40 double r = TOL + 1;
41
42 while(r > TOL && iter < maxIter) {
43 auto D_F = Df(x);
44 auto F = f(x) * -1.0;
45
46 auto delta = gaussSeidel(D_F, F);
47
48 x += delta;
49
50 r = norm(delta);
51 iter += 1;
52 }
53 if(iter == maxIter) {
54 // error max Iter reached, no convergence
55 }
56 return { x, iter };
57}
58
Definition: Matrix.h:42
size_t rows() const
Definition: Matrix.h:193
Matrix< double > gaussSeidel(const Matrix< double > &A, const Matrix< double > &b)
Definition: gaussSeidel.h:29
std::function< Matrix< double >(const Matrix< double > &)> Jacobian
representation of jacobian
Definition: newton.h:18
std::pair< Matrix< double >, int > newton(const LinearEquation &f, const Jacobian &Df, const Matrix< double > &x0, double TOL, int maxIter)
Definition: newton.h:32
std::function< Matrix< double >(const Matrix< double > &)> LinearEquation
representation of linear equation
Definition: newton.h:20
double norm(const Matrix< double > &in)