/src/quantlib/ql/math/interpolations/bilinearinterpolation.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) 2002, 2003 Ferdinando Ametrano |
5 | | Copyright (C) 2004 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 bilinearinterpolation.hpp |
22 | | \brief bilinear interpolation between discrete points |
23 | | */ |
24 | | |
25 | | #ifndef quantlib_bilinear_interpolation_hpp |
26 | | #define quantlib_bilinear_interpolation_hpp |
27 | | |
28 | | #include <ql/math/interpolations/interpolation2d.hpp> |
29 | | |
30 | | namespace QuantLib { |
31 | | |
32 | | namespace detail { |
33 | | |
34 | | template <class I1, class I2, class M> |
35 | | class BilinearInterpolationImpl |
36 | | : public Interpolation2D::templateImpl<I1,I2,M> { |
37 | | public: |
38 | | BilinearInterpolationImpl(const I1& xBegin, const I1& xEnd, |
39 | | const I2& yBegin, const I2& yEnd, |
40 | | const M& zData) |
41 | 0 | : Interpolation2D::templateImpl<I1,I2,M>(xBegin,xEnd, |
42 | 0 | yBegin,yEnd, |
43 | 0 | zData) { |
44 | 0 | BilinearInterpolationImpl::calculate(); |
45 | 0 | } Unexecuted instantiation: QuantLib::detail::BilinearInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*>, QuantLib::Matrix>::BilinearInterpolationImpl(std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&, QuantLib::Matrix const&) Unexecuted instantiation: QuantLib::detail::BilinearInterpolationImpl<std::__1::__wrap_iter<double const*>, std::__1::__wrap_iter<double const*>, QuantLib::Matrix>::BilinearInterpolationImpl(std::__1::__wrap_iter<double const*> const&, std::__1::__wrap_iter<double const*> const&, std::__1::__wrap_iter<double const*> const&, std::__1::__wrap_iter<double const*> const&, QuantLib::Matrix const&) |
46 | 0 | void calculate() override {} Unexecuted instantiation: QuantLib::detail::BilinearInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*>, QuantLib::Matrix>::calculate() Unexecuted instantiation: QuantLib::detail::BilinearInterpolationImpl<std::__1::__wrap_iter<double const*>, std::__1::__wrap_iter<double const*>, QuantLib::Matrix>::calculate() |
47 | 0 | Real value(Real x, Real y) const override { |
48 | 0 | Size i = this->locateX(x), j = this->locateY(y); |
49 | |
|
50 | 0 | Real z1 = this->zData_[j][i]; |
51 | 0 | Real z2 = this->zData_[j][i+1]; |
52 | 0 | Real z3 = this->zData_[j+1][i]; |
53 | 0 | Real z4 = this->zData_[j+1][i+1]; |
54 | |
|
55 | 0 | Real t=(x-this->xBegin_[i])/ |
56 | 0 | (this->xBegin_[i+1]-this->xBegin_[i]); |
57 | 0 | Real u=(y-this->yBegin_[j])/ |
58 | 0 | (this->yBegin_[j+1]-this->yBegin_[j]); |
59 | |
|
60 | 0 | return (1.0-t)*(1.0-u)*z1 + t*(1.0-u)*z2 |
61 | 0 | + (1.0-t)*u*z3 + t*u*z4; |
62 | 0 | } Unexecuted instantiation: QuantLib::detail::BilinearInterpolationImpl<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*>, QuantLib::Matrix>::value(double, double) const Unexecuted instantiation: QuantLib::detail::BilinearInterpolationImpl<std::__1::__wrap_iter<double const*>, std::__1::__wrap_iter<double const*>, QuantLib::Matrix>::value(double, double) const |
63 | | }; |
64 | | |
65 | | } |
66 | | |
67 | | //! %bilinear interpolation between discrete points |
68 | | /*! \ingroup interpolations |
69 | | \warning See the Interpolation class for information about the |
70 | | required lifetime of the underlying data. |
71 | | */ |
72 | | class BilinearInterpolation : public Interpolation2D { |
73 | | public: |
74 | | /*! \pre the \f$ x \f$ and \f$ y \f$ values must be sorted. */ |
75 | | template <class I1, class I2, class M> |
76 | | BilinearInterpolation(const I1& xBegin, const I1& xEnd, |
77 | | const I2& yBegin, const I2& yEnd, |
78 | 0 | const M& zData) { |
79 | 0 | impl_ = ext::shared_ptr<Interpolation2D::Impl>( |
80 | 0 | new detail::BilinearInterpolationImpl<I1,I2,M>(xBegin, xEnd, |
81 | 0 | yBegin, yEnd, |
82 | 0 | zData)); |
83 | 0 | } Unexecuted instantiation: QuantLib::BilinearInterpolation::BilinearInterpolation<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*>, QuantLib::Matrix>(std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&, QuantLib::Matrix const&) Unexecuted instantiation: QuantLib::BilinearInterpolation::BilinearInterpolation<std::__1::__wrap_iter<double const*>, std::__1::__wrap_iter<double const*>, QuantLib::Matrix>(std::__1::__wrap_iter<double const*> const&, std::__1::__wrap_iter<double const*> const&, std::__1::__wrap_iter<double const*> const&, std::__1::__wrap_iter<double const*> const&, QuantLib::Matrix const&) |
84 | | }; |
85 | | |
86 | | //! bilinear-interpolation factory |
87 | | class Bilinear { |
88 | | public: |
89 | | template <class I1, class I2, class M> |
90 | | Interpolation2D interpolate(const I1& xBegin, const I1& xEnd, |
91 | | const I2& yBegin, const I2& yEnd, |
92 | 0 | const M& z) const { |
93 | 0 | return BilinearInterpolation(xBegin,xEnd,yBegin,yEnd,z); |
94 | 0 | } Unexecuted instantiation: QuantLib::Interpolation2D QuantLib::Bilinear::interpolate<std::__1::__wrap_iter<double*>, std::__1::__wrap_iter<double*>, QuantLib::Matrix>(std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&, std::__1::__wrap_iter<double*> const&, QuantLib::Matrix const&) const Unexecuted instantiation: QuantLib::Interpolation2D QuantLib::Bilinear::interpolate<std::__1::__wrap_iter<double const*>, std::__1::__wrap_iter<double const*>, QuantLib::Matrix>(std::__1::__wrap_iter<double const*> const&, std::__1::__wrap_iter<double const*> const&, std::__1::__wrap_iter<double const*> const&, std::__1::__wrap_iter<double const*> const&, QuantLib::Matrix const&) const |
95 | | }; |
96 | | |
97 | | } |
98 | | |
99 | | |
100 | | #endif |