QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
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
80 public:
82 const std::vector<Size>& ns,
83 const std::function<ext::shared_ptr<GaussianQuadrature>(Size)>& genQuad);
84
85 Real operator()(const std::function<Real(Array)>& f) const;
86
87 const Array& weights() const { return weights_; }
88 const std::vector<Array>& x() const { return x_; }
89
90 private:
92 std::vector<Array> x_;
93 };
94
95
96 //! generalized Gauss-Laguerre integration
97 /*! This class performs a 1-dimensional Gauss-Laguerre integration.
98 \f[
99 \int_{0}^{\inf} f(x) \mathrm{d}x
100 \f]
101 The weighting function is
102 \f[
103 w(x;s)=x^s \exp{-x}
104 \f]
105 and \f[ s > -1 \f]
106 */
108 public:
111 };
112
113 //! generalized Gauss-Hermite integration
114 /*! This class performs a 1-dimensional Gauss-Hermite integration.
115 \f[
116 \int_{-\inf}^{\inf} f(x) \mathrm{d}x
117 \f]
118 The weighting function is
119 \f[
120 w(x;\mu)=|x|^{2\mu} \exp{-x*x}
121 \f]
122 and \f[ \mu > -0.5 \f]
123 */
125 public:
126 explicit GaussHermiteIntegration(Size n, Real mu = 0.0)
128 };
129
130 //! Gauss-Jacobi integration
131 /*! This class performs a 1-dimensional Gauss-Jacobi integration.
132 \f[
133 \int_{-1}^{1} f(x) \mathrm{d}x
134 \f]
135 The weighting function is
136 \f[
137 w(x;\alpha,\beta)=(1-x)^\alpha (1+x)^\beta
138 \f]
139 */
141 public:
144 };
145
146 //! Gauss-Hyperbolic integration
147 /*! This class performs a 1-dimensional Gauss-Hyperbolic integration.
148 \f[
149 \int_{-\inf}^{\inf} f(x) \mathrm{d}x
150 \f]
151 The weighting function is
152 \f[
153 w(x)=1/cosh(x)
154 \f]
155 */
157 public:
160 };
161
162 //! Gauss-Legendre integration
163 /*! This class performs a 1-dimensional Gauss-Legendre 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
170 \f]
171 */
173 public:
176 };
177
178 //! Gauss-Chebyshev integration
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-Chebyshev integration (second kind)
195 /*! This class performs a 1-dimensional Gauss-Chebyshev 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)^{1/2}
202 \f]
203 */
205 public:
208 };
209
210 //! Gauss-Gegenbauer integration
211 /*! This class performs a 1-dimensional Gauss-Gegenbauer integration.
212 \f[
213 \int_{-1}^{1} f(x) \mathrm{d}x
214 \f]
215 The weighting function is
216 \f[
217 w(x)=(1-x^2)^{\lambda-1/2}
218 \f]
219 */
221 public:
223 : GaussianQuadrature(n, GaussJacobiPolynomial(lambda-0.5, lambda-0.5))
224 {}
225 };
226
227
228 namespace detail {
229 template <class Integration>
231 public:
233
234 ext::shared_ptr<Integration> getIntegration() const { return integration_; }
235
236 private:
237 Real integrate(const std::function<Real (Real)>& f,
238 Real a,
239 Real b) const override;
240
241 const ext::shared_ptr<Integration> integration_;
242 };
243 }
244
247
250
253
254 //! tabulated Gauss-Legendre quadratures
256 public:
257 explicit TabulatedGaussLegendre(Size n = 20) { order(n); }
258 template <class F>
259 Real operator() (const F& f) const {
260 QL_ASSERT(w_ != nullptr, "Null weights");
261 QL_ASSERT(x_ != nullptr, "Null abscissas");
262 Size startIdx;
263 Real val;
264
265 const Size isOrderOdd = order_ & 1;
266
267 if (isOrderOdd) {
268 QL_ASSERT((n_>0), "assume at least 1 point in quadrature");
269 val = w_[0]*f(x_[0]);
270 startIdx=1;
271 } else {
272 val = 0.0;
273 startIdx=0;
274 }
275
276 for (Size i=startIdx; i<n_; ++i) {
277 val += w_[i]*f( x_[i]);
278 val += w_[i]*f(-x_[i]);
279 }
280 return val;
281 }
282
283 void order(Size);
284 Size order() const { return order_; }
285
286 private:
288
289 const Real* w_;
290 const Real* x_;
292
293 static const Real w6[3];
294 static const Real x6[3];
295 static const Size n6;
296
297 static const Real w7[4];
298 static const Real x7[4];
299 static const Size n7;
300
301 static const Real w12[6];
302 static const Real x12[6];
303 static const Size n12;
304
305 static const Real w20[10];
306 static const Real x20[10];
307 static const Size n20;
308 };
309
310}
311
312#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:483
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
Real operator()(const std::function< Real(Array)> &f) const
const std::vector< Array > & x() const
tabulated Gauss-Legendre quadratures
Real integrate(const std::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
std::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:37
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