QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
secant.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_secant_h
25#define quantlib_solver1d_secant_h
26
27#include <ql/math/solver1d.hpp>
28
29namespace QuantLib {
30
32
37 class Secant : public Solver1D<Secant> {
38 public:
39 template <class F>
40 Real solveImpl(const F& f,
41 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,
46 Cambridge University Press
47 */
48
49 Real fl, froot, dx, xl;
50
51 // Pick the bound with the smaller function value
52 // as the most recent guess
53 if (std::fabs(fxMin_) < std::fabs(fxMax_)) {
54 root_ = xMin_;
55 froot = fxMin_;
56 xl = xMax_;
57 fl = fxMax_;
58 } else {
59 root_ = xMax_;
60 froot = fxMax_;
61 xl = xMin_;
62 fl = fxMin_;
63 }
65 dx = (xl-root_)*froot/(froot-fl);
66 xl = root_;
67 fl = froot;
68 root_ += dx;
69 froot = f(root_);
71 if (std::fabs(dx) < xAccuracy || (close(froot, 0.0)))
72 return root_;
73 }
74 QL_FAIL("maximum number of function evaluations ("
75 << maxEvaluations_ << ") exceeded");
76 }
77 };
78
79}
80
81#endif
Secant 1-D solver
Definition: secant.hpp:37
Real solveImpl(const F &f, Real xAccuracy) const
Definition: secant.hpp:40
Base class for 1-D solvers.
Definition: solver1d.hpp:67
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35
bool close(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:163