/src/quantlib/ql/math/integrals/discreteintegrals.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2014 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 | | <https://www.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 | | #include <ql/math/integrals/discreteintegrals.hpp> |
21 | | |
22 | | namespace QuantLib { |
23 | | |
24 | | Real DiscreteTrapezoidIntegral::operator()( |
25 | 0 | const Array& x, const Array& f) const { |
26 | |
|
27 | 0 | const Size n = f.size(); |
28 | 0 | QL_REQUIRE(n == x.size(), "inconsistent size"); |
29 | | |
30 | 0 | Real sum = 0.0; |
31 | |
|
32 | 0 | for (Size i=0; i < n-1; ++i) { |
33 | 0 | sum += (x[i+1]-x[i])*(f[i]+f[i+1]); |
34 | 0 | } |
35 | |
|
36 | 0 | return 0.5*sum; |
37 | 0 | } |
38 | | |
39 | | Real DiscreteSimpsonIntegral::operator()( |
40 | 0 | const Array& x, const Array& f) const { |
41 | |
|
42 | 0 | const Size n = f.size(); |
43 | 0 | QL_REQUIRE(n == x.size(), "inconsistent size"); |
44 | | |
45 | 0 | Real sum = 0.0; |
46 | |
|
47 | 0 | for (Size j=0; j < n-2; j+=2) { |
48 | 0 | const Real dxj = x[j+1]-x[j]; |
49 | 0 | const Real dxjp1 = x[j+2]-x[j+1]; |
50 | |
|
51 | 0 | const Real alpha = dxjp1*(2*dxj-dxjp1); |
52 | 0 | const Real dd = dxj+dxjp1; |
53 | 0 | const Real k = dd/(6*dxjp1*dxj); |
54 | 0 | const Real beta = dd*dd; |
55 | 0 | const Real gamma = dxj*(2*dxjp1-dxj); |
56 | |
|
57 | 0 | sum += k*(alpha*f[j]+beta*f[j+1]+gamma*f[j+2]); |
58 | 0 | } |
59 | 0 | if ((n & 1) == 0U) { |
60 | 0 | sum += 0.5*(x[n-1]-x[n-2])*(f[n-1]+f[n-2]); |
61 | 0 | } |
62 | |
|
63 | 0 | return sum; |
64 | 0 | } |
65 | | |
66 | | Real DiscreteTrapezoidIntegrator::integrate( |
67 | 0 | const std::function<Real (Real)>& f, Real a, Real b) const { |
68 | 0 | const Size n=maxEvaluations()-1; |
69 | 0 | const Real d=(b-a)/n; |
70 | | |
71 | 0 | Real sum = f(a)*0.5; |
72 | | |
73 | 0 | for(Size i=0;i<n-1;++i) { |
74 | 0 | a += d; |
75 | 0 | sum += f(a); |
76 | 0 | } |
77 | 0 | sum += f(b)*0.5; |
78 | | |
79 | 0 | increaseNumberOfEvaluations(maxEvaluations()); |
80 | | |
81 | 0 | return d * sum; |
82 | 0 | } |
83 | | |
84 | | Real DiscreteSimpsonIntegrator::integrate( |
85 | 0 | const std::function<Real (Real)>& f, Real a, Real b) const { |
86 | 0 | const Size n=maxEvaluations()-1; |
87 | 0 | const Real d=(b-a)/n, d2=d*2; |
88 | | |
89 | 0 | Real sum = 0.0, x = a + d; |
90 | 0 | for(Size i=1;i<n;i+=2) {//to time 4 |
91 | 0 | sum += f(x); |
92 | 0 | x += d2; |
93 | 0 | } |
94 | 0 | sum *= 2; |
95 | |
|
96 | 0 | x = a+d2; |
97 | 0 | for(Size i=2;i<n-1;i+=2) {//to time 2 |
98 | 0 | sum += f(x); |
99 | 0 | x += d2; |
100 | 0 | } |
101 | 0 | sum *= 2; |
102 | |
|
103 | 0 | sum += f(a); |
104 | 0 | if((n&1) != 0U) |
105 | 0 | sum += 1.5*f(b)+2.5*f(b-d); |
106 | 0 | else |
107 | 0 | sum += f(b); |
108 | | |
109 | 0 | increaseNumberOfEvaluations(maxEvaluations()); |
110 | | |
111 | 0 | return d/3 * sum; |
112 | 0 | } |
113 | | } |