1 /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 /*
4  Copyright (C) 2007 François du Vignaud
5  Copyright (C) 2003 Niels Elken Sønderby
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 kronrodintegral.hpp
22     \brief Integral of a 1-dimensional function using the Gauss-Kronrod method
23 */
24 
25 #ifndef quantlib_kronrod_integral_hpp
26 #define quantlib_kronrod_integral_hpp
27 
28 #include <ql/errors.hpp>
29 #include <ql/types.hpp>
30 #include <ql/utilities/null.hpp>
31 #include <ql/math/integrals/integral.hpp>
32 #include <ql/functional.hpp>
33 
34 namespace QuantLib {
35 
36     //! Integral of a 1-dimensional function using the Gauss-Kronrod methods
37     /*! This class provide a non-adaptive integration procedure which
38         uses fixed Gauss-Kronrod abscissae to sample the integrand at
39         a maximum of 87 points.  It is provided for fast integration
40         of smooth functions.
41 
42         This function applies the Gauss-Kronrod 10-point, 21-point, 43-point
43         and 87-point integration rules in succession until an estimate of the
44         integral of f over (a, b) is achieved within the desired absolute and
45         relative error limits, epsabs and epsrel. The function returns the
46         final approximation, result, an estimate of the absolute error,
47         abserr and the number of function evaluations used, neval. The
48         Gauss-Kronrod rules are designed in such a way that each rule uses
49         all the results of its predecessors, in order to minimize the total
50         number of function evaluations.
51     */
52     class GaussKronrodNonAdaptive : public Integrator {
53       public:
54         GaussKronrodNonAdaptive(Real absoluteAccuracy,
55                                 Size maxEvaluations,
56                                 Real relativeAccuracy);
57         void setRelativeAccuracy(Real);
58         Real relativeAccuracy() const;
59       protected:
60         Real integrate(const ext::function<Real (Real)>& f,
61                        Real a,
62                        Real b) const;
63       private:
64         Real relativeAccuracy_;
65     };
66 
67     //! Integral of a 1-dimensional function using the Gauss-Kronrod methods
68     /*! This class provide an adaptive integration procedure using 15
69         points Gauss-Kronrod integration rule.  This is more robust in
70         that it allows to integrate less smooth functions (though
71         singular functions should be integrated using dedicated
72         algorithms) but less efficient beacuse it does not reuse
73         precedently computed points during computation steps.
74 
75         References:
76 
77         Gauss-Kronrod Integration
78         <http://mathcssun1.emporia.edu/~oneilcat/ExperimentApplet3/ExperimentApplet3.html>
79 
80         NMS - Numerical Analysis Library
81         <http://www.math.iastate.edu/burkardt/f_src/nms/nms.html>
82 
83         \test the correctness of the result is tested by checking it
84               against known good values.
85     */
86     class GaussKronrodAdaptive : public Integrator {
87       public:
88         explicit GaussKronrodAdaptive(Real tolerance,
89                                       Size maxFunctionEvaluations = Null<Size>());
90       protected:
91           Real integrate(const ext::function<Real (Real)>& f,
92                          Real a,
93                          Real b) const;
94       private:
95           Real integrateRecursively(const ext::function<Real (Real)>& f,
96                                     Real a,
97                                     Real b,
98                                     Real tolerance) const;
99       };
100 }
101 
102 #endif
103