QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
forwardflatinterpolation.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) 2005, 2008 StatPro Italia srl
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 forwardflatinterpolation.hpp
21 \brief forward-flat interpolation between discrete points
22*/
23
24#ifndef quantlib_forward_flat_interpolation_hpp
25#define quantlib_forward_flat_interpolation_hpp
26
28#include <vector>
29
30namespace QuantLib {
31
32 namespace detail {
33 template<class I1, class I2> class ForwardFlatInterpolationImpl;
34 }
35
36 //! Forward-flat interpolation between discrete points
37 /*! \ingroup interpolations
38 \warning See the Interpolation class for information about the
39 required lifetime of the underlying data.
40 */
42 public:
43 /*! \pre the \f$ x \f$ values must be sorted. */
44 template <class I1, class I2>
45 ForwardFlatInterpolation(const I1& xBegin, const I1& xEnd,
46 const I2& yBegin) {
47 impl_ = ext::shared_ptr<Interpolation::Impl>(new
49 yBegin));
50 impl_->update();
51 }
52 };
53
54 //! Forward-flat interpolation factory and traits
55 /*! \ingroup interpolations */
57 public:
58 template <class I1, class I2>
59 Interpolation interpolate(const I1& xBegin, const I1& xEnd,
60 const I2& yBegin) const {
61 return ForwardFlatInterpolation(xBegin, xEnd, yBegin);
62 }
63 static const bool global = false;
64 static const Size requiredPoints = 2;
65 };
66
67 namespace detail {
68
69 template <class I1, class I2>
71 : public Interpolation::templateImpl<I1,I2> {
72 public:
73 ForwardFlatInterpolationImpl(const I1& xBegin, const I1& xEnd,
74 const I2& yBegin)
75 : Interpolation::templateImpl<I1,I2>(xBegin, xEnd, yBegin,
76 ForwardFlat::requiredPoints),
77 primitive_(xEnd-xBegin), n_(xEnd-xBegin) {}
78 void update() override {
79 primitive_[0] = 0.0;
80 for (Size i=1; i<n_; i++) {
81 Real dx = this->xBegin_[i]-this->xBegin_[i-1];
82 primitive_[i] = primitive_[i-1] + dx*this->yBegin_[i-1];
83 }
84 }
85 Real value(Real x) const override {
86 if (x >= this->xBegin_[n_-1])
87 return this->yBegin_[n_-1];
88
89 Size i = this->locate(x);
90 return this->yBegin_[i];
91 }
92 Real primitive(Real x) const override {
93 Size i = this->locate(x);
94 Real dx = x-this->xBegin_[i];
95 return primitive_[i] + dx*this->yBegin_[i];
96 }
97 Real derivative(Real) const override { return 0.0; }
98 Real secondDerivative(Real) const override { return 0.0; }
99
100 private:
101 std::vector<Real> primitive_;
103 };
104
105 }
106
107}
108
109#endif
Forward-flat interpolation factory and traits.
Interpolation interpolate(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin) const
Forward-flat interpolation between discrete points.
ForwardFlatInterpolation(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin)
basic template implementation
templateImpl(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, const int requiredPoints=2)
base class for 1-D interpolations.
ext::shared_ptr< Impl > impl_
ForwardFlatInterpolationImpl(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin)
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
base class for 1-D interpolations
Definition: any.hpp:35