votca
2024.2-dev
Loading...
Searching...
No Matches
xtp
include
votca
xtp
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
30
namespace
votca
{
31
namespace
xtp {
32
39
template
<
class
Func>
40
class
NewtonRapson
{
41
public
:
42
enum
Errors
{
success
,
smalldenom
,
notconverged
};
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) {
50
info_
=
Errors::notconverged
;
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) < 1
e
-12) {
56
info_
=
Errors::smalldenom
;
57
break
;
58
}
59
60
double
step = -
alpha_
* res.first / res.second;
61
if
(std::abs(step) <
tolerance_
) {
62
info_
=
Errors::success
;
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
:
76
Errors
info_
=
Errors::notconverged
;
77
Index
max_iterations_
;
78
Index
iter_
;
79
double
tolerance_
;
80
double
alpha_
= 1.0;
81
};
82
83
}
// namespace xtp
84
}
// namespace votca
85
#endif
// VOTCA_XTP_NEWTON_RAPSON_H
votca::xtp::NewtonRapson
Newton Rapson rootfinder for 1d functions.
Definition
newton_rapson.h:40
votca::xtp::NewtonRapson::tolerance_
double tolerance_
Definition
newton_rapson.h:79
votca::xtp::NewtonRapson::getIterations
Index getIterations() const
Definition
newton_rapson.h:73
votca::xtp::NewtonRapson::getInfo
Errors getInfo() const
Definition
newton_rapson.h:72
votca::xtp::NewtonRapson::alpha_
double alpha_
Definition
newton_rapson.h:80
votca::xtp::NewtonRapson::NewtonRapson
NewtonRapson(Index max_iterations, double tolerance, double alpha)
Definition
newton_rapson.h:46
votca::xtp::NewtonRapson::FindRoot
double FindRoot(const Func &f, double x0)
Definition
newton_rapson.h:49
votca::xtp::NewtonRapson::NewtonRapson
NewtonRapson(Index max_iterations, double tolerance)
Definition
newton_rapson.h:43
votca::xtp::NewtonRapson::info_
Errors info_
Definition
newton_rapson.h:76
votca::xtp::NewtonRapson::max_iterations_
Index max_iterations_
Definition
newton_rapson.h:77
votca::xtp::NewtonRapson::iter_
Index iter_
Definition
newton_rapson.h:78
votca::xtp::NewtonRapson::Errors
Errors
Definition
newton_rapson.h:42
votca::xtp::NewtonRapson::success
@ success
Definition
newton_rapson.h:42
votca::xtp::NewtonRapson::notconverged
@ notconverged
Definition
newton_rapson.h:42
votca::xtp::NewtonRapson::smalldenom
@ smalldenom
Definition
newton_rapson.h:42
votca::tools::e
@ e
Definition
unitconverter.h:59
votca
base class for all analysis tools
Definition
basebead.h:33
votca::Index
Eigen::Index Index
Definition
types.h:26
types.h
eigen.h
Generated by
1.12.0