QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
tanhsinhintegral.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) 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>
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
36namespace QuantLib {
37 /*! The tanh-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 TanhSinhIntegral : public Integrator {
42 public:
44 Real relTolerance = std::sqrt(std::numeric_limits<Real>::epsilon()),
45 Size maxRefinements = 15,
46 Real minComplement = std::numeric_limits<Real>::min() * 4
47 )
48 : Integrator(QL_MAX_REAL, Null<Size>()),
49 relTolerance_(relTolerance),
50 tanh_sinh_(maxRefinements, minComplement) {}
51
52 protected:
53 Real integrate(const ext::function<Real(Real)>& f, Real a, Real b)
54 const override {
55 Real error;
56 Real value = tanh_sinh_.integrate(
57 [this, f](Real x)-> Real {
59 return f(x);
60 },
61 a, b, relTolerance_, &error);
62 setAbsoluteError(error);
63
64 return value;
65 }
66
67 private:
68 const Real relTolerance_;
69 mutable boost::math::quadrature::tanh_sinh<Real> tanh_sinh_;
70 };
71}
72
73#else
74
75namespace QuantLib {
76
78 public:
79 TanhSinhIntegral(Real relTolerance = 0, Size maxRefinements = 0, Real minComplement = 0)
80 : Integrator(Null<Real>(), Null<Size>()) {
81 QL_FAIL("boost version 1.69 or higher is required in order to use TanhSinhIntegral");
82 }
83
84 protected:
85 Real integrate(const ext::function<Real(Real)>& f, Real a, Real b) const override {
86 QL_FAIL("boost version 1.69 or higher is required in order to use TanhSinhIntegral");
87 }
88 };
89
90}
91
92#endif
93
94#endif
Integrator(Real absoluteAccuracy, Size maxEvaluations)
Definition: integral.cpp:25
void increaseNumberOfEvaluations(Size increase) const
Definition: integral.cpp:67
void setAbsoluteError(Real error) const
Definition: integral.cpp:55
template class providing a null value for a given type.
Definition: null.hpp:76
TanhSinhIntegral(Real relTolerance=0, Size maxRefinements=0, Real minComplement=0)
Real integrate(const ext::function< Real(Real)> &f, Real a, Real b) const override
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
ext::function< Real(Real)> b
#define QL_MAX_REAL
Definition: qldefines.hpp:176
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Integrators base class definition.
Definition: any.hpp:35
null values
Custom types.