/src/quantlib/ql/math/interpolations/backwardflatinterpolation.hpp
Line | Count | Source (jump to first uncovered line) |
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 backwardflatinterpolation.hpp |
21 | | \brief backward-flat interpolation between discrete points |
22 | | */ |
23 | | |
24 | | #ifndef quantlib_backward_flat_interpolation_hpp |
25 | | #define quantlib_backward_flat_interpolation_hpp |
26 | | |
27 | | #include <ql/math/interpolation.hpp> |
28 | | #include <vector> |
29 | | |
30 | | namespace QuantLib { |
31 | | |
32 | | namespace detail { |
33 | | template<class I1, class I2> class BackwardFlatInterpolationImpl; |
34 | | } |
35 | | |
36 | | //! Backward-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 | | */ |
41 | | class BackwardFlatInterpolation : public Interpolation { |
42 | | public: |
43 | | /*! \pre the \f$ x \f$ values must be sorted. */ |
44 | | template <class I1, class I2> |
45 | | BackwardFlatInterpolation(const I1& xBegin, const I1& xEnd, |
46 | 0 | const I2& yBegin) { |
47 | 0 | impl_ = ext::shared_ptr<Interpolation::Impl>(new |
48 | 0 | detail::BackwardFlatInterpolationImpl<I1,I2>(xBegin, xEnd, |
49 | 0 | yBegin)); |
50 | 0 | impl_->update(); |
51 | 0 | } Unexecuted instantiation: QuantLib::BackwardFlatInterpolation::BackwardFlatInterpolation<std::__1::__wrap_iter<double*>, double const*>(std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&, double const* const&) Unexecuted instantiation: QuantLib::BackwardFlatInterpolation::BackwardFlatInterpolation<double*, double const*>(double* const&, double* const&, double const* const&) |
52 | | }; |
53 | | |
54 | | //! Backward-flat interpolation factory and traits |
55 | | /*! \ingroup interpolations */ |
56 | | class BackwardFlat { |
57 | | public: |
58 | | template <class I1, class I2> |
59 | | Interpolation interpolate(const I1& xBegin, const I1& xEnd, |
60 | 0 | const I2& yBegin) const { |
61 | 0 | return BackwardFlatInterpolation(xBegin, xEnd, yBegin); |
62 | 0 | } |
63 | | static const bool global = false; |
64 | | static const Size requiredPoints = 1; |
65 | | }; |
66 | | |
67 | | namespace detail { |
68 | | |
69 | | template <class I1, class I2> |
70 | | class BackwardFlatInterpolationImpl |
71 | | : public Interpolation::templateImpl<I1,I2> { |
72 | | public: |
73 | | BackwardFlatInterpolationImpl(const I1& xBegin, const I1& xEnd, |
74 | | const I2& yBegin) |
75 | 0 | : Interpolation::templateImpl<I1,I2>(xBegin,xEnd,yBegin, |
76 | 0 | BackwardFlat::requiredPoints), |
77 | 0 | primitive_(xEnd-xBegin) {} Unexecuted instantiation: QuantLib::detail::BackwardFlatInterpolationImpl<std::__1::__wrap_iter<double*>, double const*>::BackwardFlatInterpolationImpl(std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&, double const* const&) Unexecuted instantiation: QuantLib::detail::BackwardFlatInterpolationImpl<double*, double const*>::BackwardFlatInterpolationImpl(double* const&, double* const&, double const* const&) |
78 | 0 | void update() override { |
79 | 0 | Size n = this->xEnd_-this->xBegin_; |
80 | 0 | primitive_[0] = 0.0; |
81 | 0 | for (Size i=1; i<n; i++) { |
82 | 0 | Real dx = this->xBegin_[i]-this->xBegin_[i-1]; |
83 | 0 | primitive_[i] = primitive_[i-1] + dx*this->yBegin_[i]; |
84 | 0 | } |
85 | 0 | } Unexecuted instantiation: QuantLib::detail::BackwardFlatInterpolationImpl<std::__1::__wrap_iter<double*>, double const*>::update() Unexecuted instantiation: QuantLib::detail::BackwardFlatInterpolationImpl<double*, double const*>::update() |
86 | 0 | Real value(Real x) const override { |
87 | 0 | if (x <= this->xBegin_[0] |
88 | 0 | || std::distance(this->xBegin_, this->xEnd_) == 1) |
89 | 0 | return this->yBegin_[0]; |
90 | | |
91 | 0 | Size i = this->locate(x); |
92 | 0 | if (x == this->xBegin_[i]) |
93 | 0 | return this->yBegin_[i]; |
94 | 0 | else |
95 | 0 | return this->yBegin_[i+1]; |
96 | 0 | } Unexecuted instantiation: QuantLib::detail::BackwardFlatInterpolationImpl<std::__1::__wrap_iter<double*>, double const*>::value(double) const Unexecuted instantiation: QuantLib::detail::BackwardFlatInterpolationImpl<double*, double const*>::value(double) const |
97 | 0 | Real primitive(Real x) const override { |
98 | 0 | if (std::distance(this->xBegin_, this->xEnd_) == 1) |
99 | 0 | return (x - this->xBegin_[0]) * this->yBegin_[0]; |
100 | | |
101 | 0 | Size i = this->locate(x); |
102 | 0 | Real dx = x-this->xBegin_[i]; |
103 | 0 | return primitive_[i] + dx*this->yBegin_[i+1]; |
104 | 0 | } Unexecuted instantiation: QuantLib::detail::BackwardFlatInterpolationImpl<std::__1::__wrap_iter<double*>, double const*>::primitive(double) const Unexecuted instantiation: QuantLib::detail::BackwardFlatInterpolationImpl<double*, double const*>::primitive(double) const |
105 | 0 | Real derivative(Real) const override { return 0.0; } Unexecuted instantiation: QuantLib::detail::BackwardFlatInterpolationImpl<std::__1::__wrap_iter<double*>, double const*>::derivative(double) const Unexecuted instantiation: QuantLib::detail::BackwardFlatInterpolationImpl<double*, double const*>::derivative(double) const |
106 | 0 | Real secondDerivative(Real) const override { return 0.0; } Unexecuted instantiation: QuantLib::detail::BackwardFlatInterpolationImpl<std::__1::__wrap_iter<double*>, double const*>::secondDerivative(double) const Unexecuted instantiation: QuantLib::detail::BackwardFlatInterpolationImpl<double*, double const*>::secondDerivative(double) const |
107 | | |
108 | | private: |
109 | | std::vector<Real> primitive_; |
110 | | }; |
111 | | |
112 | | } |
113 | | |
114 | | } |
115 | | |
116 | | #endif |