Coverage Report

Created: 2026-02-03 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/math/interpolations/linearinterpolation.hpp
Line
Count
Source
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) 2003, 2004, 2008 StatPro Italia srl
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
 <https://www.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
21
/*! \file linearinterpolation.hpp
22
    \brief linear interpolation between discrete points
23
*/
24
25
#ifndef quantlib_linear_interpolation_hpp
26
#define quantlib_linear_interpolation_hpp
27
28
#include <ql/math/interpolation.hpp>
29
#include <vector>
30
31
namespace QuantLib {
32
33
    namespace detail {
34
        template<class I1, class I2> class LinearInterpolationImpl;
35
    }
36
37
    //! %Linear interpolation between discrete points
38
    /*! \ingroup interpolations
39
        \warning See the Interpolation class for information about the
40
                 required lifetime of the underlying data.
41
    */
42
    class LinearInterpolation : public Interpolation {
43
      public:
44
        /*! \pre the \f$ x \f$ values must be sorted. */
45
        template <class I1, class I2>
46
        LinearInterpolation(const I1& xBegin, const I1& xEnd,
47
0
                            const I2& yBegin) {
48
0
            impl_ = ext::shared_ptr<Interpolation::Impl>(new
49
0
                detail::LinearInterpolationImpl<I1,I2>(xBegin, xEnd,
50
0
                                                       yBegin));
51
0
            impl_->update();
52
0
        }
Unexecuted instantiation: QuantLib::LinearInterpolation::LinearInterpolation<std::__1::__wrap_iter<double const*>, double*>(std::__1::__wrap_iter<double const*> const&, std::__1::__wrap_iter<double const*> const&, double* const&)
Unexecuted instantiation: QuantLib::LinearInterpolation::LinearInterpolation<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*> >(std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&)
Unexecuted instantiation: QuantLib::LinearInterpolation::LinearInterpolation<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double const*> >(std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double const*> const&)
Unexecuted instantiation: QuantLib::LinearInterpolation::LinearInterpolation<std::__1::__wrap_iter<double const*>, std::__1::__wrap_iter<double*> >(std::__1::__wrap_iter<double const*> const&, std::__1::__wrap_iter<double const*> const&, std::__1::__wrap_iter<double*> const&)
Unexecuted instantiation: QuantLib::LinearInterpolation::LinearInterpolation<double*, double*>(double* const&, double* const&, double* const&)
Unexecuted instantiation: QuantLib::LinearInterpolation::LinearInterpolation<std::__1::__wrap_iter<double const*>, std::__1::__wrap_iter<double const*> >(std::__1::__wrap_iter<double const*> const&, std::__1::__wrap_iter<double const*> const&, std::__1::__wrap_iter<double const*> const&)
Unexecuted instantiation: QuantLib::LinearInterpolation::LinearInterpolation<double const*, double*>(double const* const&, double const* const&, double* const&)
Unexecuted instantiation: QuantLib::LinearInterpolation::LinearInterpolation<std::__1::__wrap_iter<double*>, QuantLib::step_iterator<double*> >(std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&, QuantLib::step_iterator<double*> const&)
Unexecuted instantiation: QuantLib::LinearInterpolation::LinearInterpolation<double const*, double const*>(double const* const&, double const* const&, double const* const&)
53
    };
54
55
    //! %Linear-interpolation factory and traits
56
    /*! \ingroup interpolations */
57
    class Linear {
58
      public:
59
        template <class I1, class I2>
60
        Interpolation interpolate(const I1& xBegin, const I1& xEnd,
61
0
                                  const I2& yBegin) const {
62
0
            return LinearInterpolation(xBegin, xEnd, yBegin);
63
0
        }
Unexecuted instantiation: QuantLib::Interpolation QuantLib::Linear::interpolate<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*> >(std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&) const
Unexecuted instantiation: QuantLib::Interpolation QuantLib::Linear::interpolate<std::__1::__wrap_iter<double*>, QuantLib::step_iterator<double*> >(std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&, QuantLib::step_iterator<double*> const&) const
64
        static const bool global = false;
65
        static const Size requiredPoints = 2;
66
    };
67
68
    namespace detail {
69
70
        template <class I1, class I2>
71
        class LinearInterpolationImpl
72
            : public Interpolation::templateImpl<I1,I2> {
73
          public:
74
            LinearInterpolationImpl(const I1& xBegin, const I1& xEnd,
75
                                    const I2& yBegin)
76
0
            : Interpolation::templateImpl<I1,I2>(xBegin, xEnd, yBegin,
77
0
                                                 Linear::requiredPoints),
78
0
              primitiveConst_(xEnd-xBegin), s_(xEnd-xBegin) {}
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double const*>, double*>::LinearInterpolationImpl(std::__1::__wrap_iter<double const*> const&, std::__1::__wrap_iter<double const*> const&, double* const&)
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*> >::LinearInterpolationImpl(std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&)
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double const*> >::LinearInterpolationImpl(std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double const*> const&)
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double const*>, std::__1::__wrap_iter<double*> >::LinearInterpolationImpl(std::__1::__wrap_iter<double const*> const&, std::__1::__wrap_iter<double const*> const&, std::__1::__wrap_iter<double*> const&)
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<double*, double*>::LinearInterpolationImpl(double* const&, double* const&, double* const&)
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double const*>, std::__1::__wrap_iter<double const*> >::LinearInterpolationImpl(std::__1::__wrap_iter<double const*> const&, std::__1::__wrap_iter<double const*> const&, std::__1::__wrap_iter<double const*> const&)
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<double const*, double*>::LinearInterpolationImpl(double const* const&, double const* const&, double* const&)
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double*>, QuantLib::step_iterator<double*> >::LinearInterpolationImpl(std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&, QuantLib::step_iterator<double*> const&)
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<double const*, double const*>::LinearInterpolationImpl(double const* const&, double const* const&, double const* const&)
79
0
            void update() override {
80
0
                primitiveConst_[0] = 0.0;
81
0
                for (Size i=1; i<Size(this->xEnd_-this->xBegin_); ++i) {
82
0
                    Real dx = this->xBegin_[i]-this->xBegin_[i-1];
83
0
                    s_[i-1] = (Real(this->yBegin_[i])-Real(this->yBegin_[i-1]))/dx;
84
0
                    primitiveConst_[i] = primitiveConst_[i-1]
85
0
                        + dx*(this->yBegin_[i-1] +0.5*dx*s_[i-1]);
86
0
                }
87
0
            }
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double const*>, double*>::update()
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*> >::update()
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double const*> >::update()
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double const*>, std::__1::__wrap_iter<double*> >::update()
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<double*, double*>::update()
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double const*>, std::__1::__wrap_iter<double const*> >::update()
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<double const*, double*>::update()
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double*>, QuantLib::step_iterator<double*> >::update()
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<double const*, double const*>::update()
88
0
            Real value(Real x) const override {
89
0
                Size i = this->locate(x);
90
0
                return this->yBegin_[i] + (x-this->xBegin_[i])*s_[i];
91
0
            }
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double const*>, double*>::value(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*> >::value(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double const*> >::value(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double const*>, std::__1::__wrap_iter<double*> >::value(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<double*, double*>::value(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double const*>, std::__1::__wrap_iter<double const*> >::value(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<double const*, double*>::value(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double*>, QuantLib::step_iterator<double*> >::value(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<double const*, double const*>::value(double) const
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 primitiveConst_[i] +
96
0
                    dx*(this->yBegin_[i] + 0.5*dx*s_[i]);
97
0
            }
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double const*>, double*>::primitive(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*> >::primitive(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double const*> >::primitive(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double const*>, std::__1::__wrap_iter<double*> >::primitive(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<double*, double*>::primitive(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double const*>, std::__1::__wrap_iter<double const*> >::primitive(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<double const*, double*>::primitive(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double*>, QuantLib::step_iterator<double*> >::primitive(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<double const*, double const*>::primitive(double) const
98
0
            Real derivative(Real x) const override {
99
0
                Size i = this->locate(x);
100
0
                return s_[i];
101
0
            }
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double const*>, double*>::derivative(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*> >::derivative(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double const*> >::derivative(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double const*>, std::__1::__wrap_iter<double*> >::derivative(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<double*, double*>::derivative(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double const*>, std::__1::__wrap_iter<double const*> >::derivative(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<double const*, double*>::derivative(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double*>, QuantLib::step_iterator<double*> >::derivative(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<double const*, double const*>::derivative(double) const
102
0
            Real secondDerivative(Real) const override { return 0.0; }
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double const*>, double*>::secondDerivative(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*> >::secondDerivative(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double const*> >::secondDerivative(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double const*>, std::__1::__wrap_iter<double*> >::secondDerivative(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<double*, double*>::secondDerivative(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double const*>, std::__1::__wrap_iter<double const*> >::secondDerivative(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<double const*, double*>::secondDerivative(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<std::__1::__wrap_iter<double*>, QuantLib::step_iterator<double*> >::secondDerivative(double) const
Unexecuted instantiation: QuantLib::detail::LinearInterpolationImpl<double const*, double const*>::secondDerivative(double) const
103
104
          private:
105
            std::vector<Real> primitiveConst_, s_;
106
        };
107
108
    }
109
110
}
111
112
#endif