QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
bilinearinterpolation.hpp
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 <http://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
25#ifndef quantlib_bilinear_interpolation_hpp
26#define quantlib_bilinear_interpolation_hpp
27
28#include <ql/math/interpolations/interpolation2d.hpp>
29
30namespace QuantLib {
31
32 namespace detail {
33
34 template <class I1, class I2, class M>
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 : Interpolation2D::templateImpl<I1,I2,M>(xBegin,xEnd,
42 yBegin,yEnd,
43 zData) {
45 }
46 void calculate() override {}
47 Real value(Real x, Real y) const override {
48 Size i = this->locateX(x), j = this->locateY(y);
49
50 Real z1 = this->zData_[j][i];
51 Real z2 = this->zData_[j][i+1];
52 Real z3 = this->zData_[j+1][i];
53 Real z4 = this->zData_[j+1][i+1];
54
55 Real t=(x-this->xBegin_[i])/
56 (this->xBegin_[i+1]-this->xBegin_[i]);
57 Real u=(y-this->yBegin_[j])/
58 (this->yBegin_[j+1]-this->yBegin_[j]);
59
60 return (1.0-t)*(1.0-u)*z1 + t*(1.0-u)*z2
61 + (1.0-t)*u*z3 + t*u*z4;
62 }
63 };
64
65 }
66
68
73 public:
75 template <class I1, class I2, class M>
76 BilinearInterpolation(const I1& xBegin, const I1& xEnd,
77 const I2& yBegin, const I2& yEnd,
78 const M& zData) {
79 impl_ = ext::shared_ptr<Interpolation2D::Impl>(
81 yBegin, yEnd,
82 zData));
83 }
84 };
85
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 const M& z) const {
93 return BilinearInterpolation(xBegin,xEnd,yBegin,yEnd,z);
94 }
95 };
96
97}
98
99
100#endif
bilinear-interpolation factory
Interpolation2D interpolate(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, const I2 &yEnd, const M &z) const
bilinear interpolation between discrete points
BilinearInterpolation(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, const I2 &yEnd, const M &zData)
basic template implementation
const Matrix & zData() const override
templateImpl(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, const I2 &yEnd, const M &zData)
base class for 2-D interpolations.
const Matrix & zData() const
ext::shared_ptr< Impl > impl_
BilinearInterpolationImpl(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, const I2 &yEnd, const M &zData)
Real value(Real x, Real y) const override
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35