QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
gaussianquadratures.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) 2005 Klaus Spanderen
5 Copyright (C) 2005 Gary Kennedy
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21/*! \file gaussianquadratures.hpp
22 \brief Integral of a 1-dimensional function using the Gauss quadratures
23*/
24
25#ifndef quantlib_gaussian_quadratures_hpp
26#define quantlib_gaussian_quadratures_hpp
27
28#include <ql/math/array.hpp>
31
32namespace QuantLib {
33 class GaussianOrthogonalPolynomial;
34
35 //! Integral of a 1-dimensional function using the Gauss quadratures method
36 /*! References:
37 Gauss quadratures and orthogonal polynomials
38
39 G.H. Gloub and J.H. Welsch: Calculation of Gauss quadrature rule.
40 Math. Comput. 23 (1986), 221-230
41
42 "Numerical Recipes in C", 2nd edition,
43 Press, Teukolsky, Vetterling, Flannery,
44
45 \test the correctness of the result is tested by checking it
46 against known good values.
47 */
49 public:
52
53#if defined(__GNUC__) && (__GNUC__ >= 7)
54#pragma GCC diagnostic push
55#pragma GCC diagnostic ignored "-Wnoexcept-type"
56#endif
57
58 template <class F>
59 Real operator()(const F& f) const {
60 Real sum = 0.0;
61 for (Integer i = Integer(order())-1; i >= 0; --i) {
62 sum += w_[i] * f(x_[i]);
63 }
64 return sum;
65 }
66
67#if defined(__GNUC__) && (__GNUC__ >= 7)
68#pragma GCC diagnostic pop
69#endif
70
71 Size order() const { return x_.size(); }
72 const Array& weights() { return w_; }
73 const Array& x() { return x_; }
74
75 protected:
77 };
78
79
80 //! generalized Gauss-Laguerre integration
81 /*! This class performs a 1-dimensional Gauss-Laguerre integration.
82 \f[
83 \int_{0}^{\inf} f(x) \mathrm{d}x
84 \f]
85 The weighting function is
86 \f[
87 w(x;s)=x^s \exp{-x}
88 \f]
89 and \f[ s > -1 \f]
90 */
92 public:
95 };
96
97 //! generalized Gauss-Hermite integration
98 /*! This class performs a 1-dimensional Gauss-Hermite integration.
99 \f[
100 \int_{-\inf}^{\inf} f(x) \mathrm{d}x
101 \f]
102 The weighting function is
103 \f[
104 w(x;\mu)=|x|^{2\mu} \exp{-x*x}
105 \f]
106 and \f[ \mu > -0.5 \f]
107 */
109 public:
110 explicit GaussHermiteIntegration(Size n, Real mu = 0.0)
112 };
113
114 //! Gauss-Jacobi integration
115 /*! This class performs a 1-dimensional Gauss-Jacobi integration.
116 \f[
117 \int_{-1}^{1} f(x) \mathrm{d}x
118 \f]
119 The weighting function is
120 \f[
121 w(x;\alpha,\beta)=(1-x)^\alpha (1+x)^\beta
122 \f]
123 */
125 public:
128 };
129
130 //! Gauss-Hyperbolic integration
131 /*! This class performs a 1-dimensional Gauss-Hyperbolic integration.
132 \f[
133 \int_{-\inf}^{\inf} f(x) \mathrm{d}x
134 \f]
135 The weighting function is
136 \f[
137 w(x)=1/cosh(x)
138 \f]
139 */
141 public:
144 };
145
146 //! Gauss-Legendre integration
147 /*! This class performs a 1-dimensional Gauss-Legendre integration.
148 \f[
149 \int_{-1}^{1} f(x) \mathrm{d}x
150 \f]
151 The weighting function is
152 \f[
153 w(x)=1
154 \f]
155 */
157 public:
160 };
161
162 //! Gauss-Chebyshev integration
163 /*! This class performs a 1-dimensional Gauss-Chebyshev integration.
164 \f[
165 \int_{-1}^{1} f(x) \mathrm{d}x
166 \f]
167 The weighting function is
168 \f[
169 w(x)=(1-x^2)^{-1/2}
170 \f]
171 */
173 public:
176 };
177
178 //! Gauss-Chebyshev integration (second kind)
179 /*! This class performs a 1-dimensional Gauss-Chebyshev integration.
180 \f[
181 \int_{-1}^{1} f(x) \mathrm{d}x
182 \f]
183 The weighting function is
184 \f[
185 w(x)=(1-x^2)^{1/2}
186 \f]
187 */
189 public:
192 };
193
194 //! Gauss-Gegenbauer integration
195 /*! This class performs a 1-dimensional Gauss-Gegenbauer integration.
196 \f[
197 \int_{-1}^{1} f(x) \mathrm{d}x
198 \f]
199 The weighting function is
200 \f[
201 w(x)=(1-x^2)^{\lambda-1/2}
202 \f]
203 */
205 public:
207 : GaussianQuadrature(n, GaussJacobiPolynomial(lambda-0.5, lambda-0.5))
208 {}
209 };
210
211
212 namespace detail {
213 template <class Integration>
215 public:
217
218 ext::shared_ptr<Integration> getIntegration() const { return integration_; }
219
220 private:
221 Real integrate(const ext::function<Real (Real)>& f,
222 Real a,
223 Real b) const override;
224
225 const ext::shared_ptr<Integration> integration_;
226 };
227 }
228
231
234
237
238 //! tabulated Gauss-Legendre quadratures
240 public:
241 explicit TabulatedGaussLegendre(Size n = 20) { order(n); }
242 template <class F>
243 Real operator() (const F& f) const {
244 QL_ASSERT(w_ != nullptr, "Null weights");
245 QL_ASSERT(x_ != nullptr, "Null abscissas");
246 Size startIdx;
247 Real val;
248
249 const Size isOrderOdd = order_ & 1;
250
251 if (isOrderOdd) {
252 QL_ASSERT((n_>0), "assume at least 1 point in quadrature");
253 val = w_[0]*f(x_[0]);
254 startIdx=1;
255 } else {
256 val = 0.0;
257 startIdx=0;
258 }
259
260 for (Size i=startIdx; i<n_; ++i) {
261 val += w_[i]*f( x_[i]);
262 val += w_[i]*f(-x_[i]);
263 }
264 return val;
265 }
266
267 void order(Size);
268 Size order() const { return order_; }
269
270 private:
272
273 const Real* w_;
274 const Real* x_;
276
277 static const Real w6[3];
278 static const Real x6[3];
279 static const Size n6;
280
281 static const Real w7[4];
282 static const Real x7[4];
283 static const Size n7;
284
285 static const Real w12[6];
286 static const Real x12[6];
287 static const Size n12;
288
289 static const Real w20[10];
290 static const Real x20[10];
291 static const Size n20;
292 };
293
294}
295
296#endif
1-D array used in linear algebra.
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:495
Gauss-Chebyshev integration (second kind)
generalized Gauss-Hermite integration
GaussJacobiIntegration(Size n, Real alpha, Real beta)
generalized Gauss-Laguerre integration
orthogonal polynomial for Gaussian quadratures
Integral of a 1-dimensional function using the Gauss quadratures method.
Real operator()(const F &f) const
tabulated Gauss-Legendre quadratures
Real integrate(const ext::function< Real(Real)> &f, Real a, Real b) const override
const ext::shared_ptr< Integration > integration_
ext::shared_ptr< Integration > getIntegration() const
#define QL_ASSERT(condition, message)
throw an error if the given condition is not verified
Definition: errors.hpp:104
ext::function< Real(Real)> b
orthogonal polynomials for gaussian quadratures
QL_REAL Real
real number
Definition: types.hpp:50
QL_INTEGER Integer
integer number
Definition: types.hpp:35
std::size_t Size
size of a container
Definition: types.hpp:58
Integrators base class definition.
Definition: any.hpp:35
detail::GaussianQuadratureIntegrator< GaussChebyshevIntegration > GaussChebyshevIntegrator
detail::GaussianQuadratureIntegrator< GaussChebyshev2ndIntegration > GaussChebyshev2ndIntegrator
detail::GaussianQuadratureIntegrator< GaussLegendreIntegration > GaussLegendreIntegrator
Real beta
Definition: sabr.cpp:200
Real F
Definition: sabr.cpp:200
Real alpha
Definition: sabr.cpp:200