| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2021 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 | <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 tanhsinhintegral.hpp |
| 21 | */ |
| 22 | |
| 23 | #ifndef quantlib_tanh_sinh_integral_hpp |
| 24 | #define quantlib_tanh_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_TANH_SINH |
| 32 | |
| 33 | #include <boost/math/quadrature/tanh_sinh.hpp> |
| 34 | #include <limits> |
| 35 | |
| 36 | namespace QuantLib { |
| 37 | using tanh_sinh = boost::math::quadrature::tanh_sinh<Real>; |
| 38 | |
| 39 | /*! The tanh-sinh quadrature routine provided by boost is a rapidly convergent |
| 40 | numerical integration scheme for holomorphic integrands. The tolerance |
| 41 | is used against the error estimate for the L1 norm of the integral. |
| 42 | */ |
| 43 | class TanhSinhIntegral : public Integrator { |
| 44 | public: |
| 45 | TanhSinhIntegral( |
| 46 | Real relTolerance = std::sqrt(x: std::numeric_limits<Real>::epsilon()), |
| 47 | Size maxRefinements = 15, |
| 48 | Real minComplement = std::numeric_limits<Real>::min() * 4 |
| 49 | ) |
| 50 | : Integrator(QL_MAX_REAL, Null<Size>()), |
| 51 | relTolerance_(relTolerance), |
| 52 | tanh_sinh_(maxRefinements, minComplement) {} |
| 53 | |
| 54 | protected: |
| 55 | Real integrate(const ext::function<Real(Real)>& f, Real a, Real b) |
| 56 | const override { |
| 57 | Real error; |
| 58 | Real value = tanh_sinh_.integrate(f, a, b, tolerance: relTolerance_, error: &error); |
| 59 | setAbsoluteError(error); |
| 60 | |
| 61 | return value; |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | const Real relTolerance_; |
| 66 | mutable tanh_sinh tanh_sinh_; |
| 67 | }; |
| 68 | } |
| 69 | |
| 70 | #else |
| 71 | |
| 72 | namespace QuantLib { |
| 73 | |
| 74 | class TanhSinhIntegral : public Integrator { |
| 75 | public: |
| 76 | TanhSinhIntegral(Real relTolerance = 0, Size maxRefinements = 0, Real minComplement = 0) |
| 77 | : Integrator(Null<Real>(), Null<Size>()) { |
| 78 | QL_FAIL("boost version 1.69 or higher is required in order to use TanhSinhIntegral" ); |
| 79 | } |
| 80 | |
| 81 | protected: |
| 82 | Real integrate(const ext::function<Real(Real)>& f, Real a, Real b) |
| 83 | const override { return 0.0; } |
| 84 | }; |
| 85 | |
| 86 | } |
| 87 | |
| 88 | #endif |
| 89 | |
| 90 | #endif |
| 91 | |