/************************************************************************* Noisy test: * F = x^2 + y^2 + z^2 + noise on [-1,+1]^3 * space is either R1=[-1,+1] (other dimensions are fixed at 0), R1^2 or R1^3. * D = 1, 2 * 4096 points is used for function generation, 4096 points - for testing * RMS error of "noisy" model on test set must be lower than RMS error of interpolation model. *************************************************************************/ private static void testnoisy(ref bool idwerrors) { double noiselevel = 0; int nq = 0; int nw = 0; int d = 0; int nx = 0; int ntrn = 0; int ntst = 0; int i = 0; int j = 0; double v = 0; double t = 0; double v1 = 0; double v2 = 0; double ve = 0; double[,] xy = new double[0,0]; double[] x = new double[0]; idwint.idwinterpolant z1 = new idwint.idwinterpolant(); idwint.idwinterpolant z2 = new idwint.idwinterpolant(); double rms1 = 0; double rms2 = 0; nq = 20; nw = 40; noiselevel = 0.2; ntrn = 256; ntst = 1024; for(d=1; d<=2; d++) { for(nx=1; nx<=2; nx++) { // // prepare dataset // xy = new double[ntrn, nx+1]; for(i=0; i<=ntrn-1; i++) { v = noiselevel*(2*math.randomreal()-1); for(j=0; j<=nx-1; j++) { t = 2*math.randomreal()-1; v = v+math.sqr(t); xy[i,j] = t; } xy[i,nx] = v; } // // build interpolants // idwint.idwbuildmodifiedshepard(xy, ntrn, nx, d, nq, nw, z1); idwint.idwbuildnoisy(xy, ntrn, nx, d, nq, nw, z2); // // calculate RMS errors // x = new double[nx]; rms1 = 0; rms2 = 0; for(i=0; i<=ntst-1; i++) { ve = 0; for(j=0; j<=nx-1; j++) { t = 2*math.randomreal()-1; x[j] = t; ve = ve+math.sqr(t); } v1 = idwint.idwcalc(z1, x); v2 = idwint.idwcalc(z2, x); rms1 = rms1+math.sqr(v1-ve); rms2 = rms2+math.sqr(v2-ve); } idwerrors = idwerrors | (double)(rms2)>(double)(rms1); } } }
/************************************************************************* Testing degree properties F is either: * constant (DTask=0) * linear (DTask=1) * quadratic (DTask=2) Nodal functions are either * constant (D=0) * linear (D=1) * quadratic (D=2) When DTask<=D, we can interpolate without errors. When DTask>D, we MUST have errors. *************************************************************************/ private static void testdegree(int n, int nx, int d, int dtask, ref bool idwerrors) { double threshold = 0; int nq = 0; int nw = 0; int i = 0; int j = 0; double v = 0; double c0 = 0; double[] c1 = new double[0]; double[,] c2 = new double[0,0]; double[] x = new double[0]; double[,] xy = new double[0,0]; idwint.idwinterpolant z1 = new idwint.idwinterpolant(); double v1 = 0; double v2 = 0; int i_ = 0; threshold = 1.0E6*math.machineepsilon; nq = 2*(nx*nx+nx+1); nw = 10; ap.assert(nq<=n, "TestDegree: internal error"); // // prepare model // c0 = 2*math.randomreal()-1; c1 = new double[nx]; for(i=0; i<=nx-1; i++) { c1[i] = 2*math.randomreal()-1; } c2 = new double[nx, nx]; for(i=0; i<=nx-1; i++) { for(j=i+1; j<=nx-1; j++) { c2[i,j] = 2*math.randomreal()-1; c2[j,i] = c2[i,j]; } do { c2[i,i] = 2*math.randomreal()-1; } while( (double)(Math.Abs(c2[i,i]))<=(double)(0.3) ); } // // prepare points // xy = new double[n, nx+1]; for(i=0; i<=n-1; i++) { for(j=0; j<=nx-1; j++) { xy[i,j] = 4*math.randomreal()-2; } xy[i,nx] = c0; if( dtask>=1 ) { v = 0.0; for(i_=0; i_<=nx-1;i_++) { v += c1[i_]*xy[i,i_]; } xy[i,nx] = xy[i,nx]+v; } if( dtask==2 ) { for(j=0; j<=nx-1; j++) { v = 0.0; for(i_=0; i_<=nx-1;i_++) { v += c2[j,i_]*xy[i,i_]; } xy[i,nx] = xy[i,nx]+xy[i,j]*v; } } } // // build interpolant, calculate value at random point // idwint.idwbuildmodifiedshepard(xy, n, nx, d, nq, nw, z1); x = new double[nx]; for(i=0; i<=nx-1; i++) { x[i] = 4*math.randomreal()-2; } v1 = idwint.idwcalc(z1, x); // // calculate model value at the same point // v2 = c0; if( dtask>=1 ) { v = 0.0; for(i_=0; i_<=nx-1;i_++) { v += c1[i_]*x[i_]; } v2 = v2+v; } if( dtask==2 ) { for(j=0; j<=nx-1; j++) { v = 0.0; for(i_=0; i_<=nx-1;i_++) { v += c2[j,i_]*x[i_]; } v2 = v2+x[j]*v; } } // // Compare // if( dtask<=d ) { idwerrors = idwerrors | (double)(Math.Abs(v2-v1))>(double)(threshold); } else { idwerrors = idwerrors | (double)(Math.Abs(v2-v1))<(double)(threshold); } }
public idwinterpolant(idwint.idwinterpolant obj) { _innerobj = obj; }
/************************************************************************* Testing IDW: * generate model using R-based model * test basic properties *************************************************************************/ private static void testrxy(double[,] xy, int n, int nx, double r, ref bool idwerrors) { double threshold = 0; double lipschitzstep = 0; int i = 0; int i1 = 0; int i2 = 0; double v = 0; double v1 = 0; double v2 = 0; double t = 0; double l1 = 0; double l2 = 0; idwint.idwinterpolant z1 = new idwint.idwinterpolant(); double[] x = new double[0]; int i_ = 0; threshold = 1000*math.machineepsilon; lipschitzstep = 0.001; x = new double[nx]; // // build // idwint.idwbuildmodifiedshepardr(xy, n, nx, r, z1); // // first, test interpolation properties at nodes // for(i=0; i<=n-1; i++) { for(i_=0; i_<=nx-1;i_++) { x[i_] = xy[i,i_]; } idwerrors = idwerrors | (double)(idwint.idwcalc(z1, x))!=(double)(xy[i,nx]); } // // test Lipschitz continuity // i1 = math.randominteger(n); do { i2 = math.randominteger(n); } while( i2==i1 ); l1 = 0; t = 0; while( (double)(t)<(double)(1) ) { v = 1-t; for(i_=0; i_<=nx-1;i_++) { x[i_] = v*xy[i1,i_]; } v = t; for(i_=0; i_<=nx-1;i_++) { x[i_] = x[i_] + v*xy[i2,i_]; } v1 = idwint.idwcalc(z1, x); v = 1-(t+lipschitzstep); for(i_=0; i_<=nx-1;i_++) { x[i_] = v*xy[i1,i_]; } v = t+lipschitzstep; for(i_=0; i_<=nx-1;i_++) { x[i_] = x[i_] + v*xy[i2,i_]; } v2 = idwint.idwcalc(z1, x); l1 = Math.Max(l1, Math.Abs(v2-v1)/lipschitzstep); t = t+lipschitzstep; } l2 = 0; t = 0; while( (double)(t)<(double)(1) ) { v = 1-t; for(i_=0; i_<=nx-1;i_++) { x[i_] = v*xy[i1,i_]; } v = t; for(i_=0; i_<=nx-1;i_++) { x[i_] = x[i_] + v*xy[i2,i_]; } v1 = idwint.idwcalc(z1, x); v = 1-(t+lipschitzstep/3); for(i_=0; i_<=nx-1;i_++) { x[i_] = v*xy[i1,i_]; } v = t+lipschitzstep/3; for(i_=0; i_<=nx-1;i_++) { x[i_] = x[i_] + v*xy[i2,i_]; } v2 = idwint.idwcalc(z1, x); l2 = Math.Max(l2, Math.Abs(v2-v1)/(lipschitzstep/3)); t = t+lipschitzstep/3; } idwerrors = idwerrors | (double)(l2)>(double)(2.0*l1); }
// // Public declarations // public idwinterpolant() { _innerobj = new idwint.idwinterpolant(); }