1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2003 Ferdinando Ametrano
Copyright (C) 2004 StatPro Italia srl
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<https://www.quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
/*! \file bicubicsplineinterpolation.hpp
\brief bicubic spline interpolation between discrete points
*/
#ifndef quantlib_bicubic_spline_interpolation_hpp
#define quantlib_bicubic_spline_interpolation_hpp
#include <ql/math/interpolations/interpolation2d.hpp>
#include <ql/math/interpolations/cubicinterpolation.hpp>
namespace QuantLib {
namespace detail {
class BicubicSplineDerivatives {
public:
virtual ~BicubicSplineDerivatives() = default;
virtual Real derivativeX(Real x, Real y) const = 0;
virtual Real derivativeY(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;
};
template <class I1, class I2, class M>
class BicubicSplineImpl
: public Interpolation2D::templateImpl<I1,I2,M>,
public BicubicSplineDerivatives {
public:
BicubicSplineImpl(const I1& xBegin, const I1& xEnd,
const I2& yBegin, const I2& yEnd,
const M& zData)
: Interpolation2D::templateImpl<I1,I2,M>(xBegin,xEnd,
yBegin,yEnd,
zData) {
BicubicSplineImpl::calculate();
}
void calculate() override {
splines_.resize(this->zData_.rows());
for (Size i=0; i<(this->zData_.rows()); ++i)
splines_[i] = CubicInterpolation(
this->xBegin_, this->xEnd_,
this->zData_.row_begin(i),
CubicInterpolation::Spline, false,
CubicInterpolation::SecondDerivative, 0.0,
CubicInterpolation::SecondDerivative, 0.0);
}
Real value(Real x, Real y) const override {
std::vector<Real> section(splines_.size());
for (Size i=0; i<splines_.size(); i++)
section[i]=splines_[i](x,true);
CubicInterpolation spline(this->yBegin_, this->yEnd_,
section.begin(),
CubicInterpolation::Spline, false,
CubicInterpolation::SecondDerivative, 0.0,
CubicInterpolation::SecondDerivative, 0.0);
return spline(y,true);
}
Real derivativeX(Real x, Real y) const override {
std::vector<Real> section(this->zData_.columns());
for (Size i=0; i < section.size(); ++i) {
section[i] = value(this->xBegin_[i], y);
}
return CubicInterpolation(
this->xBegin_, this->xEnd_,
section.begin(),
CubicInterpolation::Spline, false,
CubicInterpolation::SecondDerivative, 0.0,
CubicInterpolation::SecondDerivative, 0.0).derivative(x);
}
Real secondDerivativeX(Real x, Real y) const override {
std::vector<Real> section(this->zData_.columns());
for (Size i=0; i < section.size(); ++i) {
section[i] = value(this->xBegin_[i], y);
}
return CubicInterpolation(
this->xBegin_, this->xEnd_,
section.begin(),
CubicInterpolation::Spline, false,
CubicInterpolation::SecondDerivative, 0.0,
CubicInterpolation::SecondDerivative, 0.0)
.secondDerivative(x);
}
Real derivativeY(Real x, Real y) const override {
std::vector<Real> section(splines_.size());
for (Size i=0; i<splines_.size(); i++)
section[i]=splines_[i](x,true);
return CubicInterpolation(
this->yBegin_, this->yEnd_,
section.begin(),
CubicInterpolation::Spline, false,
CubicInterpolation::SecondDerivative, 0.0,
CubicInterpolation::SecondDerivative, 0.0).derivative(y);
}
Real secondDerivativeY(Real x, Real y) const override {
std::vector<Real> section(splines_.size());
for (Size i=0; i<splines_.size(); i++)
section[i]=splines_[i](x,true);
return CubicInterpolation(
this->yBegin_, this->yEnd_,
section.begin(),
CubicInterpolation::Spline, false,
CubicInterpolation::SecondDerivative, 0.0,
CubicInterpolation::SecondDerivative, 0.0)
.secondDerivative(y);
}
Real derivativeXY(Real x, Real y) const override {
std::vector<Real> section(this->zData_.columns());
for (Size i=0; i < section.size(); ++i) {
section[i] = derivativeY(this->xBegin_[i], y);
}
return CubicInterpolation(
this->xBegin_, this->xEnd_,
section.begin(),
CubicInterpolation::Spline, false,
CubicInterpolation::SecondDerivative, 0.0,
CubicInterpolation::SecondDerivative, 0.0).derivative(x);
}
private:
std::vector<Interpolation> splines_;
};
}
//! bicubic-spline interpolation between discrete points
/*! \ingroup interpolations
\todo revise end conditions
\warning See the Interpolation class for information about the
required lifetime of the underlying data.
*/
class BicubicSpline : public Interpolation2D {
public:
/*! \pre the \f$ x \f$ and \f$ y \f$ values must be sorted. */
template <class I1, class I2, class M>
BicubicSpline(const I1& xBegin, const I1& xEnd,
const I2& yBegin, const I2& yEnd,
const M& zData) {
impl_ = ext::shared_ptr<Interpolation2D::Impl>(
new detail::BicubicSplineImpl<I1,I2,M>(xBegin, xEnd,
yBegin, yEnd, zData));
}
Real derivativeX(Real x, Real y) const {
return ext::dynamic_pointer_cast<detail::BicubicSplineDerivatives>
(impl_)->derivativeX(x, y);
}
Real derivativeY(Real x, Real y) const {
return ext::dynamic_pointer_cast<detail::BicubicSplineDerivatives>
(impl_)->derivativeY(x, y);
}
Real secondDerivativeX(Real x, Real y) const {
return ext::dynamic_pointer_cast<detail::BicubicSplineDerivatives>
(impl_)->secondDerivativeX(x, y);
}
Real secondDerivativeY(Real x, Real y) const {
return ext::dynamic_pointer_cast<detail::BicubicSplineDerivatives>
(impl_)->secondDerivativeY(x, y);
}
Real derivativeXY(Real x, Real y) const {
return ext::dynamic_pointer_cast<detail::BicubicSplineDerivatives>
(impl_)->derivativeXY(x, y);
}
};
//! bicubic-spline-interpolation factory
class Bicubic {
public:
template <class I1, class I2, class M>
Interpolation2D interpolate(const I1& xBegin, const I1& xEnd,
const I2& yBegin, const I2& yEnd,
const M& z) const {
return BicubicSpline(xBegin,xEnd,yBegin,yEnd,z);
}
};
}
#endif
|