/src/quantlib/ql/math/interpolations/lagrangeinterpolation.hpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2016 Klaus Spanderen |
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 | | #ifndef quantlib_lagrange_interpolation_hpp |
21 | | #define quantlib_lagrange_interpolation_hpp |
22 | | |
23 | | #include <ql/math/array.hpp> |
24 | | #include <ql/math/interpolation.hpp> |
25 | | #if defined(QL_EXTRA_SAFETY_CHECKS) |
26 | | #include <set> |
27 | | #endif |
28 | | |
29 | | namespace QuantLib { |
30 | | /*! References: J-P. Berrut and L.N. Trefethen, |
31 | | Barycentric Lagrange interpolation, |
32 | | SIAM Review, 46(3):501–517, 2004. |
33 | | https://people.maths.ox.ac.uk/trefethen/barycentric.pdf |
34 | | */ |
35 | | |
36 | | namespace detail { |
37 | | class UpdatedYInterpolation : public Interpolation::Impl { |
38 | | public: |
39 | | virtual Real updatedValue(const Array& yValues, Real x) const = 0; |
40 | | }; |
41 | | |
42 | | template <class I1, class I2> |
43 | | class LagrangeInterpolationImpl final |
44 | | : public Interpolation::templateImpl<I1, I2, UpdatedYInterpolation> { |
45 | | public: |
46 | | LagrangeInterpolationImpl(const I1& xBegin, const I1& xEnd, |
47 | | const I2& yBegin) |
48 | 55 | : Interpolation::templateImpl<I1, I2, UpdatedYInterpolation>(xBegin, xEnd, yBegin), |
49 | 55 | n_(std::distance(xBegin, xEnd)), |
50 | 55 | lambda_(n_) { |
51 | | #if defined(QL_EXTRA_SAFETY_CHECKS) |
52 | | QL_REQUIRE(std::set<Real>(xBegin, xEnd).size() == n_, |
53 | | "x values must not contain duplicates"); |
54 | | #endif |
55 | 55 | } QuantLib::detail::LagrangeInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*> >::LagrangeInterpolationImpl(std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&) Line | Count | Source | 48 | 55 | : Interpolation::templateImpl<I1, I2, UpdatedYInterpolation>(xBegin, xEnd, yBegin), | 49 | 55 | n_(std::distance(xBegin, xEnd)), | 50 | 55 | lambda_(n_) { | 51 | | #if defined(QL_EXTRA_SAFETY_CHECKS) | 52 | | QL_REQUIRE(std::set<Real>(xBegin, xEnd).size() == n_, | 53 | | "x values must not contain duplicates"); | 54 | | #endif | 55 | 55 | } |
Unexecuted instantiation: QuantLib::detail::LagrangeInterpolationImpl<double const*, double const*>::LagrangeInterpolationImpl(double const* const&, double const* const&, double const* const&) Unexecuted instantiation: QuantLib::detail::LagrangeInterpolationImpl<double*, double*>::LagrangeInterpolationImpl(double* const&, double* const&, double* const&) |
56 | | |
57 | 55 | void update() override { |
58 | 55 | const Real cM1 = 4.0/(*(this->xEnd_-1) - *(this->xBegin_)); |
59 | | |
60 | 782 | for (Size i=0; i < n_; ++i) { |
61 | 727 | lambda_[i] = 1.0; |
62 | | |
63 | 727 | const Real x_i = this->xBegin_[i]; |
64 | 20.8k | for (Size j=0; j < n_; ++j) { |
65 | 20.0k | if (i != j) |
66 | 19.3k | lambda_[i] *= cM1*(x_i-this->xBegin_[j]); |
67 | 20.0k | } |
68 | 727 | lambda_[i] = 1.0/lambda_[i]; |
69 | 727 | } |
70 | 55 | } QuantLib::detail::LagrangeInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*> >::update() Line | Count | Source | 57 | 55 | void update() override { | 58 | 55 | const Real cM1 = 4.0/(*(this->xEnd_-1) - *(this->xBegin_)); | 59 | | | 60 | 782 | for (Size i=0; i < n_; ++i) { | 61 | 727 | lambda_[i] = 1.0; | 62 | | | 63 | 727 | const Real x_i = this->xBegin_[i]; | 64 | 20.8k | for (Size j=0; j < n_; ++j) { | 65 | 20.0k | if (i != j) | 66 | 19.3k | lambda_[i] *= cM1*(x_i-this->xBegin_[j]); | 67 | 20.0k | } | 68 | 727 | lambda_[i] = 1.0/lambda_[i]; | 69 | 727 | } | 70 | 55 | } |
Unexecuted instantiation: QuantLib::detail::LagrangeInterpolationImpl<double const*, double const*>::update() Unexecuted instantiation: QuantLib::detail::LagrangeInterpolationImpl<double*, double*>::update() |
71 | | |
72 | 55 | Real value(Real x) const override { return _value(this->yBegin_, x); }QuantLib::detail::LagrangeInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*> >::value(double) const Line | Count | Source | 72 | 55 | Real value(Real x) const override { return _value(this->yBegin_, x); } |
Unexecuted instantiation: QuantLib::detail::LagrangeInterpolationImpl<double const*, double const*>::value(double) const Unexecuted instantiation: QuantLib::detail::LagrangeInterpolationImpl<double*, double*>::value(double) const |
73 | | |
74 | 0 | Real derivative(Real x) const override { |
75 | 0 | Real n=0.0, d=0.0, nd=0.0, dd=0.0; |
76 | 0 | for (Size i=0; i < n_; ++i) { |
77 | 0 | const Real x_i = this->xBegin_[i]; |
78 | |
|
79 | 0 | if (close_enough(x, x_i)) { |
80 | 0 | Real p=0.0; |
81 | 0 | for (Size j=0; j < n_; ++j) |
82 | 0 | if (i != j) { |
83 | 0 | p+=lambda_[j]/(x-this->xBegin_[j]) |
84 | 0 | *(this->yBegin_[j] - this->yBegin_[i]); |
85 | 0 | } |
86 | 0 | return p/lambda_[i]; |
87 | 0 | } |
88 | | |
89 | 0 | const Real alpha = lambda_[i]/(x-x_i); |
90 | 0 | const Real alphad = -alpha/(x-x_i); |
91 | 0 | n += alpha * this->yBegin_[i]; |
92 | 0 | d += alpha; |
93 | 0 | nd += alphad * this->yBegin_[i]; |
94 | 0 | dd += alphad; |
95 | 0 | } |
96 | 0 | return (nd * d - n * dd)/(d*d); |
97 | 0 | } Unexecuted instantiation: QuantLib::detail::LagrangeInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*> >::derivative(double) const Unexecuted instantiation: QuantLib::detail::LagrangeInterpolationImpl<double const*, double const*>::derivative(double) const Unexecuted instantiation: QuantLib::detail::LagrangeInterpolationImpl<double*, double*>::derivative(double) const |
98 | | |
99 | 55 | Real primitive(Real) const override { |
100 | 55 | QL_FAIL("LagrangeInterpolation primitive is not implemented"); |
101 | 55 | } QuantLib::detail::LagrangeInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*> >::primitive(double) const Line | Count | Source | 99 | 55 | Real primitive(Real) const override { | 100 | | QL_FAIL("LagrangeInterpolation primitive is not implemented"); | 101 | 55 | } |
Unexecuted instantiation: QuantLib::detail::LagrangeInterpolationImpl<double const*, double const*>::primitive(double) const Unexecuted instantiation: QuantLib::detail::LagrangeInterpolationImpl<double*, double*>::primitive(double) const |
102 | | |
103 | 0 | Real secondDerivative(Real) const override { |
104 | 0 | QL_FAIL("LagrangeInterpolation secondDerivative " |
105 | 0 | "is not implemented"); |
106 | 0 | } Unexecuted instantiation: QuantLib::detail::LagrangeInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*> >::secondDerivative(double) const Unexecuted instantiation: QuantLib::detail::LagrangeInterpolationImpl<double const*, double const*>::secondDerivative(double) const Unexecuted instantiation: QuantLib::detail::LagrangeInterpolationImpl<double*, double*>::secondDerivative(double) const |
107 | | |
108 | 0 | Real updatedValue(const Array& y, Real x) const override { |
109 | 0 | return _value(y.begin(), x); |
110 | 0 | } Unexecuted instantiation: QuantLib::detail::LagrangeInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*> >::updatedValue(QuantLib::Array const&, double) const Unexecuted instantiation: QuantLib::detail::LagrangeInterpolationImpl<double const*, double const*>::updatedValue(QuantLib::Array const&, double) const Unexecuted instantiation: QuantLib::detail::LagrangeInterpolationImpl<double*, double*>::updatedValue(QuantLib::Array const&, double) const |
111 | | |
112 | | private: |
113 | | template <class Iterator> |
114 | 55 | Real _value(const Iterator& yBegin, Real x) const { |
115 | | |
116 | 55 | const Real eps = 10*QL_EPSILON*std::abs(x); |
117 | 55 | const auto iter = std::lower_bound( |
118 | 55 | this->xBegin_, this->xEnd_, x - eps); |
119 | 55 | if (iter != this->xEnd_ && *iter - x < eps) { |
120 | 1 | return yBegin[std::distance(this->xBegin_, iter)]; |
121 | 1 | } |
122 | | |
123 | 54 | Real n = 0.0, d = 0.0; |
124 | 778 | for (Size i = 0; i < n_; ++i) { |
125 | 724 | const Real alpha = lambda_[i] / (x - this->xBegin_[i]); |
126 | 724 | n += alpha * yBegin[i]; |
127 | 724 | d += alpha; |
128 | 724 | } |
129 | 54 | return n / d; |
130 | 55 | } double QuantLib::detail::LagrangeInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*> >::_value<std::__1::__wrap_iter<double*> >(std::__1::__wrap_iter<double*> const&, double) const Line | Count | Source | 114 | 55 | Real _value(const Iterator& yBegin, Real x) const { | 115 | | | 116 | 55 | const Real eps = 10*QL_EPSILON*std::abs(x); | 117 | 55 | const auto iter = std::lower_bound( | 118 | 55 | this->xBegin_, this->xEnd_, x - eps); | 119 | 55 | if (iter != this->xEnd_ && *iter - x < eps) { | 120 | 1 | return yBegin[std::distance(this->xBegin_, iter)]; | 121 | 1 | } | 122 | | | 123 | 54 | Real n = 0.0, d = 0.0; | 124 | 778 | for (Size i = 0; i < n_; ++i) { | 125 | 724 | const Real alpha = lambda_[i] / (x - this->xBegin_[i]); | 126 | 724 | n += alpha * yBegin[i]; | 127 | 724 | d += alpha; | 128 | 724 | } | 129 | 54 | return n / d; | 130 | 55 | } |
Unexecuted instantiation: double QuantLib::detail::LagrangeInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*> >::_value<double const*>(double const* const&, double) const Unexecuted instantiation: double QuantLib::detail::LagrangeInterpolationImpl<double const*, double const*>::_value<double const*>(double const* const&, double) const Unexecuted instantiation: double QuantLib::detail::LagrangeInterpolationImpl<double*, double*>::_value<double*>(double* const&, double) const Unexecuted instantiation: double QuantLib::detail::LagrangeInterpolationImpl<double*, double*>::_value<double const*>(double const* const&, double) const |
131 | | |
132 | | const Size n_; |
133 | | Array lambda_; |
134 | | }; |
135 | | } |
136 | | |
137 | | /*! \ingroup interpolations |
138 | | \warning See the Interpolation class for information about the |
139 | | required lifetime of the underlying data. |
140 | | */ |
141 | | class LagrangeInterpolation : public Interpolation { |
142 | | public: |
143 | | template <class I1, class I2> |
144 | | LagrangeInterpolation(const I1& xBegin, const I1& xEnd, |
145 | 55 | const I2& yBegin) { |
146 | 55 | impl_ = ext::make_shared<detail::LagrangeInterpolationImpl<I1,I2> >( |
147 | 55 | xBegin, xEnd, yBegin); |
148 | 55 | impl_->update(); |
149 | 55 | } QuantLib::LagrangeInterpolation::LagrangeInterpolation<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&) Line | Count | Source | 145 | 55 | const I2& yBegin) { | 146 | 55 | impl_ = ext::make_shared<detail::LagrangeInterpolationImpl<I1,I2> >( | 147 | 55 | xBegin, xEnd, yBegin); | 148 | 55 | impl_->update(); | 149 | 55 | } |
Unexecuted instantiation: QuantLib::LagrangeInterpolation::LagrangeInterpolation<double const*, double const*>(double const* const&, double const* const&, double const* const&) Unexecuted instantiation: QuantLib::LagrangeInterpolation::LagrangeInterpolation<double*, double*>(double* const&, double* const&, double* const&) |
150 | | |
151 | | // interpolate with new set of y values for a new x value |
152 | 0 | Real value(const Array& y, Real x) const { |
153 | 0 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-static-cast-downcast) |
154 | 0 | return static_cast<detail::UpdatedYInterpolation&>(*impl_).updatedValue(y, x); |
155 | 0 | } |
156 | | }; |
157 | | |
158 | | } |
159 | | |
160 | | #endif |