Class interpm_idw (o2scl)

O2scl : Class List

template<class mat_t = const_matrix_view_table<>>
class interpm_idw

Multi-dimensional interpolation by inverse distance weighting.

This class is experimental, particularly the evaluation of derivatives.

This class performs interpolation on a multi-dimensional data set specified as a series of scattered points using the inverse distance-weighted average of nearby points.

The function set_data() takes as input: the number of input dimensions, the number of output functions, the number of points which specify the data, and a “vector of vectors” which contains the data for all the points. The vector of vectors must be of a type which allows std::swap on individual elements (which are of type vec_t). The vector of vectors should be structured so that the first index selects the input or output function and the second index selects the data point.

The number of nearby points which are averaged defaults to 3 and can be changed by set_points(). To obtain interpolation uncertainties, this class finds one additional nearby point and returns the standard deviation of the interpolated value for all subsets which are missing one nearby point. One-point interpolation corresponds to nearest-neighbor interpolation.

This class requires a distance metric to weight the interpolation, and a Euclidean distance is used. By default, the length scales in each direction are automatically determined by extent of the data (absolute value of max minus min in each direction), but the user can specify the length scales manually in set_scales() .

First derivatives can be obtained using derivs_err() , but these derivatives are not obtained from the same approximation used in the interpolation. That is, the derivatives returned are not equal to exact derivatives from the interpolated function (as is the case in, e.g., cubic spline interpolation in one dimension). This will typically only be particularly noticable near discontinuities.

If the user specifies an array of pointers, the data can be changed between calls to the interpolation, but data points cannot be added (as set data separately stores the total number of data points) without a new call to set_data(). Also, the automatically-determined length scales may need to be recomputed by calling auto_scale().

Increasing the value of n_extra away from zero allows the interpolation to ignore points in the data set which are degenerate because they are too close to each other. Points with a distance (normalized by the scales) less than min_dist are automatically considered degenerate and only the single point closest to the requested coordinate is considered. Increasing the value of n_extra increases the computational time required to compute the nearest points which are nondegenerate.

Evaluate derivatives

size_t np

The number of points.

size_t nd_in

The number of dimensions of the inputs.

size_t nd_out

The number of dimensions of the outputs.

mat_t data

The copy of the data.

bool data_set

True if the data has been specified.

size_t points

Number of points to include in each interpolation (default 3)

template<class vec3_t>
inline void derivs_err(size_t func_index, size_t point_index, vec3_t &derivs, vec3_t &errs) const

For one of the functions, compute the partial derivatives (and uncertainties) with respect to all of the inputs at one data point.

Todo

  • Use the mechanism provided by <tt>n_extra</tt> above to remove degenerate points.

  • Future: This function requires an extra copy from “ders” to “ders2” which could be removed.

Note

This function ignores the points chosen by set_points() and always chooses to average derivative calculations determined from n_in+1 combinations of n_in points .

Distance determination [protected]

ubvector scales

Distance scales for each coordinate.

template<class vec2_t>
inline double dist(size_t index, const vec2_t &x) const

Compute the distance between x and the point at index index.

inline double dist(size_t j, size_t k) const

Compute the distance between two points in the data set.

Get and set functions

inline void set_points(size_t n)

Set the number of closest points to use for each interpolation (default 3)

template<class vec2_t>
inline void set_scales(size_t n, vec2_t &v)

Set the scales for the distance metric.

All the scales must be positive and non-zero. The size of the vector (specified in n) must be larger than zero.

inline void set_data(size_t n_in, size_t n_out, size_t n_points, mat_t &dat, bool auto_scale_flag = true)

Initialize the data for the interpolation.

The object vecs should be a matrix with a first index of size n_in+n_out and a second index of size n_points. It may have be any type which allows the use of operator(,) and std::swap.

inline void get_data(size_t &n_in, size_t &n_out, size_t &n_points, mat_t &dat)

Get the data used for interpolation.

inline void auto_scale()

Automatically determine the length scales from the data.

inline void set_data(size_t n_in, size_t n_points, mat_t &dat, bool auto_scale = true)

Initialize the data for the interpolation for only one output function.

The object vecs should be a vector (of size n_in+1) of vectors (all of size n_points). It may be any type which allows the use of std::swap for each vector in the list.

Evaluate interpolation

template<class vec2_t>
inline double operator()(const vec2_t &x) const

Perform the interpolation over the first function.

template<class vec2_t>
inline double eval(const vec2_t &x) const

Perform the interpolation over the first function.

template<class vec2_t>
inline void eval_err(const vec2_t &x, double &val, double &err)

Perform the interpolation over the first function with uncertainty.

template<class vec2_t, class vec3_t>
inline void eval(const vec2_t &x, vec3_t &y) const

Perform the interpolation over all the functions, at point x, storing the result in y.

template<class vec2_t, class vec3_t, class vec4_t>
inline void eval_err_index(const vec2_t &x, vec3_t &val, vec4_t &err, std::vector<size_t> &index)

Perform the interpolation over all the functions giving uncertainties and the sorted index vector.

The vector index is automatically resized to a size equal to n_points+1+n_extra.

template<class vec2_t, class vec3_t, class vec4_t>
inline void eval_err(const vec2_t &x, vec3_t &val, vec4_t &err)

Perform the interpolation over all the functions with uncertainties.

Public Types

typedef boost::numeric::ublas::vector<double> ubvector
typedef boost::numeric::ublas::matrix<double> ubmatrix
typedef boost::numeric::ublas::vector<size_t> ubvector_size_t

Public Functions

inline interpm_idw()

Public Members

std::vector<double> extrap

Extrapolation factor (computed in eval_err() )

double dist_expo

Exponent in computing distance (default 2.0)

int verbose

Verbosity parameter (default 0)

size_t n_extra

The number of extra nearest neighbors to include to avoid degeneracies (default 0)

double min_dist

The minimum distance to consider points as non-degenerate (default \( 10^{-6} \) )