QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
newtonsafe.hpp
Go to the documentation of this file.
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
20/*! \file newtonsafe.hpp
21 \brief Safe (bracketed) Newton 1-D solver
22*/
23
24#ifndef quantlib_solver1d_newtonsafe_h
25#define quantlib_solver1d_newtonsafe_h
26
27#include <ql/math/solver1d.hpp>
28
29namespace QuantLib {
30
31 //! safe %Newton 1-D solver
32 /*! \note This solver requires that the passed function object
33 implement a method <tt>Real derivative(Real)</tt>.
34
35 \test the correctness of the returned values is tested by
36 checking them against known good results.
37
38 \ingroup solvers
39 */
40 class NewtonSafe : public Solver1D<NewtonSafe> {
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, dxold;
53 Real xh, xl;
54
55 // Orient the search so that f(xl) < 0
56 if (fxMin_ < 0.0) {
57 xl = xMin_;
58 xh = xMax_;
59 } else {
60 xh = xMin_;
61 xl = xMax_;
62 }
63
64 // the "stepsize before last"
65 dxold = xMax_-xMin_;
66 // it was dxold=std::fabs(xMax_-xMin_); in Numerical Recipes
67 // here (xMax_-xMin_ > 0) is verified in the constructor
68
69 // and the last step
70 dx = dxold;
71
72 froot = f(root_);
73 dfroot = f.derivative(root_);
74 QL_REQUIRE(dfroot != Null<Real>(),
75 "NewtonSafe requires function's derivative");
77
79 // Bisect if (out of range || not decreasing fast enough)
80 if ((((root_-xh)*dfroot-froot)*
81 ((root_-xl)*dfroot-froot) > 0.0)
82 || (std::fabs(2.0*froot) > std::fabs(dxold*dfroot))) {
83
84 dxold = dx;
85 dx = (xh-xl)/2.0;
86 root_=xl+dx;
87 } else {
88 dxold = dx;
89 dx = froot/dfroot;
90 root_ -= dx;
91 }
92 // Convergence criterion
93 if (std::fabs(dx) < xAccuracy) {
94 f(root_);
96 return root_;
97 }
98 froot = f(root_);
99 dfroot = f.derivative(root_);
101 if (froot < 0.0)
102 xl=root_;
103 else
104 xh=root_;
105 }
106
107 QL_FAIL("maximum number of function evaluations ("
108 << maxEvaluations_ << ") exceeded");
109 }
110 };
111
112}
113
114#endif
safe Newton 1-D solver
Definition: newtonsafe.hpp:40
Real solveImpl(const F &f, Real xAccuracy) const
Definition: newtonsafe.hpp:43
template class providing a null value for a given type.
Definition: null.hpp:76
Base class for 1-D solvers.
Definition: solver1d.hpp:67
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35
Real F
Definition: sabr.cpp:200
Abstract 1-D solver class.