QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
piecewiseintegral.hpp
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) 2015 Peter Caspers
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
20/*! \file piecewiseintegral.hpp
21 \brief Integral of a piecewise well behaved function using
22 a custom integrator for the pieces. It can be forced
23 that the function is integrated only over intervals
24 strictly not containing the critical points
25*/
26
27#ifndef quantlib_piecewise_integral_hpp
28#define quantlib_piecewise_integral_hpp
29
32#include <ql/shared_ptr.hpp>
33#include <algorithm>
34#include <vector>
35
36namespace QuantLib {
37
39 public:
40 PiecewiseIntegral(ext::shared_ptr<Integrator> integrator,
41 std::vector<Real> criticalPoints,
42 bool avoidCriticalPoints = true);
43
44 protected:
45 Real integrate(const ext::function<Real(Real)>& f, Real a, Real b) const override;
46
47 private:
48 Real integrate_h(const ext::function<Real(Real)> &f, Real a,
49 Real b) const;
50 const ext::shared_ptr<Integrator> integrator_;
51 std::vector<Real> criticalPoints_;
52 const Real eps_;
53};
54
55// inline
56
57inline Real PiecewiseIntegral::integrate_h(const ext::function<Real(Real)> &f,
58 Real a, Real b) const {
59
60 if (!close_enough(a, b))
61 return (*integrator_)(f, a, b);
62 else
63 return 0.0;
64}
65
66inline Real PiecewiseIntegral::integrate(const ext::function<Real(Real)> &f,
67 Real a, Real b) const {
68
69 auto a0 = std::lower_bound(criticalPoints_.begin(), criticalPoints_.end(), a);
70
71 auto b0 = std::lower_bound(criticalPoints_.begin(), criticalPoints_.end(), b);
72
73 if (a0 == criticalPoints_.end()) {
74 Real tmp = 1.0;
75 if (!criticalPoints_.empty()) {
76 if (close_enough(a, criticalPoints_.back())) {
77 tmp = eps_;
78 }
79 }
80 return integrate_h(f, a * tmp, b);
81 }
82
83 Real res = 0.0;
84
85 if (!close_enough(a, *a0)) {
86 res += integrate_h(f, a, std::min(*a0 / eps_, b));
87 }
88
89 if (b0 == criticalPoints_.end()) {
90 --b0;
91 if (!close_enough(*b0, b)) {
92 res += integrate_h(f, (*b0) * eps_, b);
93 }
94 }
95
96 for (auto x = a0; x < b0; ++x) {
97 res += integrate_h(f, (*x) * eps_, std::min(*(x + 1) / eps_, b));
98 }
99
100 return res;
101}
102
103} // namespace QuantLib
104
105#endif
Real integrate_h(const ext::function< Real(Real)> &f, Real a, Real b) const
Real integrate(const ext::function< Real(Real)> &f, Real a, Real b) const override
std::vector< Real > criticalPoints_
const ext::shared_ptr< Integrator > integrator_
floating-point comparisons
ext::function< Real(Real)> b
QL_REAL Real
real number
Definition: types.hpp:50
Integrators base class definition.
Definition: any.hpp:35
bool close_enough(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:182
Maps shared_ptr to either the boost or std implementation.