QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
discreteintegrals.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) 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 <http://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
21
22namespace QuantLib {
23
25 const Array& x, const Array& f) const {
26
27 const Size n = f.size();
28 QL_REQUIRE(n == x.size(), "inconsistent size");
29
30 Real sum = 0.0;
31
32 for (Size i=0; i < n-1; ++i) {
33 sum += (x[i+1]-x[i])*(f[i]+f[i+1]);
34 }
35
36 return 0.5*sum;
37 }
38
40 const Array& x, const Array& f) const {
41
42 const Size n = f.size();
43 QL_REQUIRE(n == x.size(), "inconsistent size");
44
45 Real sum = 0.0;
46
47 for (Size j=0; j < n-2; j+=2) {
48 const Real dxj = x[j+1]-x[j];
49 const Real dxjp1 = x[j+2]-x[j+1];
50
51 const Real alpha = dxjp1*(2*dxj-dxjp1);
52 const Real dd = dxj+dxjp1;
53 const Real k = dd/(6*dxjp1*dxj);
54 const Real beta = dd*dd;
55 const Real gamma = dxj*(2*dxjp1-dxj);
56
57 sum += k*(alpha*f[j]+beta*f[j+1]+gamma*f[j+2]);
58 }
59 if ((n & 1) == 0U) {
60 sum += 0.5*(x[n-1]-x[n-2])*(f[n-1]+f[n-2]);
61 }
62
63 return sum;
64 }
65
67 const ext::function<Real (Real)>& f, Real a, Real b) const {
68 const Size n=maxEvaluations()-1;
69 const Real d=(b-a)/n;
70
71 Real sum = f(a)*0.5;
72
73 for(Size i=0;i<n-1;++i) {
74 a += d;
75 sum += f(a);
76 }
77 sum += f(b)*0.5;
78
80
81 return d * sum;
82 }
83
85 const ext::function<Real (Real)>& f, Real a, Real b) const {
86 const Size n=maxEvaluations()-1;
87 const Real d=(b-a)/n, d2=d*2;
88
89 Real sum = 0.0, x = a + d;
90 for(Size i=1;i<n;i+=2) {//to time 4
91 sum += f(x);
92 x += d2;
93 }
94 sum *= 2;
95
96 x = a+d2;
97 for(Size i=2;i<n-1;i+=2) {//to time 2
98 sum += f(x);
99 x += d2;
100 }
101 sum *= 2;
102
103 sum += f(a);
104 if((n&1) != 0U)
105 sum += 1.5*f(b)+2.5*f(b-d);
106 else
107 sum += f(b);
108
110
111 return d/3 * sum;
112 }
113}
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:495
Real operator()(const Array &x, const Array &f) const
Real integrate(const ext::function< Real(Real)> &f, Real a, Real b) const override
Real operator()(const Array &x, const Array &f) const
Real integrate(const ext::function< Real(Real)> &f, Real a, Real b) const override
Size maxEvaluations() const
Definition: integral.cpp:47
void increaseNumberOfEvaluations(Size increase) const
Definition: integral.cpp:67
integrals on non uniform grids
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Date d
ext::function< Real(Real)> b
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
Real beta
Definition: sabr.cpp:200
Real alpha
Definition: sabr.cpp:200