QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
bicubicsplineinterpolation.hpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 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
21/*! \file bicubicsplineinterpolation.hpp
22 \brief bicubic spline interpolation between discrete points
23*/
24
25#ifndef quantlib_bicubic_spline_interpolation_hpp
26#define quantlib_bicubic_spline_interpolation_hpp
27
30
31namespace QuantLib {
32
33 namespace detail {
34
36 public:
37 virtual ~BicubicSplineDerivatives() = default;
38 virtual Real derivativeX(Real x, Real y) const = 0;
39 virtual Real derivativeY(Real x, Real y) const = 0;
40 virtual Real derivativeXY(Real x, Real y) const = 0;
41 virtual Real secondDerivativeX(Real x, Real y) const = 0;
42 virtual Real secondDerivativeY(Real x, Real y) const = 0;
43 };
44
45 template <class I1, class I2, class M>
47 : public Interpolation2D::templateImpl<I1,I2,M>,
49 public:
50 BicubicSplineImpl(const I1& xBegin, const I1& xEnd,
51 const I2& yBegin, const I2& yEnd,
52 const M& zData)
53 : Interpolation2D::templateImpl<I1,I2,M>(xBegin,xEnd,
54 yBegin,yEnd,
55 zData) {
57 }
58 void calculate() override {
59 splines_.resize(this->zData_.rows());
60 for (Size i=0; i<(this->zData_.rows()); ++i)
62 this->xBegin_, this->xEnd_,
63 this->zData_.row_begin(i),
67 }
68 Real value(Real x, Real y) const override {
69 std::vector<Real> section(splines_.size());
70 for (Size i=0; i<splines_.size(); i++)
71 section[i]=splines_[i](x,true);
72
73 CubicInterpolation spline(this->yBegin_, this->yEnd_,
74 section.begin(),
78 return spline(y,true);
79 }
80
81 Real derivativeX(Real x, Real y) const override {
82 std::vector<Real> section(this->zData_.columns());
83 for (Size i=0; i < section.size(); ++i) {
84 section[i] = value(this->xBegin_[i], y);
85 }
86
87 return CubicInterpolation(
88 this->xBegin_, this->xEnd_,
89 section.begin(),
93 }
94
95 Real secondDerivativeX(Real x, Real y) const override {
96 std::vector<Real> section(this->zData_.columns());
97 for (Size i=0; i < section.size(); ++i) {
98 section[i] = value(this->xBegin_[i], y);
99 }
100
101 return CubicInterpolation(
102 this->xBegin_, this->xEnd_,
103 section.begin(),
108 }
109
110 Real derivativeY(Real x, Real y) const override {
111 std::vector<Real> section(splines_.size());
112 for (Size i=0; i<splines_.size(); i++)
113 section[i]=splines_[i](x,true);
114
115 return CubicInterpolation(
116 this->yBegin_, this->yEnd_,
117 section.begin(),
121 }
122
123 Real secondDerivativeY(Real x, Real y) const override {
124 std::vector<Real> section(splines_.size());
125 for (Size i=0; i<splines_.size(); i++)
126 section[i]=splines_[i](x,true);
127
128 return CubicInterpolation(
129 this->yBegin_, this->yEnd_,
130 section.begin(),
135 }
136
137 Real derivativeXY(Real x, Real y) const override {
138 std::vector<Real> section(this->zData_.columns());
139 for (Size i=0; i < section.size(); ++i) {
140 section[i] = derivativeY(this->xBegin_[i], y);
141 }
142
143 return CubicInterpolation(
144 this->xBegin_, this->xEnd_,
145 section.begin(),
149 }
150
151 private:
152 std::vector<Interpolation> splines_;
153 };
154
155 }
156
157 //! bicubic-spline interpolation between discrete points
158 /*! \ingroup interpolations
159 \todo revise end conditions
160 \warning See the Interpolation class for information about the
161 required lifetime of the underlying data.
162 */
164 public:
165 /*! \pre the \f$ x \f$ and \f$ y \f$ values must be sorted. */
166 template <class I1, class I2, class M>
167 BicubicSpline(const I1& xBegin, const I1& xEnd,
168 const I2& yBegin, const I2& yEnd,
169 const M& zData) {
170 impl_ = ext::shared_ptr<Interpolation2D::Impl>(
171 new detail::BicubicSplineImpl<I1,I2,M>(xBegin, xEnd,
172 yBegin, yEnd, zData));
173 }
174
176 return ext::dynamic_pointer_cast<detail::BicubicSplineDerivatives>
177 (impl_)->derivativeX(x, y);
178 }
180 return ext::dynamic_pointer_cast<detail::BicubicSplineDerivatives>
181 (impl_)->derivativeY(x, y);
182 }
184 return ext::dynamic_pointer_cast<detail::BicubicSplineDerivatives>
185 (impl_)->secondDerivativeX(x, y);
186 }
188 return ext::dynamic_pointer_cast<detail::BicubicSplineDerivatives>
189 (impl_)->secondDerivativeY(x, y);
190 }
191
193 return ext::dynamic_pointer_cast<detail::BicubicSplineDerivatives>
194 (impl_)->derivativeXY(x, y);
195 }
196 };
197
198 //! bicubic-spline-interpolation factory
199 class Bicubic {
200 public:
201 template <class I1, class I2, class M>
202 Interpolation2D interpolate(const I1& xBegin, const I1& xEnd,
203 const I2& yBegin, const I2& yEnd,
204 const M& z) const {
205 return BicubicSpline(xBegin,xEnd,yBegin,yEnd,z);
206 }
207 };
208
209}
210
211#endif
bicubic-spline-interpolation factory
Interpolation2D interpolate(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, const I2 &yEnd, const M &z) const
bicubic-spline interpolation between discrete points
Real secondDerivativeX(Real x, Real y) const
Real secondDerivativeY(Real x, Real y) const
Real derivativeY(Real x, Real y) const
Real derivativeX(Real x, Real y) const
Real derivativeXY(Real x, Real y) const
BicubicSpline(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, const I2 &yEnd, const M &zData)
Cubic interpolation between discrete points.
@ SecondDerivative
Match value of second derivative at end.
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_
Real secondDerivative(Real x, bool allowExtrapolation=false) const
Real derivative(Real x, bool allowExtrapolation=false) const
virtual Real derivativeX(Real x, Real y) const =0
virtual Real derivativeXY(Real x, Real y) const =0
virtual Real secondDerivativeX(Real x, Real y) const =0
virtual Real secondDerivativeY(Real x, Real y) const =0
virtual Real derivativeY(Real x, Real y) const =0
BicubicSplineImpl(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, const I2 &yEnd, const M &zData)
Real secondDerivativeY(Real x, Real y) const override
Real derivativeX(Real x, Real y) const override
Real value(Real x, Real y) const override
Real secondDerivativeX(Real x, Real y) const override
Real derivativeY(Real x, Real y) const override
Real derivativeXY(Real x, Real y) const override
cubic interpolation between discrete points
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
abstract base classes for 2-D interpolations
Definition: any.hpp:35