QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
segmentintegral.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
5 Copyright (C) 2015 Peter Caspers
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_segment_integral_h
26#define quantlib_segment_integral_h
27
28#include <ql/math/integrals/integral.hpp>
29#include <ql/math/comparison.hpp>
30#include <ql/errors.hpp>
31
32namespace QuantLib {
33
35
50 class SegmentIntegral : public Integrator {
51 public:
52 explicit SegmentIntegral(Size intervals);
53 protected:
54 Real integrate(const ext::function<Real(Real)>& f, Real a, Real b) const override;
55
56 private:
58 };
59
60
61 // inline and template definitions
62
63 inline Real
64 SegmentIntegral::integrate(const ext::function<Real (Real)>& f,
65 Real a,
66 Real b) const {
67 if(close_enough(a,b))
68 return 0.0;
69 Real dx = (b-a)/intervals_;
70 Real sum = 0.5*(f(a)+f(b));
71 Real end = b - 0.5*dx;
72 for (Real x = a+dx; x < end; x += dx)
73 sum += f(x);
74 return sum*dx;
75 }
76
77}
78
79#endif
Integral of a one-dimensional function.
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
bool close_enough(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:182