Coverage Report

Created: 2026-01-25 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/math/interpolations/forwardflatinterpolation.hpp
Line
Count
Source
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
 <https://www.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
27
#include <ql/math/interpolation.hpp>
28
#include <vector>
29
30
namespace 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
    */
41
    class ForwardFlatInterpolation : public Interpolation {
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
0
                                 const I2& yBegin) {
47
0
            impl_ = ext::shared_ptr<Interpolation::Impl>(new
48
0
                detail::ForwardFlatInterpolationImpl<I1,I2>(xBegin, xEnd,
49
0
                                                            yBegin));
50
0
            impl_->update();
51
0
        }
52
    };
53
54
    //! Forward-flat interpolation factory and traits
55
    /*! \ingroup interpolations */
56
    class ForwardFlat {
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 ForwardFlatInterpolation(xBegin, xEnd, yBegin);
62
0
        }
63
        static const bool global = false;
64
        static const Size requiredPoints = 2;
65
    };
66
67
    namespace detail {
68
69
        template <class I1, class I2>
70
        class ForwardFlatInterpolationImpl
71
            : public Interpolation::templateImpl<I1,I2> {
72
          public:
73
            ForwardFlatInterpolationImpl(const I1& xBegin, const I1& xEnd,
74
                                         const I2& yBegin)
75
0
            : Interpolation::templateImpl<I1,I2>(xBegin, xEnd, yBegin,
76
0
                                                 ForwardFlat::requiredPoints),
77
0
              primitive_(xEnd-xBegin), n_(xEnd-xBegin) {}
78
0
            void update() override {
79
0
                primitive_[0] = 0.0;
80
0
                for (Size i=1; i<n_; i++) {
81
0
                    Real dx = this->xBegin_[i]-this->xBegin_[i-1];
82
0
                    primitive_[i] = primitive_[i-1] + dx*this->yBegin_[i-1];
83
0
                }
84
0
            }
85
0
            Real value(Real x) const override {
86
0
                if (x >= this->xBegin_[n_-1])
87
0
                    return this->yBegin_[n_-1];
88
89
0
                Size i = this->locate(x);
90
0
                return this->yBegin_[i];
91
0
            }
92
0
            Real primitive(Real x) const override {
93
0
                Size i = this->locate(x);
94
0
                Real dx = x-this->xBegin_[i];
95
0
                return primitive_[i] + dx*this->yBegin_[i];
96
0
            }
97
0
            Real derivative(Real) const override { return 0.0; }
98
0
            Real secondDerivative(Real) const override { return 0.0; }
99
100
          private:
101
            std::vector<Real> primitive_;
102
            Size n_;
103
        };
104
105
    }
106
107
}
108
109
#endif