/src/quantlib/ql/math/integrals/expsinhintegral.hpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2023 Klaus Spanderen |
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 expsinhintegral.hpp |
21 | | */ |
22 | | |
23 | | #ifndef quantlib_exp_sinh_integral_hpp |
24 | | #define quantlib_exp_sinh_integral_hpp |
25 | | |
26 | | #include <ql/types.hpp> |
27 | | #include <ql/utilities/null.hpp> |
28 | | #include <ql/math/integrals/integral.hpp> |
29 | | |
30 | | #if BOOST_VERSION >= 106900 |
31 | | #define QL_BOOST_HAS_EXP_SINH |
32 | | |
33 | | #include <boost/math/quadrature/exp_sinh.hpp> |
34 | | #include <limits> |
35 | | |
36 | | namespace QuantLib { |
37 | | /*! The exp-sinh quadrature routine provided by boost is a rapidly convergent |
38 | | numerical integration scheme for holomorphic integrands. The tolerance |
39 | | is used against the error estimate for the L1 norm of the integral. |
40 | | */ |
41 | | class ExpSinhIntegral : public Integrator { |
42 | | public: |
43 | | explicit ExpSinhIntegral( |
44 | | Real relTolerance = std::sqrt(std::numeric_limits<Real>::epsilon()), |
45 | | Size maxRefinements = 9) |
46 | 0 | : Integrator(QL_MAX_REAL, Null<Size>()), |
47 | 0 | relTolerance_(relTolerance), |
48 | 0 | exp_sinh_(maxRefinements) {} |
49 | | |
50 | | // For half-infinite interval [0, \inf), the exp-sinh quadrature is provided by |
51 | 0 | Real integrate(const std::function<Real(Real)>& f) const { |
52 | 0 | setNumberOfEvaluations(0); |
53 | 0 | Real error; |
54 | 0 | Real value = exp_sinh_.integrate( |
55 | 0 | [this, &f](Real x) -> Real { increaseNumberOfEvaluations(1); return f(x); }, |
56 | 0 | relTolerance_, &error); |
57 | 0 | setAbsoluteError(error); |
58 | 0 |
|
59 | 0 | return value; |
60 | 0 | } |
61 | | |
62 | | protected: |
63 | | Real integrate(const std::function<Real(Real)>& f, Real a, Real b) |
64 | 0 | const override { |
65 | 0 | Real error; |
66 | 0 | Real value = exp_sinh_.integrate( |
67 | 0 | [this, &f](Real x) -> Real { |
68 | 0 | increaseNumberOfEvaluations(1); |
69 | 0 | return f(x); |
70 | 0 | }, |
71 | 0 | a, b, relTolerance_, &error); |
72 | 0 | setAbsoluteError(error); |
73 | |
|
74 | 0 | return value; |
75 | 0 | } |
76 | | |
77 | | private: |
78 | | const Real relTolerance_; |
79 | | mutable boost::math::quadrature::exp_sinh<Real> exp_sinh_; |
80 | | }; |
81 | | } |
82 | | |
83 | | #else |
84 | | |
85 | | namespace QuantLib { |
86 | | |
87 | | class ExpSinhIntegral : public Integrator { |
88 | | public: |
89 | | explicit ExpSinhIntegral( Real relTolerance = 0, Size maxRefinements = 0) |
90 | | : Integrator(Null<Real>(), Null<Size>()) { |
91 | | QL_FAIL("boost version 1.69 or higher is required in order to use ExpSinhIntegral"); |
92 | | } |
93 | | |
94 | | Real integrate(const std::function<Real(Real)>& f) const { |
95 | | QL_FAIL("boost version 1.69 or higher is required in order to use ExpSinhIntegral"); |
96 | | } |
97 | | |
98 | | protected: |
99 | | Real integrate(const std::function<Real(Real)>& f, Real a, Real b) const override { |
100 | | QL_FAIL("boost version 1.69 or higher is required in order to use ExpSinhIntegral"); |
101 | | } |
102 | | }; |
103 | | |
104 | | } |
105 | | |
106 | | #endif |
107 | | |
108 | | #endif |