Make 2D interpolation on a grid, from values stored in a file. (x,y) values are in columns 1 and 2, z (interpolated) values in the 3rd column (works for different dimensions, intuitively...)
function f = function::interpolate_linear("filename",function(_1,_2),_3);
Make a linear interpolated function from vectors:
vectorx(10), y(10); // fill x and y here function lin = function::interpolate_linear(x,y);
Make spline interpolation from a file (2nd column=x, 5+6th colum=y)
function sp = function::spline("filename",_2, _5+_6);
Delaunay interpolation:
delaunay_interpolator d;
d.add_point(x,y,z); // many times
cerr<<"Value at (1.2, 3.4) = "<<d(1.2,3.4)<<endl;
// create a blop-function from this interpolator:
function f(d);
cerr<<"Value at (1.2, 3.4) = "<<f(1.2,3.4)<<endl;
// read from file:
function fff = function::interpolate_delaunay("filename",function(_1,_2),_3);
You can use the low-level interpolation classes directly, or use interpolated blop-functions which use the same base classes.
function::interpolate_XXX(const var &filename, const function &x=_1, const function &y=_2); function::interpolate_XXX(const array &x, const array &y); function::interpolate_XXX(const vectorAn example for their usage:&x, const vector &y); function::interpolate_XXX(const double x[], const double y[], int n);
function f = function::interpolate_linear("filename.dat",function(_1,_2),_3);
This creates a 2D linear interpolator, (x,y) values (independent
variables) are the first two columns of the file, the interpolated
value is the 3rd column. For spline and sppchip interpolation only 1D
is possible (i.e. the independent variable must be a single value,
unlike in this example).
Delaunay interpolation can be used to interpolate z-values on a set of irregular (x,y) points. This is therefore a 2D interpolation. The (x,y) plane is Delaunay-triangulated, and a triangulated surface is created having nodes at the z-(x,y) points.
function::interpolate_delaunay(const var &filename); // (x,y)=(1st,2nd)column, z=3rd column
function::interpolate_delaunay(const var &filename, const function &xy, const function &z);
// exapmle for the second: function f = function::interpolate_delaunay(filename,function(_2,_3),_1);
If you don't want to have the overhead from the usage of
blop-functions, or if you want to programmatically fill up your
interpolator with the discrete values, or you want to have access to
these discrete values later, you can also use the interpolator classes
directly.
The base class interpolator provides some common interface functions to be used by blop classes (i.e. no interest for the user), and a name() member function returning a name of the interpolation method (for example LINEAR, SPLINE, etc).
This is the base class for 1D interpolation. This class does nothing more than storing the (x,y) point pairs used for the interpolation. Derived classes will implement the different interpolation methods.
Member functions:
This derived class, polynomial_interpolator_1d interpolates within each interval [x[i];x[i+1]] using a polynomial of some order, the coefficients of which are stored in this function. Methods include linear, spline, shape-preserving piecewise cubic hermite, etc. interpolations. To make an interpolated blop-function, use these functions (where XXX can be linear, spline or sppchip (shape-preserving piecewise cubic hermite interpolating polynomial, see http://www.mathworks.com/moler/interp.pdf for details) More details about the interpolator classes are given below.
static function function::XXX(const var &filename, const function &f1=unset,... const function &f4=unset);
// read discrete values from the given file, possibly transform
// the values, for example first column is x, 2nd+3rd column is y
// function linint = function::linear("filename",_1,_2+_3);
static function function::XXX(const array &x, const array &y);
static function function::XXX(const vector &x, const vector &y);
static function function::XXX(double x[], double y[], int n);
The coefficients of
the interpolation are calculated by calling one of the functions
linear(), spline(), etc. Note that you need to call
one of these functions every time you change the discrete points, or
add new ones.
Member functions (besides those inherited from interpolator_1d, see above)
vector<double> x(10), y(10); x[0] = 1; y[0] = 1.2; .... etc, fill the vectors x,y polynomial_interpolator_1d interp(x,y); interp.spline(); cerr<<"Value at 2.3 = "<<interp(2.3)<<endl;
Source files: interpolate.h interpolate.cc