QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
newton.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 newton.hpp
21 \brief Newton 1-D solver
22*/
23
24#ifndef quantlib_solver1d_newton_h
25#define quantlib_solver1d_newton_h
26
28
29namespace QuantLib {
30
31 //! %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 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");
59
61 dx = froot/dfroot;
62 root_ -= dx;
63 // jumped out of brackets, switch to NewtonSafe
64 if ((xMin_-root_)*(root_-xMax_) < 0.0) {
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_);
72 return root_;
73 }
74 froot = f(root_);
75 dfroot = f.derivative(root_);
77 }
78
79 QL_FAIL("maximum number of function evaluations ("
80 << maxEvaluations_ << ") exceeded");
81 }
82 };
83
84}
85
86#endif
Newton 1-D solver
Definition: newton.hpp:40
Real solveImpl(const F &f, Real xAccuracy) const
Definition: newton.hpp:43
safe Newton 1-D solver
Definition: newtonsafe.hpp:40
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
Safe (bracketed) Newton 1-D solver.
Real F
Definition: sabr.cpp:200