QuantLib
: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
ql
math
solvers1d
newton.hpp
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
5
6
This file is part of QuantLib, a free-software/open-source library
7
for financial quantitative analysts and developers - http://quantlib.org/
8
9
QuantLib is free software: you can redistribute it and/or modify it
10
under the terms of the QuantLib license. You should have received a
11
copy of the license along with this program; if not, please email
12
<quantlib-dev@lists.sf.net>. The license is also available online at
13
<http://quantlib.org/license.shtml>.
14
15
This program is distributed in the hope that it will be useful, but WITHOUT
16
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17
FOR A PARTICULAR PURPOSE. See the license for more details.
18
*/
19
24
#ifndef quantlib_solver1d_newton_h
25
#define quantlib_solver1d_newton_h
26
27
#include <ql/math/solvers1d/newtonsafe.hpp>
28
29
namespace
QuantLib
{
30
32
40
class
Newton
:
public
Solver1D
<Newton> {
41
public
:
42
template
<
class
F>
43
Real
solveImpl
(
const
F& f,
44
Real
xAccuracy)
const
{
45
46
/* The implementation of the algorithm was inspired by
47
Press, Teukolsky, Vetterling, and Flannery,
48
"Numerical Recipes in C", 2nd edition,
49
Cambridge University Press
50
*/
51
52
Real
froot, dfroot, dx;
53
54
froot = f(
root_
);
55
dfroot = f.derivative(
root_
);
56
QL_REQUIRE(dfroot !=
Null<Real>
(),
57
"Newton requires function's derivative"
);
58
++
evaluationNumber_
;
59
60
while
(
evaluationNumber_
<=
maxEvaluations_
) {
61
dx = froot/dfroot;
62
root_
-= dx;
63
// jumped out of brackets, switch to NewtonSafe
64
if
((
xMin_
-
root_
)*(
root_
-
xMax_
) < 0.0) {
65
NewtonSafe
s;
66
s.setMaxEvaluations(
maxEvaluations_
-
evaluationNumber_
);
67
return
s.solve(f, xAccuracy,
root_
+dx,
xMin_
,
xMax_
);
68
}
69
if
(std::fabs(dx) < xAccuracy) {
70
f(
root_
);
71
++
evaluationNumber_
;
72
return
root_
;
73
}
74
froot = f(
root_
);
75
dfroot = f.derivative(
root_
);
76
++
evaluationNumber_
;
77
}
78
79
QL_FAIL(
"maximum number of function evaluations ("
80
<<
maxEvaluations_
<<
") exceeded"
);
81
}
82
};
83
84
}
85
86
#endif
QuantLib::Newton
Newton 1-D solver
Definition:
newton.hpp:40
QuantLib::Newton::solveImpl
Real solveImpl(const F &f, Real xAccuracy) const
Definition:
newton.hpp:43
QuantLib::NewtonSafe
safe Newton 1-D solver
Definition:
newtonsafe.hpp:40
QuantLib::Null
template class providing a null value for a given type.
Definition:
null.hpp:76
QuantLib::Solver1D
Base class for 1-D solvers.
Definition:
solver1d.hpp:67
QuantLib::Solver1D< Newton >::evaluationNumber_
Size evaluationNumber_
Definition:
solver1d.hpp:227
QuantLib::Solver1D< Newton >::xMin_
Real xMin_
Definition:
solver1d.hpp:225
QuantLib::Solver1D< Newton >::root_
Real root_
Definition:
solver1d.hpp:225
QuantLib::Solver1D< Newton >::xMax_
Real xMax_
Definition:
solver1d.hpp:225
QuantLib::Solver1D< Newton >::maxEvaluations_
Size maxEvaluations_
Definition:
solver1d.hpp:226
QuantLib::Real
QL_REAL Real
real number
Definition:
types.hpp:50
QuantLib
Definition:
any.hpp:35
Generated by
Doxygen
1.9.5