QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
backwardflatlinearinterpolation.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2014 Peter Caspers
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 <http://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
24#ifndef quantlib_backwardflatlinear_interpolation_hpp
25#define quantlib_backwardflatlinear_interpolation_hpp
26
27#include <ql/math/interpolations/interpolation2d.hpp>
28
29namespace QuantLib {
30
31 namespace detail {
32
33 template <class I1, class I2, class M>
35 : public Interpolation2D::templateImpl<I1,I2,M> {
36 public:
37 BackwardflatLinearInterpolationImpl(const I1& xBegin, const I1& xEnd,
38 const I2& yBegin, const I2& yEnd,
39 const M& zData)
40 : Interpolation2D::templateImpl<I1,I2,M>(xBegin,xEnd,
41 yBegin,yEnd,
42 zData) {
43 calculate();
44 }
45 void calculate() override {}
46 Real value(Real x, Real y) const override {
47 Size j = this->locateY(y);
48 Real z1,z2;
49 if(x <= this->xBegin_[0]) {
50 z1 = this->zData_[j][0];
51 z2 = this->zData_[j+1][0];
52 }
53 else {
54 Size i = this->locateX(x);
55 if(x == this->xBegin_[i]) {
56 z1 = this->zData_[j][i];
57 z2 = this->zData_[j+1][i];
58 }
59 else {
60 z1 = this->zData_[j][i+1];
61 z2 = this->zData_[j+1][i+1];
62 }
63 }
64
65 Real u=(y-this->yBegin_[j])/
66 (this->yBegin_[j+1]-this->yBegin_[j]);
67
68 return (1.0-u)*z1 + u*z2;
69 }
70 };
71
72 }
73
78 public:
80 template <class I1, class I2, class M>
81 BackwardflatLinearInterpolation(const I1& xBegin, const I1& xEnd,
82 const I2& yBegin, const I2& yEnd,
83 const M& zData) {
84 impl_ = ext::shared_ptr<Interpolation2D::Impl>(
86 yBegin, yEnd,
87 zData));
88 }
89 };
90
92 public:
93 template <class I1, class I2, class M>
94 Interpolation2D interpolate(const I1& xBegin, const I1& xEnd,
95 const I2& yBegin, const I2& yEnd,
96 const M& z) const {
97 return BackwardflatLinearInterpolation(xBegin,xEnd,yBegin,yEnd,z);
98 }
99 };
100
101}
102
103
104#endif
Interpolation2D interpolate(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, const I2 &yEnd, const M &z) const
BackwardflatLinearInterpolation(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_
BackwardflatLinearInterpolationImpl(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, const I2 &yEnd, const M &zData)
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