/src/quantlib/ql/math/solvers1d/bisection.hpp
Line | Count | Source |
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 | | <https://www.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 bisection.hpp |
21 | | \brief bisection 1-D solver |
22 | | */ |
23 | | |
24 | | #ifndef quantlib_solver1d_bisection_h |
25 | | #define quantlib_solver1d_bisection_h |
26 | | |
27 | | #include <ql/math/solver1d.hpp> |
28 | | |
29 | | namespace QuantLib { |
30 | | |
31 | | //! %Bisection 1-D solver |
32 | | /*! \test the correctness of the returned values is tested by |
33 | | checking them against known good results. |
34 | | |
35 | | \ingroup solvers |
36 | | */ |
37 | | class Bisection : public Solver1D<Bisection> { |
38 | | public: |
39 | | template <class F> |
40 | | Real solveImpl(const F& f, |
41 | 0 | Real xAccuracy) const { |
42 | | |
43 | | /* The implementation of the algorithm was inspired by |
44 | | Press, Teukolsky, Vetterling, and Flannery, |
45 | | "Numerical Recipes in C", 2nd edition, Cambridge |
46 | | University Press |
47 | | */ |
48 | |
|
49 | 0 | Real dx, xMid, fMid; |
50 | | |
51 | | // Orient the search so that f>0 lies at root_+dx |
52 | 0 | if (fxMin_ < 0.0) { |
53 | 0 | dx = xMax_-xMin_; |
54 | 0 | root_ = xMin_; |
55 | 0 | } else { |
56 | 0 | dx = xMin_-xMax_; |
57 | 0 | root_ = xMax_; |
58 | 0 | } |
59 | |
|
60 | 0 | while (evaluationNumber_<=maxEvaluations_) { |
61 | 0 | dx /= 2.0; |
62 | 0 | xMid = root_+dx; |
63 | 0 | fMid = f(xMid); |
64 | 0 | ++evaluationNumber_; |
65 | 0 | if (fMid <= 0.0) |
66 | 0 | root_ = xMid; |
67 | 0 | if (std::fabs(dx) < xAccuracy || (close(fMid, 0.0))) { |
68 | 0 | f(root_); |
69 | 0 | ++evaluationNumber_; |
70 | 0 | return root_; |
71 | 0 | } |
72 | 0 | } |
73 | 0 | QL_FAIL("maximum number of function evaluations (" |
74 | 0 | << maxEvaluations_ << ") exceeded"); |
75 | 0 | } Unexecuted instantiation: randomdefaultmodel.cpp:double QuantLib::Bisection::solveImpl<QuantLib::(anonymous namespace)::Root>(QuantLib::(anonymous namespace)::Root const&, double) const Unexecuted instantiation: double QuantLib::Bisection::solveImpl<QuantLib::HaganIrregularSwaptionEngine::Basket>(QuantLib::HaganIrregularSwaptionEngine::Basket const&, double) const |
76 | | }; |
77 | | |
78 | | } |
79 | | |
80 | | #endif |