QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
gaussianquadratures.hpp
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
25#ifndef quantlib_gaussian_quadratures_hpp
26#define quantlib_gaussian_quadratures_hpp
27
28#include <ql/math/array.hpp>
29#include <ql/math/integrals/integral.hpp>
30#include <ql/math/integrals/gaussianorthogonalpolynomial.hpp>
31
32namespace QuantLib {
33 class GaussianOrthogonalPolynomial;
34
36
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
81
92 public:
93 explicit GaussLaguerreIntegration(Size n, Real s = 0.0)
95 };
96
98
109 public:
110 explicit GaussHermiteIntegration(Size n, Real mu = 0.0)
112 };
113
115
125 public:
127 : GaussianQuadrature(n, GaussJacobiPolynomial(alpha, beta)) {}
128 };
129
131
141 public:
144 };
145
147
157 public:
160 };
161
163
173 public:
175 : GaussianQuadrature(n, GaussJacobiPolynomial(-0.5, -0.5)) {}
176 };
177
179
189 public:
192 };
193
195
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
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.
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
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
Definition: any.hpp:35
detail::GaussianQuadratureIntegrator< GaussChebyshevIntegration > GaussChebyshevIntegrator
detail::GaussianQuadratureIntegrator< GaussChebyshev2ndIntegration > GaussChebyshev2ndIntegrator
detail::GaussianQuadratureIntegrator< GaussLegendreIntegration > GaussLegendreIntegrator