QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
gaussianquadratures.cpp
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#include <ql/utilities/null.hpp>
29
30#include <map>
31
32namespace QuantLib {
33
35 Size n,
36 const GaussianOrthogonalPolynomial& orthPoly)
37 : x_(n), w_(n) {
38
39 // set-up matrix to compute the roots and the weights
40 Array e(n-1);
41
42 Size i;
43 for (i=1; i < n; ++i) {
44 x_[i] = orthPoly.alpha(i);
45 e[i-1] = std::sqrt(orthPoly.beta(i));
46 }
47 x_[0] = orthPoly.alpha(0);
48
50 x_, e,
53
54 x_ = tqr.eigenvalues();
55 const Matrix& ev = tqr.eigenvectors();
56
57 Real mu_0 = orthPoly.mu_0();
58 for (i=0; i<n; ++i) {
59 w_[i] = mu_0*ev[0][i]*ev[0][i] / orthPoly.w(x_[i]);
60 }
61 }
62
63
65 const std::vector<Size>& ns,
66 const std::function<ext::shared_ptr<GaussianQuadrature>(Size)>& genQuad)
67 : weights_(std::accumulate(ns.begin(), ns.end(), Size(1), std::multiplies<>()), 1.0),
68 x_(weights_.size(), Array(ns.size())) {
69
70 const Size m = ns.size();
71 const Size n = x_.size();
72
73 std::vector<Size> spacing(m);
74 spacing[0] = 1;
75 std::partial_sum(ns.begin(), ns.end()-1, spacing.begin()+1, std::multiplies<>());
76
77 std::map<Size, Array> n2weights, n2x;
78 for (auto order: ns) {
79 if (n2x.find(order) == n2x.end()) {
80 const ext::shared_ptr<GaussianQuadrature> quad = genQuad(order);
81 n2x[order] = quad->x();
82 n2weights[order] = quad->weights();
83 }
84 }
85
86 for (Size i=0; i < n; ++i) {
87 for (Size j=0; j < m; ++j) {
88 const Size order = ns[j];
89 const Size nx = (i / spacing[j]) % ns[j];
90 weights_[i] *= n2weights[order][nx];
91 x_[i][j] = n2x[order][nx];
92 }
93 }
94 }
95
97 const std::function<Real(Array)>& f) const {
98 Real s = 0.0;
99 const Size n = x_.size();
100 for (Size i=0; i < n; ++i)
101 s += weights_[i]*f(x_[i]);
102
103 return s;
104 }
105
106
107
108 namespace detail {
109 template <class Integration>
111 Size n)
112 : Integrator(Null<Real>(), n),
113 integration_(ext::make_shared<Integration>(n))
114 { }
115
116 template <class Integration>
118 const std::function<Real (Real)>& f, Real a, Real b) const {
119
120 const Real c1 = 0.5*(b-a);
121 const Real c2 = 0.5*(a+b);
122
123 return c1*integration_->operator()(
124 [c1, c2, f](Real x) { return f(c1*x+c2);});
125 }
126
130 }
131
133 switch(order) {
134 case(6):
135 order_=order; x_=x6; w_=w6; n_=n6;
136 break;
137 case(7):
138 order_=order; x_=x7; w_=w7; n_=n7;
139 break;
140 case(12):
142 break;
143 case(20):
145 break;
146 default:
147 QL_FAIL("order " << order << " not supported");
148 }
149 }
150
151
152 // Abscissas and Weights from Abramowitz and Stegun
153
154 /* order 6 */
155 const Real TabulatedGaussLegendre::x6[3] = { 0.238619186083197,
156 0.661209386466265,
157 0.932469514203152 };
158
159 const Real TabulatedGaussLegendre::w6[3] = { 0.467913934572691,
160 0.360761573048139,
161 0.171324492379170 };
162
164
165 /* order 7 */
166 const Real TabulatedGaussLegendre::x7[4] = { 0.000000000000000,
167 0.405845151377397,
168 0.741531185599394,
169 0.949107912342759 };
170
171 const Real TabulatedGaussLegendre::w7[4] = { 0.417959183673469,
172 0.381830050505119,
173 0.279705391489277,
174 0.129484966168870 };
175
177
178 /* order 12 */
179 const Real TabulatedGaussLegendre::x12[6] = { 0.125233408511469,
180 0.367831498998180,
181 0.587317954286617,
182 0.769902674194305,
183 0.904117256370475,
184 0.981560634246719 };
185
186 const Real TabulatedGaussLegendre::w12[6] = { 0.249147045813403,
187 0.233492536538355,
188 0.203167426723066,
189 0.160078328543346,
190 0.106939325995318,
191 0.047175336386512 };
192
194
195 /* order 20 */
196 const Real TabulatedGaussLegendre::x20[10] = { 0.076526521133497,
197 0.227785851141645,
198 0.373706088715420,
199 0.510867001950827,
200 0.636053680726515,
201 0.746331906460151,
202 0.839116971822219,
203 0.912234428251326,
204 0.963971927277914,
205 0.993128599185095 };
206
207 const Real TabulatedGaussLegendre::w20[10] = { 0.152753387130726,
208 0.149172986472604,
209 0.142096109318382,
210 0.131688638449177,
211 0.118194531961518,
212 0.101930119817240,
213 0.083276741576704,
214 0.062672048334109,
215 0.040601429800387,
216 0.017614007139152 };
217
219
220}
1-D array used in linear algebra.
Definition: array.hpp:52
orthogonal polynomial for Gaussian quadratures
virtual Real alpha(Size i) const =0
virtual Real beta(Size i) const =0
virtual Real w(Real x) const =0
GaussianQuadrature(Size n, const GaussianOrthogonalPolynomial &p)
Matrix used in linear algebra.
Definition: matrix.hpp:41
Real operator()(const std::function< Real(Array)> &f) const
MultiDimGaussianIntegration(const std::vector< Size > &ns, const std::function< ext::shared_ptr< GaussianQuadrature >(Size)> &genQuad)
template class providing a null value for a given type.
Definition: null.hpp:59
tridiag. QR eigen decomposition with explicite shift aka Wilkinson
Real integrate(const std::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
std::function< Real(Real)> b
Integral of a 1-dimensional function using the Gauss quadratures.
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:37
STL namespace.
null values
Eigenvalues/eigenvectors of a real symmetric matrix.
tridiag. QR eigen decompositions with implicit shift
std::uint64_t x_