Version: SMASH-3.4
smash::InterpolateData2DSpline Class Reference

#include <interpolation2D.h>

Represent a bicubic spline interpolation.

Definition at line 23 of file interpolation2D.h.

Collaboration diagram for smash::InterpolateData2DSpline:
[legend]

Public Member Functions

 InterpolateData2DSpline (const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &z, ExtrapolationType extrapolation_type=ExtrapolationType::None)
 Interpolate function f given discrete samples f(x_i, y_i) = z_i. More...
 
 ~InterpolateData2DSpline ()
 Destructor. More...
 
double operator() (double xi, double yi) const
 Calculate bicubic interpolation for given x and y. More...
 

Private Attributes

ExtrapolationType extrapolation_type_ = ExtrapolationType::None
 Extrapolation type. More...
 
double first_x_ = smash_NaN<double>
 First x value of underlying data. More...
 
double last_x_ = smash_NaN<double>
 Last x value of underlying data. More...
 
double first_y_ = smash_NaN<double>
 First y value of underlying data. More...
 
double last_y_ = smash_NaN<double>
 Last y value of underlying data. More...
 
gsl_interp_accel * xacc_ = nullptr
 GSL iterator for interpolation lookups in x direction. More...
 
gsl_interp_accel * yacc_ = nullptr
 GSL iterator for interpolation lookupin y direction. More...
 
gsl_spline2d * spline_ = nullptr
 GSL spline in 2D. More...
 

Constructor & Destructor Documentation

◆ InterpolateData2DSpline()

smash::InterpolateData2DSpline::InterpolateData2DSpline ( const std::vector< double > &  x,
const std::vector< double > &  y,
const std::vector< double > &  z,
ExtrapolationType  extrapolation_type = ExtrapolationType::None 
)

Interpolate function f given discrete samples f(x_i, y_i) = z_i.

A bicubic spline interpolation is used.

Parameters
xx-values.
yy-values.
zz-values
extrapolation_typeType of extrapolation for requested x_i and y_i values that are out of bounds. Extrapolation is by default disabled. Possible types are None, Zero, and Constant.
Returns
The interpolation function.
Exceptions
std::out_of_rangeif values outside of the boundaries of the underlying data are tried to be accessed and extrapolation is disabled.
std::invalid_argumentif unsupported extrapolation type is requested.
std::invalid_argumentif product of dimensions of x and y does not equal dimension of z.
std::invalid_argumentif less than three data points in x or y are provided.
std::invalid_argumentif x and y are not sorted that their values strictly increase.

Definition at line 18 of file interpolation2D.cc.

21  : extrapolation_type_{extrapolation_type} {
22  switch (extrapolation_type_) {
26  break;
27  default:
28  throw std::invalid_argument(
29  "The provided extrapolation type is not supported. Valid types are "
30  "'None', 'Zero', and 'Constant'.");
31  }
32 
33  const size_t M = x.size();
34  const size_t N = y.size();
35 
36  if (z.size() != N * M) {
37  throw std::invalid_argument(
38  "Dimensions not suitable for 2D interpolation. DIM(z) != DIM(x) * "
39  "DIM(y).");
40  }
41 
42  if (M < 4 || N < 4) {
43  throw std::invalid_argument(
44  "Need at least 4 data points in each dimension for bicubic spline "
45  "interpolation.");
46  }
47 
48  if (!std::is_sorted(x.begin(), x.end()) ||
49  !std::is_sorted(y.begin(), y.end())) {
50  throw std::invalid_argument(
51  "x and y values must be strictly increasing, i.e. the vectors have to "
52  "be sorted by size of their values. This is required by GSL.");
53  }
54 
55  // Assign lower and upper bounds for constant extrapolation
56  first_x_ = x.front();
57  last_x_ = x.back();
58  first_y_ = y.front();
59  last_y_ = y.back();
60 
61  // cast vectors into arrays, as GSL functions can only handle arrays
62  const double* xa = &x[0];
63  const double* ya = &y[0];
64  const double* za = &z[0];
65 
66  // Create accelerator objects (interpolation lookups)
67  xacc_ = gsl_interp_accel_alloc();
68  yacc_ = gsl_interp_accel_alloc();
69 
70  // Initialize bicubic spline interpolation
71  spline_ = gsl_spline2d_alloc(gsl_interp2d_bicubic, M, N);
72  gsl_spline2d_init(spline_, xa, ya, za, M, N);
73 }
ExtrapolationType extrapolation_type_
Extrapolation type.
double first_y_
First y value of underlying data.
double last_y_
Last y value of underlying data.
double last_x_
Last x value of underlying data.
gsl_interp_accel * xacc_
GSL iterator for interpolation lookups in x direction.
gsl_spline2d * spline_
GSL spline in 2D.
double first_x_
First x value of underlying data.
gsl_interp_accel * yacc_
GSL iterator for interpolation lookupin y direction.
@ None
No extrapolation is done.
@ Constant
Extrapolate using a constant value.
@ Zero
Extrapolate with zero.

◆ ~InterpolateData2DSpline()

smash::InterpolateData2DSpline::~InterpolateData2DSpline ( )

Destructor.

Definition at line 75 of file interpolation2D.cc.

75  {
76  gsl_spline2d_free(spline_);
77  gsl_interp_accel_free(xacc_);
78  gsl_interp_accel_free(yacc_);
79 }

Member Function Documentation

◆ operator()()

double smash::InterpolateData2DSpline::operator() ( double  xi,
double  yi 
) const

Calculate bicubic interpolation for given x and y.

Parameters
xiInterpolation argument in first dimension.
yiInterpolation argument in second dimension.
Returns
Interpolated value.
Exceptions
std::out_of_rangeif values outside of the boundaries of the underlying data are tried to be accessed and extrapolation is disabled.

Definition at line 81 of file interpolation2D.cc.

81  {
82  if ((xi < first_x_ || xi > last_x_) || (yi < first_y_ || yi > last_y_)) {
84  std::ostringstream error_msg{
85  "InterpolateData2DSpline only accepts x and y values within the "
86  "range of the underlying data\nwhen extrapolation is not specified. ",
87  std::ios::ate};
88  error_msg << "x value " << xi << " or y value " << yi
89  << " are out of bounds.";
90  throw std::out_of_range(error_msg.str());
92  return 0.;
94  // constant extrapolation at the edges
95  xi = (xi < first_x_) ? first_x_ : xi;
96  xi = (xi > last_x_) ? last_x_ : xi;
97  yi = (yi < first_y_) ? first_y_ : yi;
98  yi = (yi > last_y_) ? last_y_ : yi;
99  }
100  }
101 
102  // bicubic spline interpolation
103  return gsl_spline2d_eval(spline_, xi, yi, xacc_, yacc_);
104 }

Member Data Documentation

◆ extrapolation_type_

ExtrapolationType smash::InterpolateData2DSpline::extrapolation_type_ = ExtrapolationType::None
private

Extrapolation type.

Definition at line 75 of file interpolation2D.h.

◆ first_x_

double smash::InterpolateData2DSpline::first_x_ = smash_NaN<double>
private

First x value of underlying data.

Definition at line 77 of file interpolation2D.h.

◆ last_x_

double smash::InterpolateData2DSpline::last_x_ = smash_NaN<double>
private

Last x value of underlying data.

Definition at line 79 of file interpolation2D.h.

◆ first_y_

double smash::InterpolateData2DSpline::first_y_ = smash_NaN<double>
private

First y value of underlying data.

Definition at line 81 of file interpolation2D.h.

◆ last_y_

double smash::InterpolateData2DSpline::last_y_ = smash_NaN<double>
private

Last y value of underlying data.

Definition at line 83 of file interpolation2D.h.

◆ xacc_

gsl_interp_accel* smash::InterpolateData2DSpline::xacc_ = nullptr
private

GSL iterator for interpolation lookups in x direction.

Definition at line 86 of file interpolation2D.h.

◆ yacc_

gsl_interp_accel* smash::InterpolateData2DSpline::yacc_ = nullptr
private

GSL iterator for interpolation lookupin y direction.

Definition at line 88 of file interpolation2D.h.

◆ spline_

gsl_spline2d* smash::InterpolateData2DSpline::spline_ = nullptr
private

GSL spline in 2D.

Definition at line 90 of file interpolation2D.h.


The documentation for this class was generated from the following files: