votca 2024.2-dev
Loading...
Searching...
No Matches
newton_rapson.h
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team
3 * (http://www.votca.org)
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License")
6 *
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20#pragma once
21#ifndef VOTCA_XTP_NEWTON_RAPSON_H
22#define VOTCA_XTP_NEWTON_RAPSON_H
23
24// VOTCA includes
25#include <votca/tools/types.h>
26
27// Local VOTCA includes
28#include "eigen.h"
29
30namespace votca {
31namespace xtp {
32
39template <class Func>
41 public:
43 NewtonRapson(Index max_iterations, double tolerance)
44 : max_iterations_(max_iterations), tolerance_(tolerance) {}
45
46 NewtonRapson(Index max_iterations, double tolerance, double alpha)
47 : max_iterations_(max_iterations), tolerance_(tolerance), alpha_(alpha) {}
48
49 double FindRoot(const Func& f, double x0) {
51 double x = x0;
52 for (iter_ = 0; iter_ < max_iterations_; iter_++) {
53
54 std::pair<double, double> res = f(x);
55 if (std::abs(res.second) < 1e-12) {
57 break;
58 }
59
60 double step = -alpha_ * res.first / res.second;
61 if (std::abs(step) < tolerance_) {
63 break;
64 }
65
66 x += step;
67 }
68
69 return x;
70 }
71
72 Errors getInfo() const { return info_; }
73 Index getIterations() const { return iter_; }
74
75 private:
79 double tolerance_;
80 double alpha_ = 1.0;
81};
82
83} // namespace xtp
84} // namespace votca
85#endif // VOTCA_XTP_NEWTON_RAPSON_H
Newton Rapson rootfinder for 1d functions.
Index getIterations() const
NewtonRapson(Index max_iterations, double tolerance, double alpha)
double FindRoot(const Func &f, double x0)
NewtonRapson(Index max_iterations, double tolerance)
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26