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
| #include <libalglib/stdafx.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <libalglib/interpolation.h>
using namespace alglib;
int main(int argc, char **argv)
{
//
// We use bicubic spline to reproduce f(x,y)=1/(1+x^2+2*y^2) sampled
// at irregular points (x,y) from [-1,+1]*[-1,+1]
//
// We have 5 such points, located approximately at corners of the area
// and its center - but not exactly at the grid. Thus, we have to FIT
// the spline, i.e. to solve least squares problem
//
real_2d_array xy = "[[-0.987,-0.902,0.359],[0.948,-0.992,0.347],[-1.000,1.000,0.333],[1.000,0.973,0.339],[0.017,0.180,0.968]]";
//
// First step is to create spline2dbuilder object and set its properties:
// * d=1 means that we create vector-valued spline with 1 component
// * we specify dataset xy
// * we rely on automatic selection of interpolation area
// * we tell builder that we want to use 5x5 grid for an underlying spline
// * we choose least squares solver named BlockLLS and configure it by
// telling that we want to apply zero nonlinearity penalty.
//
// NOTE: ALGLIB has two solvers which fit bicubic splines to irregular data,
// one of them is BlockLLS and another one is FastDDM. Former is
// intended for moderately sized grids (up to 512x512 nodes, although
// it may take up to few minutes); it is the most easy to use and
// control spline fitting function in the library. Latter, FastDDM,
// is intended for efficient solution of large-scale problems
// (up to 100.000.000 nodes). Both solvers can be parallelized, but
// FastDDM is much more efficient. See comments for more information.
//
spline2dbuilder builder;
ae_int_t d = 1;
spline2dbuildercreate(d, builder);
spline2dbuildersetpoints(builder, xy, 5);
spline2dbuildersetgrid(builder, 5, 5);
spline2dbuildersetalgoblocklls(builder, 0.000);
//
// Now we are ready to fit and evaluate our results
//
spline2dinterpolant s;
spline2dfitreport rep;
spline2dfit(builder, s, rep);
// evaluate results - function value at the grid is reproduced exactly
double v;
v = spline2dcalc(s, -1, 1);
printf("%.2f\n", double(v)); // EXPECTED: 0.333000
// check maximum error - it must be nearly zero
printf("%.2f\n", double(rep.maxerror)); // EXPECTED: 0.000
return 0;
} |
Partager