24#ifndef quantlib_multi_cubic_spline_hpp
25#define quantlib_multi_cubic_spline_hpp
49 DataTable(
const std::vector<Size>::const_iterator &i) {
50 std::vector<X> temp(*i, X(i + 1));
54 std::vector<X> temp(i->size(), X(i + 1));
71 DataTable(
const std::vector<Size>::const_iterator& i)
89 template<
class X,
class Y>
struct Data {
92 Data(
const SplineGrid::const_iterator &i)
106 Data(
const SplineGrid::const_iterator &i)
122 template<
class X,
class Y>
struct Point {
126 Point(
const std::vector<Real>::const_iterator &i)
130 Point(
const SplineGrid::const_iterator &i)
147 Point(
const std::vector<Real>::const_iterator &i)
153 QL_REQUIRE(
n == 0,
"operator[] : access violation");
157 QL_REQUIRE(
n == 0,
"operator[] : access violation");
174 QL_REQUIRE(
n == 0,
"operator[] : access violation");
178 QL_REQUIRE(
n == 0,
"operator[] : access violation");
195 QL_REQUIRE(
n == 0,
"operator[] : access violation");
199 QL_REQUIRE(
n == 0,
"operator[] : access violation");
211 Point(
const SplineGrid::const_iterator &i)
212 :
first(i->size()) {}
214 :
first(grid[0].size()) {}
238 Size dim =
d.first.size();
239 Size j = 1, k = 2, l = 3;
241 &u = ((y2[0] =
y[2]) -=
y[1]) /=
d[1], &
t =
v[dim];
242 y2[1] = -
d[1] / d2[0],
v[1] = 6.0 * (u - w) / d2[0];
243 for(; k < dim; u = w, j = k, k = l, ++l) {
244 w = (
y[l]-
y[k])/
d[k];
246 (y2[k] =
d[k]) /= ((
t = -y2[j]) *=
d[j]) -= d2[j];
247 (
v[k] = (u +=
d[j] *
v[j])) /=
t;
249 y2[0] = y2[dim] = 0.0;
251 (y2[k-1] *= y2[l-1]) +=
v[k-1];
266 for(
Size j = 0, dim =
y_.
size(); j < dim; ++j)
295 res = a *
y[i] +
b *
y[i + 1] + a2 * y2[i] + b2 * y2[i + 1];
318 for(
Size j = 0, dim =
y_.
size(); j < dim; ++j)
478 const std::vector<bool>& ae =
479 std::vector<bool>(20,
false))
496 const std::vector<bool> &
ae_;
511 for(
Size j = 0; j < i; k = 0, ++j) {
512 const std::vector<Real> &
v = grid_[j];
513 if((dim =
v.size() - 1) > 2) {
514 std::vector<Real> tmp1(dim);
516 std::vector<Real> tmp2(dim - 1);
518 for(; k < dim; ++k) {
519 if((x[j][k] =
v[k + 1] -
v[k]) <= 0.0)
break;
521 y[j][k - 1] = 2.0 * (
v[k + 1] -
v[k - 1]);
526 <<
" : not enough points for interpolation");
528 "Dimension " << j <<
" : invalid data");
542 for(
Size j = 0; j < i; ++j) {
543 Size &k = i_[j], sz = grid_[j].size() - 1;
544 const std::vector<Real> &
v = grid_[j];
545 if(x[j] <
v[0] || x[j] >=
v[sz]) {
548 <<
": extrapolation is not allowed.");
549 a_[j] = 1.0, a2_[j] =
b_[j] = b2_[j] = 0.0;
550 k = x[j] <
v[0] ? 0 : sz;
553 k =
v[k] <= x[j] && x[j] <
v[k + 1] ? k :
554 std::upper_bound(
v.begin(),
v.end(),x[j])-
v.begin()-1;
556 a_[j] = (
v[k + 1] - x[j]) / h,
b_[j] = (x[j] -
v[k]) / h;
557 a2_[j] = (
a_[j] *
a_[j] *
a_[j] -
a_[j]) * h * h / 6.0,
558 b2_[j] = (
b_[j] *
b_[j] *
b_[j] -
b_[j]) * h * h / 6.0;
N-dimensional cubic spline interpolation between discrete points.
c_splint::argument_type argument_type
c_splint::result_type result_type
c_splint::dimensions dimensions
void set_shared_increments() const
c_splint::output_data output_data
const std::vector< bool > & ae_
result_type operator()(const argument_type &x) const
void set_shared_coefficients(const argument_type &x) const
c_splint::return_type return_type
MultiCubicSpline(const SplineGrid &grid, const data_table &y, const std::vector< bool > &ae=std::vector< bool >(20, false))
c_splint::data_table data_table
detail::Int2Type< i >::c_splint c_splint
detail::Int2Type< i >::c_spline c_spline
base_data_table data_table
base_cubic_spline(const data &d, const data &d2, const data_table &y, data_table &y2, output_data &v)
base_output_data output_data
base_arg_type argument_type
base_cubic_splint(const return_type &a, const return_type &b, const return_type &a2, const return_type &b2, const dimensions &i, const data &, const data &, const data_table &y, data_table &y2, output_data &, output_data &, output_data &, result_type &res)
base_data_table data_table
base_return_type return_type
base_dimensions dimensions
base_output_data output_data
DataTable< typename X::data_table > data_table
n_cubic_spline(const data &d, const data &d2, const data_table &y, data_table &y2, output_data &v)
Data< base_data, typename X::data > data
Point< base_output_data, typename X::output_data > output_data
~n_cubic_spline()=default
DataTable< typename X::data_table > data_table
Point< Size, typename X::dimensions > dimensions
Point< Real, typename X::argument_type > argument_type
Data< base_data, typename X::data > data
Point< base_output_data, typename X::output_data > output_data
~n_cubic_splint()=default
n_cubic_splint(const return_type &a, const return_type &b, const return_type &a2, const return_type &b2, const dimensions &i, const data &d, const data &d2, const data_table &y, data_table &y2, output_data &v, output_data &v1, output_data &v2, result_type &r)
Point< result_type, typename X::return_type > return_type
Classes and functions for error handling.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
ext::function< Real(Real)> b
std::size_t Size
size of a container
n_cubic_splint< cubic_splint_13 > cubic_splint_14
n_cubic_splint< cubic_splint_05 > cubic_splint_06
Point< Size, EmptyDim > base_dimensions
base_cubic_spline cubic_spline_01
Data< std::vector< Real >, EmptyArg > base_data
n_cubic_splint< cubic_splint_12 > cubic_splint_13
n_cubic_spline< cubic_spline_12 > cubic_spline_13
n_cubic_splint< cubic_splint_02 > cubic_splint_03
n_cubic_splint< cubic_splint_01 > cubic_splint_02
Point< Real, EmptyRes > base_return_type
n_cubic_spline< cubic_spline_02 > cubic_spline_03
n_cubic_splint< cubic_splint_11 > cubic_splint_12
n_cubic_spline< cubic_spline_07 > cubic_spline_08
n_cubic_spline< cubic_spline_10 > cubic_spline_11
n_cubic_splint< cubic_splint_03 > cubic_splint_04
base_cubic_splint cubic_splint_01
n_cubic_spline< cubic_spline_04 > cubic_spline_05
n_cubic_spline< cubic_spline_05 > cubic_spline_06
Point< base_data_table, EmptyRes > base_output_data
n_cubic_splint< cubic_splint_10 > cubic_splint_11
n_cubic_splint< cubic_splint_08 > cubic_splint_09
n_cubic_splint< cubic_splint_14 > cubic_splint_15
Point< Real, EmptyArg > base_arg_type
DataTable< Real > base_data_table
n_cubic_splint< cubic_splint_06 > cubic_splint_07
n_cubic_splint< cubic_splint_09 > cubic_splint_10
n_cubic_splint< cubic_splint_04 > cubic_splint_05
n_cubic_spline< cubic_spline_14 > cubic_spline_15
std::vector< std::vector< Real > > SplineGrid
n_cubic_spline< cubic_spline_03 > cubic_spline_04
n_cubic_splint< cubic_splint_07 > cubic_splint_08
n_cubic_spline< cubic_spline_11 > cubic_spline_12
n_cubic_spline< cubic_spline_09 > cubic_spline_10
n_cubic_spline< cubic_spline_01 > cubic_spline_02
n_cubic_spline< cubic_spline_06 > cubic_spline_07
n_cubic_spline< cubic_spline_08 > cubic_spline_09
n_cubic_spline< cubic_spline_13 > cubic_spline_14
detail::SplineGrid SplineGrid
ext::shared_ptr< YieldTermStructure > r
ext::shared_ptr< BlackVolTermStructure > v
Real operator[](Size n) const
std::vector< Real > first
Data(std::vector< Real > v)
void swap(Data< std::vector< Real >, EmptyArg > &d) noexcept
Data(const SplineGrid &v)
Real & operator[](Size n)
Data(const SplineGrid::const_iterator &i)
void swap(Data< X, Y > &d) noexcept
Data(const SplineGrid &v)
Data(const SplineGrid::const_iterator &i)
std::vector< Real > data_table_
DataTable(const std::vector< Size >::const_iterator &i)
Real operator[](Size n) const
DataTable(const std::vector< U > &v)
Real & operator[](Size n)
DataTable(const SplineGrid::const_iterator &i)
DataTable(const std::vector< Size >::const_iterator &i)
std::vector< X > data_table_
const X & operator[](Size n) const
DataTable(const std::vector< U > &v)
DataTable(const SplineGrid::const_iterator &i)
data_type & operator[](Size n)
Point(const std::vector< Real >::const_iterator &i)
Point(const std::vector< Real > &v)
data_type operator[](Size n) const
data_type & operator[](Size n)
const data_type & operator[](Size n) const
data_type & operator[](Size n)
data_type operator[](Size n) const
Real operator[](Size n) const
Point(const SplineGrid::const_iterator &i)
base_data_table data_type
Real & operator[](Size n)
Point(const SplineGrid &grid)
data_type & operator[](Size n)
Point(const std::vector< Real >::const_iterator &i)
Point(const std::vector< Real > &v)
data_type operator[](Size n) const
Point(const SplineGrid::const_iterator &i)
Point(const SplineGrid &grid)