QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
trapezoidintegral.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2003 Roman Gitlin
5 Copyright (C) 2003 StatPro Italia srl
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_trapezoid_integral_hpp
26#define quantlib_trapezoid_integral_hpp
27
28#include <ql/math/integrals/integral.hpp>
29#include <ql/utilities/null.hpp>
30#include <ql/errors.hpp>
31
32namespace QuantLib {
33
35
51 template <class IntegrationPolicy>
53 public:
55 Size maxIterations)
56 : Integrator(accuracy, maxIterations){}
57
58 protected:
59 Real integrate(const ext::function<Real(Real)>& f, Real a, Real b) const override {
60
61 // start from the coarsest trapezoid...
62 Size N = 1;
63 Real I = (f(a)+f(b))*(b-a)/2.0, newI;
64 // ...and refine it
65 Size i = 1;
66 do {
67 newI = IntegrationPolicy::integrate(f,a,b,I,N);
68 N *= IntegrationPolicy::nbEvalutions();
69 // good enough? Also, don't run away immediately
70 if (std::fabs(I-newI) <= absoluteAccuracy() && i > 5)
71 // ok, exit
72 return newI;
73 // oh well. Another step.
74 I = newI;
75 i++;
76 } while (i < maxEvaluations());
77 QL_FAIL("max number of iterations reached");
78 }
79 };
80
81 // Integration policies
82 struct Default {
83 inline static Real integrate(const ext::function<Real (Real)>& f,
84 Real a,
85 Real b,
86 Real I,
87 Size N)
88 {
89 Real sum = 0.0;
90 Real dx = (b-a)/N;
91 Real x = a + dx/2.0;
92 for (Size i=0; i<N; x += dx, ++i)
93 sum += f(x);
94 return (I + dx*sum)/2.0;
95 }
96 inline static Size nbEvalutions(){ return 2;}
97 };
98
99 struct MidPoint {
100 inline static Real integrate(const ext::function<Real (Real)>& f,
101 Real a,
102 Real b,
103 Real I,
104 Size N)
105 {
106 Real sum = 0.0;
107 Real dx = (b-a)/N;
108 Real x = a + dx/6.0;
109 Real D = 2.0*dx/3.0;
110 for (Size i=0; i<N; x += dx, ++i)
111 sum += f(x) + f(x+D);
112 return (I + dx*sum)/3.0;
113 }
114 inline static Size nbEvalutions(){ return 3;}
115 };
116
117}
118
119#endif
Real absoluteAccuracy() const
Definition: integral.cpp:43
Size maxEvaluations() const
Definition: integral.cpp:47
Integral of a one-dimensional function.
TrapezoidIntegral(Real accuracy, Size maxIterations)
Real integrate(const ext::function< Real(Real)> &f, Real a, Real b) const override
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
static Real integrate(const ext::function< Real(Real)> &f, Real a, Real b, Real I, Size N)
static Size nbEvalutions()
static Real integrate(const ext::function< Real(Real)> &f, Real a, Real b, Real I, Size N)
static Size nbEvalutions()