/************************************************************************* * 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 * AP.Math.RandomReal() - 1); for (j = 0; j <= nx - 1; j++) { t = 2 * AP.Math.RandomReal() - 1; v = v + AP.Math.Sqr(t); xy[i, j] = t; } xy[i, nx] = v; } // // build interpolants // idwint.idwbuildmodifiedshepard(ref xy, ntrn, nx, d, nq, nw, ref z1); idwint.idwbuildnoisy(ref xy, ntrn, nx, d, nq, nw, ref 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 * AP.Math.RandomReal() - 1; x[j] = t; ve = ve + AP.Math.Sqr(t); } v1 = idwint.idwcalc(ref z1, ref x); v2 = idwint.idwcalc(ref z2, ref x); rms1 = rms1 + AP.Math.Sqr(v1 - ve); rms2 = rms2 + AP.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 * AP.Math.MachineEpsilon; nq = 2 * (nx * nx + nx + 1); nw = 10; System.Diagnostics.Debug.Assert(nq <= n, "TestDegree: internal error"); // // prepare model // c0 = 2 * AP.Math.RandomReal() - 1; c1 = new double[nx]; for (i = 0; i <= nx - 1; i++) { c1[i] = 2 * AP.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 * AP.Math.RandomReal() - 1; c2[j, i] = c2[i, j]; } do { c2[i, i] = 2 * AP.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 * AP.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(ref xy, n, nx, d, nq, nw, ref z1); x = new double[nx]; for (i = 0; i <= nx - 1; i++) { x[i] = 4 * AP.Math.RandomReal() - 2; } v1 = idwint.idwcalc(ref z1, ref 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); } }
/************************************************************************* * Testing IDW: * generate model using R-based model * test basic properties *************************************************************************/ private static void testrxy(ref double[,] xy, int n, int nx, double r, ref bool idwerrors) { double threshold = 0; double lipschitzstep = 0; int i = 0; int j = 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 * AP.Math.MachineEpsilon; lipschitzstep = 0.001; x = new double[nx]; // // build // idwint.idwbuildmodifiedshepardr(ref xy, n, nx, r, ref 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(ref z1, ref x)) != (double)(xy[i, nx]); } // // test Lipschitz continuity // i1 = AP.Math.RandomInteger(n); do { i2 = AP.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(ref z1, ref 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(ref z1, ref 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(ref z1, ref 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(ref z1, ref x); l2 = Math.Max(l2, Math.Abs(v2 - v1) / (lipschitzstep / 3)); t = t + lipschitzstep / 3; } idwerrors = idwerrors | (double)(l2) > (double)(2.0 * l1); }
/************************************************************************* 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*AP.Math.RandomReal()-1); for(j=0; j<=nx-1; j++) { t = 2*AP.Math.RandomReal()-1; v = v+AP.Math.Sqr(t); xy[i,j] = t; } xy[i,nx] = v; } // // build interpolants // idwint.idwbuildmodifiedshepard(ref xy, ntrn, nx, d, nq, nw, ref z1); idwint.idwbuildnoisy(ref xy, ntrn, nx, d, nq, nw, ref 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*AP.Math.RandomReal()-1; x[j] = t; ve = ve+AP.Math.Sqr(t); } v1 = idwint.idwcalc(ref z1, ref x); v2 = idwint.idwcalc(ref z2, ref x); rms1 = rms1+AP.Math.Sqr(v1-ve); rms2 = rms2+AP.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*AP.Math.MachineEpsilon; nq = 2*(nx*nx+nx+1); nw = 10; System.Diagnostics.Debug.Assert(nq<=n, "TestDegree: internal error"); // // prepare model // c0 = 2*AP.Math.RandomReal()-1; c1 = new double[nx]; for(i=0; i<=nx-1; i++) { c1[i] = 2*AP.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*AP.Math.RandomReal()-1; c2[j,i] = c2[i,j]; } do { c2[i,i] = 2*AP.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*AP.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(ref xy, n, nx, d, nq, nw, ref z1); x = new double[nx]; for(i=0; i<=nx-1; i++) { x[i] = 4*AP.Math.RandomReal()-2; } v1 = idwint.idwcalc(ref z1, ref 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); } }
/************************************************************************* Testing IDW: * generate model using R-based model * test basic properties *************************************************************************/ private static void testrxy(ref double[,] xy, int n, int nx, double r, ref bool idwerrors) { double threshold = 0; double lipschitzstep = 0; int i = 0; int j = 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*AP.Math.MachineEpsilon; lipschitzstep = 0.001; x = new double[nx]; // // build // idwint.idwbuildmodifiedshepardr(ref xy, n, nx, r, ref 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(ref z1, ref x))!=(double)(xy[i,nx]); } // // test Lipschitz continuity // i1 = AP.Math.RandomInteger(n); do { i2 = AP.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(ref z1, ref 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(ref z1, ref 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(ref z1, ref 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(ref z1, ref x); l2 = Math.Max(l2, Math.Abs(v2-v1)/(lipschitzstep/3)); t = t+lipschitzstep/3; } idwerrors = idwerrors | (double)(l2)>(double)(2.0*l1); }