public static int Main(string[] args) { int m = 0; int n = 0; int k = 0; double[] y = new double[0]; double[,] x = new double[0,0]; double[] c = new double[0]; lsfit.lsfitreport rep = new lsfit.lsfitreport(); lsfit.lsfitstate state = new lsfit.lsfitstate(); int info = 0; double epsf = 0; double epsx = 0; int maxits = 0; int i = 0; int j = 0; double a = 0; double b = 0; System.Console.Write("Fitting 0.5(1+cos(x)) on [-pi,+pi] with exp(-alpha*x^2)"); System.Console.WriteLine(); // // Fitting 0.5(1+cos(x)) on [-pi,+pi] with Gaussian exp(-alpha*x^2): // * without Hessian (gradient only) // * using alpha=1 as initial value // * using 1000 uniformly distributed points to fit to // // Notes: // * N - number of points // * M - dimension of space where points reside // * K - number of parameters being fitted // n = 1000; m = 1; k = 1; a = -Math.PI; b = +Math.PI; // // Prepare task matrix // y = new double[n]; x = new double[n, m]; c = new double[k]; for(i=0; i<=n-1; i++) { x[i,0] = a+(b-a)*i/(n-1); y[i] = 0.5*(1+Math.Cos(x[i,0])); } c[0] = 1.0; epsf = 0.0; epsx = 0.0001; maxits = 0; // // Solve // lsfit.lsfitnonlinearfg(ref x, ref y, ref c, n, m, k, true, ref state); lsfit.lsfitnonlinearsetcond(ref state, epsf, epsx, maxits); while( lsfit.lsfitnonlineariteration(ref state) ) { if( state.needf ) { // // F(x) = Exp(-alpha*x^2) // state.f = Math.Exp(-(state.c[0]*AP.Math.Sqr(state.x[0]))); } if( state.needfg ) { // // F(x) = Exp(-alpha*x^2) // dF/dAlpha = (-x^2)*Exp(-alpha*x^2) // state.f = Math.Exp(-(state.c[0]*AP.Math.Sqr(state.x[0]))); state.g[0] = -(AP.Math.Sqr(state.x[0])*state.f); } } lsfit.lsfitnonlinearresults(ref state, ref info, ref c, ref rep); System.Console.Write("alpha: "); System.Console.Write("{0,0:F3}",c[0]); System.Console.WriteLine(); System.Console.Write("rms.err: "); System.Console.Write("{0,0:F3}",rep.rmserror); System.Console.WriteLine(); System.Console.Write("max.err: "); System.Console.Write("{0,0:F3}",rep.maxerror); System.Console.WriteLine(); System.Console.Write("Termination type: "); System.Console.Write("{0,0:d}",info); System.Console.WriteLine(); System.Console.WriteLine(); System.Console.WriteLine(); return 0; }
private static void testgeneralfitting(ref bool llserrors, ref bool nlserrors) { double threshold = 0; double nlthreshold = 0; int maxn = 0; int maxm = 0; int passcount = 0; int n = 0; int m = 0; int i = 0; int j = 0; int k = 0; int pass = 0; double xscale = 0; double diffstep = 0; double[] x = new double[0]; double[] y = new double[0]; double[] w = new double[0]; double[] w2 = new double[0]; double[] c = new double[0]; double[] c2 = new double[0]; double[,] a = new double[0,0]; double[,] a2 = new double[0,0]; double[,] cm = new double[0,0]; double v = 0; double v1 = 0; double v2 = 0; lsfit.lsfitreport rep = new lsfit.lsfitreport(); lsfit.lsfitreport rep2 = new lsfit.lsfitreport(); int info = 0; int info2 = 0; double refrms = 0; double refavg = 0; double refavgrel = 0; double refmax = 0; lsfit.lsfitstate state = new lsfit.lsfitstate(); llserrors = false; nlserrors = false; threshold = 10000*math.machineepsilon; nlthreshold = 0.00001; diffstep = 0.0001; maxn = 6; maxm = 6; passcount = 4; // // Testing unconstrained least squares (linear/nonlinear) // for(n=1; n<=maxn; n++) { for(m=1; m<=maxm; m++) { for(pass=1; pass<=passcount; pass++) { // // Solve non-degenerate linear least squares task // Use Chebyshev basis. Its condition number is very good. // a = new double[n, m]; x = new double[n]; y = new double[n]; w = new double[n]; xscale = 0.9+0.1*math.randomreal(); for(i=0; i<=n-1; i++) { if( n==1 ) { x[i] = 2*math.randomreal()-1; } else { x[i] = xscale*((double)(2*i)/(double)(n-1)-1); } y[i] = 3*x[i]+Math.Exp(x[i]); w[i] = 1+math.randomreal(); a[i,0] = 1; if( m>1 ) { a[i,1] = x[i]; } for(j=2; j<=m-1; j++) { a[i,j] = 2*x[i]*a[i,j-1]-a[i,j-2]; } } // // 1. test weighted fitting (optimality) // 2. Solve degenerate least squares task built on the basis // of previous task // lsfit.lsfitlinearw(y, w, a, n, m, ref info, ref c, rep); if( info<=0 ) { llserrors = true; } else { llserrors = llserrors | !isglssolution(n, m, 0, y, w, a, cm, c); } a2 = new double[n, 2*m]; for(i=0; i<=n-1; i++) { for(j=0; j<=m-1; j++) { a2[i,2*j+0] = a[i,j]; a2[i,2*j+1] = a[i,j]; } } lsfit.lsfitlinearw(y, w, a2, n, 2*m, ref info, ref c2, rep); if( info<=0 ) { llserrors = true; } else { // // test answer correctness using design matrix properties // and previous task solution // for(j=0; j<=m-1; j++) { llserrors = llserrors | (double)(Math.Abs(c2[2*j+0]+c2[2*j+1]-c[j]))>(double)(threshold); } } // // test non-weighted fitting // w2 = new double[n]; for(i=0; i<=n-1; i++) { w2[i] = 1; } lsfit.lsfitlinearw(y, w2, a, n, m, ref info, ref c, rep); lsfit.lsfitlinear(y, a, n, m, ref info2, ref c2, rep2); if( info<=0 | info2<=0 ) { llserrors = true; } else { // // test answer correctness // for(j=0; j<=m-1; j++) { llserrors = llserrors | (double)(Math.Abs(c[j]-c2[j]))>(double)(threshold); } llserrors = llserrors | (double)(Math.Abs(rep.taskrcond-rep2.taskrcond))>(double)(threshold); } // // test nonlinear fitting on the linear task // (only non-degenerate tasks are tested) // and compare with answer from linear fitting subroutine // if( n>=m ) { c2 = new double[m]; // // test function/gradient/Hessian-based weighted fitting // lsfit.lsfitlinearw(y, w, a, n, m, ref info, ref c, rep); for(i=0; i<=m-1; i++) { c2[i] = 2*math.randomreal()-1; } lsfit.lsfitcreatewf(a, y, w, c2, n, m, m, diffstep, state); lsfit.lsfitsetcond(state, 0.0, nlthreshold, 0); fitlinearnonlinear(m, 0, a, state, ref nlserrors); lsfit.lsfitresults(state, ref info, ref c2, rep2); if( info<=0 ) { nlserrors = true; } else { for(i=0; i<=m-1; i++) { nlserrors = nlserrors | (double)(Math.Abs(c[i]-c2[i]))>(double)(100*nlthreshold); } } for(i=0; i<=m-1; i++) { c2[i] = 2*math.randomreal()-1; } lsfit.lsfitcreatewfg(a, y, w, c2, n, m, m, (double)(math.randomreal())>(double)(0.5), state); lsfit.lsfitsetcond(state, 0.0, nlthreshold, 0); fitlinearnonlinear(m, 1, a, state, ref nlserrors); lsfit.lsfitresults(state, ref info, ref c2, rep2); if( info<=0 ) { nlserrors = true; } else { for(i=0; i<=m-1; i++) { nlserrors = nlserrors | (double)(Math.Abs(c[i]-c2[i]))>(double)(100*nlthreshold); } } for(i=0; i<=m-1; i++) { c2[i] = 2*math.randomreal()-1; } lsfit.lsfitcreatewfgh(a, y, w, c2, n, m, m, state); lsfit.lsfitsetcond(state, 0.0, nlthreshold, 0); fitlinearnonlinear(m, 2, a, state, ref nlserrors); lsfit.lsfitresults(state, ref info, ref c2, rep2); if( info<=0 ) { nlserrors = true; } else { for(i=0; i<=m-1; i++) { nlserrors = nlserrors | (double)(Math.Abs(c[i]-c2[i]))>(double)(100*nlthreshold); } } // // test gradient-only or Hessian-based fitting without weights // lsfit.lsfitlinear(y, a, n, m, ref info, ref c, rep); for(i=0; i<=m-1; i++) { c2[i] = 2*math.randomreal()-1; } lsfit.lsfitcreatef(a, y, c2, n, m, m, diffstep, state); lsfit.lsfitsetcond(state, 0.0, nlthreshold, 0); fitlinearnonlinear(m, 0, a, state, ref nlserrors); lsfit.lsfitresults(state, ref info, ref c2, rep2); if( info<=0 ) { nlserrors = true; } else { for(i=0; i<=m-1; i++) { nlserrors = nlserrors | (double)(Math.Abs(c[i]-c2[i]))>(double)(100*nlthreshold); } } for(i=0; i<=m-1; i++) { c2[i] = 2*math.randomreal()-1; } lsfit.lsfitcreatefg(a, y, c2, n, m, m, (double)(math.randomreal())>(double)(0.5), state); lsfit.lsfitsetcond(state, 0.0, nlthreshold, 0); fitlinearnonlinear(m, 1, a, state, ref nlserrors); lsfit.lsfitresults(state, ref info, ref c2, rep2); if( info<=0 ) { nlserrors = true; } else { for(i=0; i<=m-1; i++) { nlserrors = nlserrors | (double)(Math.Abs(c[i]-c2[i]))>(double)(100*nlthreshold); } } for(i=0; i<=m-1; i++) { c2[i] = 2*math.randomreal()-1; } lsfit.lsfitcreatefgh(a, y, c2, n, m, m, state); lsfit.lsfitsetcond(state, 0.0, nlthreshold, 0); fitlinearnonlinear(m, 2, a, state, ref nlserrors); lsfit.lsfitresults(state, ref info, ref c2, rep2); if( info<=0 ) { nlserrors = true; } else { for(i=0; i<=m-1; i++) { nlserrors = nlserrors | (double)(Math.Abs(c[i]-c2[i]))>(double)(100*nlthreshold); } } } } } // // test correctness of the RCond field // a = new double[n-1+1, n-1+1]; x = new double[n-1+1]; y = new double[n-1+1]; w = new double[n-1+1]; v1 = math.maxrealnumber; v2 = math.minrealnumber; for(i=0; i<=n-1; i++) { x[i] = 0.1+0.9*math.randomreal(); y[i] = 0.1+0.9*math.randomreal(); w[i] = 1; for(j=0; j<=n-1; j++) { if( i==j ) { a[i,i] = 0.1+0.9*math.randomreal(); v1 = Math.Min(v1, a[i,i]); v2 = Math.Max(v2, a[i,i]); } else { a[i,j] = 0; } } } lsfit.lsfitlinearw(y, w, a, n, n, ref info, ref c, rep); if( info<=0 ) { llserrors = true; } else { llserrors = llserrors | (double)(Math.Abs(rep.taskrcond-v1/v2))>(double)(threshold); } } // // Test constrained least squares // for(pass=1; pass<=passcount; pass++) { for(n=1; n<=maxn; n++) { for(m=1; m<=maxm; m++) { // // test for K<>0 // for(k=1; k<=m-1; k++) { // // Prepare Chebyshev basis. Its condition number is very good. // Prepare constraints (random numbers) // a = new double[n, m]; x = new double[n]; y = new double[n]; w = new double[n]; xscale = 0.9+0.1*math.randomreal(); for(i=0; i<=n-1; i++) { if( n==1 ) { x[i] = 2*math.randomreal()-1; } else { x[i] = xscale*((double)(2*i)/(double)(n-1)-1); } y[i] = 3*x[i]+Math.Exp(x[i]); w[i] = 1+math.randomreal(); a[i,0] = 1; if( m>1 ) { a[i,1] = x[i]; } for(j=2; j<=m-1; j++) { a[i,j] = 2*x[i]*a[i,j-1]-a[i,j-2]; } } cm = new double[k, m+1]; for(i=0; i<=k-1; i++) { for(j=0; j<=m; j++) { cm[i,j] = 2*math.randomreal()-1; } } // // Solve constrained task // lsfit.lsfitlinearwc(y, w, a, cm, n, m, k, ref info, ref c, rep); if( info<=0 ) { llserrors = true; } else { llserrors = llserrors | !isglssolution(n, m, k, y, w, a, cm, c); } // // test non-weighted fitting // w2 = new double[n]; for(i=0; i<=n-1; i++) { w2[i] = 1; } lsfit.lsfitlinearwc(y, w2, a, cm, n, m, k, ref info, ref c, rep); lsfit.lsfitlinearc(y, a, cm, n, m, k, ref info2, ref c2, rep2); if( info<=0 | info2<=0 ) { llserrors = true; } else { // // test answer correctness // for(j=0; j<=m-1; j++) { llserrors = llserrors | (double)(Math.Abs(c[j]-c2[j]))>(double)(threshold); } llserrors = llserrors | (double)(Math.Abs(rep.taskrcond-rep2.taskrcond))>(double)(threshold); } } } } } // // nonlinear task for nonlinear fitting: // // f(X,C) = 1/(1+C*X^2), // C(true) = 2. // n = 100; c = new double[1]; c[0] = 1+2*math.randomreal(); a = new double[n, 1]; y = new double[n]; for(i=0; i<=n-1; i++) { a[i,0] = 4*math.randomreal()-2; y[i] = 1/(1+2*math.sqr(a[i,0])); } lsfit.lsfitcreatefg(a, y, c, n, 1, 1, true, state); lsfit.lsfitsetcond(state, 0.0, nlthreshold, 0); while( lsfit.lsfititeration(state) ) { if( state.needf ) { state.f = 1/(1+state.c[0]*math.sqr(state.x[0])); } if( state.needfg ) { state.f = 1/(1+state.c[0]*math.sqr(state.x[0])); state.g[0] = -(math.sqr(state.x[0])/math.sqr(1+state.c[0]*math.sqr(state.x[0]))); } } lsfit.lsfitresults(state, ref info, ref c, rep); if( info<=0 ) { nlserrors = true; } else { nlserrors = nlserrors | (double)(Math.Abs(c[0]-2))>(double)(100*nlthreshold); } // // solve simple task (fitting by constant function) and check // correctness of the errors calculated by subroutines // for(pass=1; pass<=passcount; pass++) { // // test on task with non-zero Yi // n = 4; v1 = math.randomreal(); v2 = math.randomreal(); v = 1+math.randomreal(); c = new double[1]; c[0] = 1+2*math.randomreal(); a = new double[4, 1]; y = new double[4]; a[0,0] = 1; y[0] = v-v2; a[1,0] = 1; y[1] = v-v1; a[2,0] = 1; y[2] = v+v1; a[3,0] = 1; y[3] = v+v2; refrms = Math.Sqrt((math.sqr(v1)+math.sqr(v2))/2); refavg = (Math.Abs(v1)+Math.Abs(v2))/2; refavgrel = 0.25*(Math.Abs(v2)/Math.Abs(v-v2)+Math.Abs(v1)/Math.Abs(v-v1)+Math.Abs(v1)/Math.Abs(v+v1)+Math.Abs(v2)/Math.Abs(v+v2)); refmax = Math.Max(v1, v2); // // Test LLS // lsfit.lsfitlinear(y, a, 4, 1, ref info, ref c, rep); if( info<=0 ) { llserrors = true; } else { llserrors = llserrors | (double)(Math.Abs(c[0]-v))>(double)(threshold); llserrors = llserrors | (double)(Math.Abs(rep.rmserror-refrms))>(double)(threshold); llserrors = llserrors | (double)(Math.Abs(rep.avgerror-refavg))>(double)(threshold); llserrors = llserrors | (double)(Math.Abs(rep.avgrelerror-refavgrel))>(double)(threshold); llserrors = llserrors | (double)(Math.Abs(rep.maxerror-refmax))>(double)(threshold); } // // Test NLS // lsfit.lsfitcreatefg(a, y, c, 4, 1, 1, true, state); lsfit.lsfitsetcond(state, 0.0, nlthreshold, 0); while( lsfit.lsfititeration(state) ) { if( state.needf ) { state.f = state.c[0]; } if( state.needfg ) { state.f = state.c[0]; state.g[0] = 1; } } lsfit.lsfitresults(state, ref info, ref c, rep); if( info<=0 ) { nlserrors = true; } else { nlserrors = nlserrors | (double)(Math.Abs(c[0]-v))>(double)(threshold); nlserrors = nlserrors | (double)(Math.Abs(rep.rmserror-refrms))>(double)(threshold); nlserrors = nlserrors | (double)(Math.Abs(rep.avgerror-refavg))>(double)(threshold); nlserrors = nlserrors | (double)(Math.Abs(rep.avgrelerror-refavgrel))>(double)(threshold); nlserrors = nlserrors | (double)(Math.Abs(rep.maxerror-refmax))>(double)(threshold); } } }
public lsfitstate() { _innerobj = new lsfit.lsfitstate(); }
public lsfitstate(lsfit.lsfitstate obj) { _innerobj = obj; }
public static int Main(string[] args) { int m = 0; int n = 0; int k = 0; double[] y = new double[0]; double[,] x = new double[0, 0]; double[] c = new double[0]; lsfit.lsfitreport rep = new lsfit.lsfitreport(); lsfit.lsfitstate state = new lsfit.lsfitstate(); int info = 0; double epsf = 0; double epsx = 0; int maxits = 0; int i = 0; int j = 0; double a = 0; double b = 0; System.Console.Write("Fitting 0.5(1+cos(x)) on [-pi,+pi] with exp(-alpha*x^2)"); System.Console.WriteLine(); // // Fitting 0.5(1+cos(x)) on [-pi,+pi] with Gaussian exp(-alpha*x^2): // * without Hessian (gradient only) // * using alpha=1 as initial value // * using 1000 uniformly distributed points to fit to // // Notes: // * N - number of points // * M - dimension of space where points reside // * K - number of parameters being fitted // n = 1000; m = 1; k = 1; a = -Math.PI; b = +Math.PI; // // Prepare task matrix // y = new double[n]; x = new double[n, m]; c = new double[k]; for (i = 0; i <= n - 1; i++) { x[i, 0] = a + (b - a) * i / (n - 1); y[i] = 0.5 * (1 + Math.Cos(x[i, 0])); } c[0] = 1.0; epsf = 0.0; epsx = 0.0001; maxits = 0; // // Solve // lsfit.lsfitnonlinearfg(ref x, ref y, ref c, n, m, k, true, ref state); lsfit.lsfitnonlinearsetcond(ref state, epsf, epsx, maxits); while (lsfit.lsfitnonlineariteration(ref state)) { if (state.needf) { // // F(x) = Exp(-alpha*x^2) // state.f = Math.Exp(-(state.c[0] * AP.Math.Sqr(state.x[0]))); } if (state.needfg) { // // F(x) = Exp(-alpha*x^2) // dF/dAlpha = (-x^2)*Exp(-alpha*x^2) // state.f = Math.Exp(-(state.c[0] * AP.Math.Sqr(state.x[0]))); state.g[0] = -(AP.Math.Sqr(state.x[0]) * state.f); } } lsfit.lsfitnonlinearresults(ref state, ref info, ref c, ref rep); System.Console.Write("alpha: "); System.Console.Write("{0,0:F3}", c[0]); System.Console.WriteLine(); System.Console.Write("rms.err: "); System.Console.Write("{0,0:F3}", rep.rmserror); System.Console.WriteLine(); System.Console.Write("max.err: "); System.Console.Write("{0,0:F3}", rep.maxerror); System.Console.WriteLine(); System.Console.Write("Termination type: "); System.Console.Write("{0,0:d}", info); System.Console.WriteLine(); System.Console.WriteLine(); System.Console.WriteLine(); return(0); }
public static int Main(string[] args) { int m = 0; int n = 0; int k = 0; double[] y = new double[0]; double[,] x = new double[0,0]; double[] c = new double[0]; lsfit.lsfitreport rep = new lsfit.lsfitreport(); lsfit.lsfitstate state = new lsfit.lsfitstate(); int info = 0; double epsf = 0; double epsx = 0; int maxits = 0; int i = 0; int j = 0; double a = 0; double b = 0; System.Console.Write("Fitting 1-x^2 on [-1,+1] with cos(alpha*pi*x)+beta"); System.Console.WriteLine(); // // Fitting 1-x^2 on [-1,+1] with cos(alpha*pi*x)+beta: // * using Hessian // * using alpha=1 and beta=0 as initial values // * using 1000 uniformly distributed points to fit to // // Notes: // * N - number of points // * M - dimension of space where points reside // * K - number of parameters being fitted // n = 1000; m = 1; k = 2; a = -1; b = +1; // // Prepare task matrix // y = new double[n]; x = new double[n, m]; c = new double[k]; for(i=0; i<=n-1; i++) { x[i,0] = a+(b-a)*i/(n-1); y[i] = 1-AP.Math.Sqr(x[i,0]); } c[0] = 1.0; c[1] = 0.0; epsf = 0.0; epsx = 0.0001; maxits = 0; // // Solve // lsfit.lsfitnonlinearfgh(ref x, ref y, ref c, n, m, k, ref state); lsfit.lsfitnonlinearsetcond(ref state, epsf, epsx, maxits); while( lsfit.lsfitnonlineariteration(ref state) ) { // // F(x) = Cos(alpha*pi*x)+beta // state.f = Math.Cos(state.c[0]*Math.PI*state.x[0])+state.c[1]; // // F(x) = Cos(alpha*pi*x)+beta // dF/dAlpha = -pi*x*Sin(alpha*pi*x) // dF/dBeta = 1.0 // if( state.needfg | state.needfgh ) { state.g[0] = -(Math.PI*state.x[0]*Math.Sin(state.c[0]*Math.PI*state.x[0])); state.g[1] = 1.0; } // // F(x) = Cos(alpha*pi*x)+beta // d2F/dAlpha2 = -(pi*x)^2*Cos(alpha*pi*x) // d2F/dAlphadBeta = 0 // d2F/dBeta2 = 0 // if( state.needfgh ) { state.h[0,0] = -(AP.Math.Sqr(Math.PI*state.x[0])*Math.Cos(state.c[0]*Math.PI*state.x[0])); state.h[0,1] = 0.0; state.h[1,0] = 0.0; state.h[1,1] = 0.0; } } lsfit.lsfitnonlinearresults(ref state, ref info, ref c, ref rep); System.Console.Write("alpha: "); System.Console.Write("{0,0:F3}",c[0]); System.Console.WriteLine(); System.Console.Write("beta: "); System.Console.Write("{0,0:F3}",c[1]); System.Console.WriteLine(); System.Console.Write("rms.err: "); System.Console.Write("{0,0:F3}",rep.rmserror); System.Console.WriteLine(); System.Console.Write("max.err: "); System.Console.Write("{0,0:F3}",rep.maxerror); System.Console.WriteLine(); System.Console.Write("Termination type: "); System.Console.Write("{0,0:d}",info); System.Console.WriteLine(); System.Console.WriteLine(); System.Console.WriteLine(); return 0; }
/************************************************************************* * Subroutine for nonlinear fitting of linear problem *************************************************************************/ private static void fitlinearnonlinear(int m, bool gradonly, ref double[,] xy, ref lsfit.lsfitstate state, ref bool nlserrors) { int i = 0; int j = 0; double v = 0; int i_ = 0; while (lsfit.lsfitnonlineariteration(ref state)) { // // assume that one and only one of flags is set // test that we didn't request hessian in hessian-free setting // if (gradonly & state.needfgh) { nlserrors = true; } i = 0; if (state.needf) { i = i + 1; } if (state.needfg) { i = i + 1; } if (state.needfgh) { i = i + 1; } if (i != 1) { nlserrors = true; } // // test that PointIndex is consistent with actual point passed // for (i = 0; i <= m - 1; i++) { nlserrors = nlserrors | (double)(xy[state.pointindex, i]) != (double)(state.x[i]); } // // calculate // if (state.needf) { v = 0.0; for (i_ = 0; i_ <= m - 1; i_++) { v += state.x[i_] * state.c[i_]; } state.f = v; continue; } if (state.needfg) { v = 0.0; for (i_ = 0; i_ <= m - 1; i_++) { v += state.x[i_] * state.c[i_]; } state.f = v; for (i_ = 0; i_ <= m - 1; i_++) { state.g[i_] = state.x[i_]; } continue; } if (state.needfgh) { v = 0.0; for (i_ = 0; i_ <= m - 1; i_++) { v += state.x[i_] * state.c[i_]; } state.f = v; for (i_ = 0; i_ <= m - 1; i_++) { state.g[i_] = state.x[i_]; } for (i = 0; i <= m - 1; i++) { for (j = 0; j <= m - 1; j++) { state.h[i, j] = 0; } } continue; } } }
public static bool testlls(bool silent) { bool result = new bool(); bool waserrors = new bool(); bool llserrors = new bool(); bool nlserrors = new bool(); double threshold = 0; double nlthreshold = 0; int maxn = 0; int maxm = 0; int passcount = 0; int n = 0; int m = 0; int i = 0; int j = 0; int k = 0; int pass = 0; double xscale = 0; double[] x = new double[0]; double[] y = new double[0]; double[] w = new double[0]; double[] w2 = new double[0]; double[] c = new double[0]; double[] c2 = new double[0]; double[,] a = new double[0, 0]; double[,] a2 = new double[0, 0]; double[,] cm = new double[0, 0]; double v = 0; double v1 = 0; double v2 = 0; lsfit.lsfitreport rep = new lsfit.lsfitreport(); lsfit.lsfitreport rep2 = new lsfit.lsfitreport(); int info = 0; int info2 = 0; double refrms = 0; double refavg = 0; double refavgrel = 0; double refmax = 0; lsfit.lsfitstate state = new lsfit.lsfitstate(); waserrors = false; llserrors = false; nlserrors = false; threshold = 10000 * AP.Math.MachineEpsilon; nlthreshold = 0.00001; maxn = 6; maxm = 6; passcount = 4; // // Testing unconstrained least squares (linear/nonlinear) // for (n = 1; n <= maxn; n++) { for (m = 1; m <= maxm; m++) { for (pass = 1; pass <= passcount; pass++) { // // Solve non-degenerate linear least squares task // Use Chebyshev basis. Its condition number is very good. // a = new double[n, m]; x = new double[n]; y = new double[n]; w = new double[n]; xscale = 0.9 + 0.1 * AP.Math.RandomReal(); for (i = 0; i <= n - 1; i++) { if (n == 1) { x[i] = 2 * AP.Math.RandomReal() - 1; } else { x[i] = xscale * ((double)(2 * i) / ((double)(n - 1)) - 1); } y[i] = 3 * x[i] + Math.Exp(x[i]); w[i] = 1 + AP.Math.RandomReal(); a[i, 0] = 1; if (m > 1) { a[i, 1] = x[i]; } for (j = 2; j <= m - 1; j++) { a[i, j] = 2 * x[i] * a[i, j - 1] - a[i, j - 2]; } } // // 1. test weighted fitting (optimality) // 2. Solve degenerate least squares task built on the basis // of previous task // lsfit.lsfitlinearw(ref y, ref w, ref a, n, m, ref info, ref c, ref rep); if (info <= 0) { llserrors = true; } else { llserrors = llserrors | !isglssolution(n, m, 0, ref y, ref w, ref a, ref cm, c); } a2 = new double[n, 2 * m]; for (i = 0; i <= n - 1; i++) { for (j = 0; j <= m - 1; j++) { a2[i, 2 * j + 0] = a[i, j]; a2[i, 2 * j + 1] = a[i, j]; } } lsfit.lsfitlinearw(ref y, ref w, ref a2, n, 2 * m, ref info, ref c2, ref rep); if (info <= 0) { llserrors = true; } else { // // test answer correctness using design matrix properties // and previous task solution // for (j = 0; j <= m - 1; j++) { llserrors = llserrors | (double)(Math.Abs(c2[2 * j + 0] + c2[2 * j + 1] - c[j])) > (double)(threshold); } } // // test non-weighted fitting // w2 = new double[n]; for (i = 0; i <= n - 1; i++) { w2[i] = 1; } lsfit.lsfitlinearw(ref y, ref w2, ref a, n, m, ref info, ref c, ref rep); lsfit.lsfitlinear(ref y, ref a, n, m, ref info2, ref c2, ref rep2); if (info <= 0 | info2 <= 0) { llserrors = true; } else { // // test answer correctness // for (j = 0; j <= m - 1; j++) { llserrors = llserrors | (double)(Math.Abs(c[j] - c2[j])) > (double)(threshold); } llserrors = llserrors | (double)(Math.Abs(rep.taskrcond - rep2.taskrcond)) > (double)(threshold); } // // test nonlinear fitting on the linear task // (only non-degenerate task are tested) // and compare with answer from linear fitting subroutine // if (n >= m) { c2 = new double[m]; // // test gradient-only or Hessian-based weighted fitting // lsfit.lsfitlinearw(ref y, ref w, ref a, n, m, ref info, ref c, ref rep); for (i = 0; i <= m - 1; i++) { c2[i] = 2 * AP.Math.RandomReal() - 1; } lsfit.lsfitnonlinearwfg(ref a, ref y, ref w, ref c2, n, m, m, (double)(AP.Math.RandomReal()) > (double)(0.5), ref state); lsfit.lsfitnonlinearsetcond(ref state, 0.0, nlthreshold, 0); fitlinearnonlinear(m, true, ref a, ref state, ref nlserrors); lsfit.lsfitnonlinearresults(ref state, ref info, ref c2, ref rep2); if (info <= 0) { nlserrors = true; } else { for (i = 0; i <= m - 1; i++) { nlserrors = nlserrors | (double)(Math.Abs(c[i] - c2[i])) > (double)(100 * nlthreshold); } } for (i = 0; i <= m - 1; i++) { c2[i] = 2 * AP.Math.RandomReal() - 1; } lsfit.lsfitnonlinearwfgh(ref a, ref y, ref w, ref c2, n, m, m, ref state); lsfit.lsfitnonlinearsetcond(ref state, 0.0, nlthreshold, 0); fitlinearnonlinear(m, false, ref a, ref state, ref nlserrors); lsfit.lsfitnonlinearresults(ref state, ref info, ref c2, ref rep2); if (info <= 0) { nlserrors = true; } else { for (i = 0; i <= m - 1; i++) { nlserrors = nlserrors | (double)(Math.Abs(c[i] - c2[i])) > (double)(100 * nlthreshold); } } // // test gradient-only or Hessian-based fitting without weights // lsfit.lsfitlinear(ref y, ref a, n, m, ref info, ref c, ref rep); for (i = 0; i <= m - 1; i++) { c2[i] = 2 * AP.Math.RandomReal() - 1; } lsfit.lsfitnonlinearfg(ref a, ref y, ref c2, n, m, m, (double)(AP.Math.RandomReal()) > (double)(0.5), ref state); lsfit.lsfitnonlinearsetcond(ref state, 0.0, nlthreshold, 0); fitlinearnonlinear(m, true, ref a, ref state, ref nlserrors); lsfit.lsfitnonlinearresults(ref state, ref info, ref c2, ref rep2); if (info <= 0) { nlserrors = true; } else { for (i = 0; i <= m - 1; i++) { nlserrors = nlserrors | (double)(Math.Abs(c[i] - c2[i])) > (double)(100 * nlthreshold); } } for (i = 0; i <= m - 1; i++) { c2[i] = 2 * AP.Math.RandomReal() - 1; } lsfit.lsfitnonlinearfgh(ref a, ref y, ref c2, n, m, m, ref state); lsfit.lsfitnonlinearsetcond(ref state, 0.0, nlthreshold, 0); fitlinearnonlinear(m, false, ref a, ref state, ref nlserrors); lsfit.lsfitnonlinearresults(ref state, ref info, ref c2, ref rep2); if (info <= 0) { nlserrors = true; } else { for (i = 0; i <= m - 1; i++) { nlserrors = nlserrors | (double)(Math.Abs(c[i] - c2[i])) > (double)(100 * nlthreshold); } } } } } // // test correctness of the RCond field // a = new double[n - 1 + 1, n - 1 + 1]; x = new double[n - 1 + 1]; y = new double[n - 1 + 1]; w = new double[n - 1 + 1]; v1 = AP.Math.MaxRealNumber; v2 = AP.Math.MinRealNumber; for (i = 0; i <= n - 1; i++) { x[i] = 0.1 + 0.9 * AP.Math.RandomReal(); y[i] = 0.1 + 0.9 * AP.Math.RandomReal(); w[i] = 1; for (j = 0; j <= n - 1; j++) { if (i == j) { a[i, i] = 0.1 + 0.9 * AP.Math.RandomReal(); v1 = Math.Min(v1, a[i, i]); v2 = Math.Max(v2, a[i, i]); } else { a[i, j] = 0; } } } lsfit.lsfitlinearw(ref y, ref w, ref a, n, n, ref info, ref c, ref rep); if (info <= 0) { llserrors = true; } else { llserrors = llserrors | (double)(Math.Abs(rep.taskrcond - v1 / v2)) > (double)(threshold); } } // // Test constrained least squares // for (pass = 1; pass <= passcount; pass++) { for (n = 1; n <= maxn; n++) { for (m = 1; m <= maxm; m++) { // // test for K<>0 // for (k = 1; k <= m - 1; k++) { // // Prepare Chebyshev basis. Its condition number is very good. // Prepare constraints (random numbers) // a = new double[n, m]; x = new double[n]; y = new double[n]; w = new double[n]; xscale = 0.9 + 0.1 * AP.Math.RandomReal(); for (i = 0; i <= n - 1; i++) { if (n == 1) { x[i] = 2 * AP.Math.RandomReal() - 1; } else { x[i] = xscale * ((double)(2 * i) / ((double)(n - 1)) - 1); } y[i] = 3 * x[i] + Math.Exp(x[i]); w[i] = 1 + AP.Math.RandomReal(); a[i, 0] = 1; if (m > 1) { a[i, 1] = x[i]; } for (j = 2; j <= m - 1; j++) { a[i, j] = 2 * x[i] * a[i, j - 1] - a[i, j - 2]; } } cm = new double[k, m + 1]; for (i = 0; i <= k - 1; i++) { for (j = 0; j <= m; j++) { cm[i, j] = 2 * AP.Math.RandomReal() - 1; } } // // Solve constrained task // lsfit.lsfitlinearwc(y, ref w, ref a, cm, n, m, k, ref info, ref c, ref rep); if (info <= 0) { llserrors = true; } else { llserrors = llserrors | !isglssolution(n, m, k, ref y, ref w, ref a, ref cm, c); } // // test non-weighted fitting // w2 = new double[n]; for (i = 0; i <= n - 1; i++) { w2[i] = 1; } lsfit.lsfitlinearwc(y, ref w2, ref a, cm, n, m, k, ref info, ref c, ref rep); lsfit.lsfitlinearc(y, ref a, ref cm, n, m, k, ref info2, ref c2, ref rep2); if (info <= 0 | info2 <= 0) { llserrors = true; } else { // // test answer correctness // for (j = 0; j <= m - 1; j++) { llserrors = llserrors | (double)(Math.Abs(c[j] - c2[j])) > (double)(threshold); } llserrors = llserrors | (double)(Math.Abs(rep.taskrcond - rep2.taskrcond)) > (double)(threshold); } } } } } // // nonlinear task for nonlinear fitting: // // f(X,C) = 1/(1+C*X^2), // C(true) = 2. // n = 100; c = new double[1]; c[0] = 1 + 2 * AP.Math.RandomReal(); a = new double[n, 1]; y = new double[n]; for (i = 0; i <= n - 1; i++) { a[i, 0] = 4 * AP.Math.RandomReal() - 2; y[i] = 1 / (1 + 2 * AP.Math.Sqr(a[i, 0])); } lsfit.lsfitnonlinearfg(ref a, ref y, ref c, n, 1, 1, true, ref state); lsfit.lsfitnonlinearsetcond(ref state, 0.0, nlthreshold, 0); while (lsfit.lsfitnonlineariteration(ref state)) { if (state.needf) { state.f = 1 / (1 + state.c[0] * AP.Math.Sqr(state.x[0])); } if (state.needfg) { state.f = 1 / (1 + state.c[0] * AP.Math.Sqr(state.x[0])); state.g[0] = -(AP.Math.Sqr(state.x[0]) / AP.Math.Sqr(1 + state.c[0] * AP.Math.Sqr(state.x[0]))); } } lsfit.lsfitnonlinearresults(ref state, ref info, ref c, ref rep); if (info <= 0) { nlserrors = true; } else { nlserrors = nlserrors | (double)(Math.Abs(c[0] - 2)) > (double)(100 * nlthreshold); } // // solve simple task (fitting by constant function) and check // correctness of the errors calculated by subroutines // for (pass = 1; pass <= passcount; pass++) { // // test on task with non-zero Yi // n = 4; v1 = AP.Math.RandomReal(); v2 = AP.Math.RandomReal(); v = 1 + AP.Math.RandomReal(); c = new double[1]; c[0] = 1 + 2 * AP.Math.RandomReal(); a = new double[4, 1]; y = new double[4]; a[0, 0] = 1; y[0] = v - v2; a[1, 0] = 1; y[1] = v - v1; a[2, 0] = 1; y[2] = v + v1; a[3, 0] = 1; y[3] = v + v2; refrms = Math.Sqrt((AP.Math.Sqr(v1) + AP.Math.Sqr(v2)) / 2); refavg = (Math.Abs(v1) + Math.Abs(v2)) / 2; refavgrel = 0.25 * (Math.Abs(v2) / Math.Abs(v - v2) + Math.Abs(v1) / Math.Abs(v - v1) + Math.Abs(v1) / Math.Abs(v + v1) + Math.Abs(v2) / Math.Abs(v + v2)); refmax = Math.Max(v1, v2); // // Test LLS // lsfit.lsfitlinear(ref y, ref a, 4, 1, ref info, ref c, ref rep); if (info <= 0) { llserrors = true; } else { llserrors = llserrors | (double)(Math.Abs(c[0] - v)) > (double)(threshold); llserrors = llserrors | (double)(Math.Abs(rep.rmserror - refrms)) > (double)(threshold); llserrors = llserrors | (double)(Math.Abs(rep.avgerror - refavg)) > (double)(threshold); llserrors = llserrors | (double)(Math.Abs(rep.avgrelerror - refavgrel)) > (double)(threshold); llserrors = llserrors | (double)(Math.Abs(rep.maxerror - refmax)) > (double)(threshold); } // // Test NLS // lsfit.lsfitnonlinearfg(ref a, ref y, ref c, 4, 1, 1, true, ref state); lsfit.lsfitnonlinearsetcond(ref state, 0.0, nlthreshold, 0); while (lsfit.lsfitnonlineariteration(ref state)) { if (state.needf) { state.f = state.c[0]; } if (state.needfg) { state.f = state.c[0]; state.g[0] = 1; } } lsfit.lsfitnonlinearresults(ref state, ref info, ref c, ref rep); if (info <= 0) { nlserrors = true; } else { nlserrors = nlserrors | (double)(Math.Abs(c[0] - v)) > (double)(threshold); nlserrors = nlserrors | (double)(Math.Abs(rep.rmserror - refrms)) > (double)(threshold); nlserrors = nlserrors | (double)(Math.Abs(rep.avgerror - refavg)) > (double)(threshold); nlserrors = nlserrors | (double)(Math.Abs(rep.avgrelerror - refavgrel)) > (double)(threshold); nlserrors = nlserrors | (double)(Math.Abs(rep.maxerror - refmax)) > (double)(threshold); } } // // report // waserrors = llserrors | nlserrors; if (!silent) { System.Console.Write("TESTING LEAST SQUARES"); System.Console.WriteLine(); System.Console.Write("LINEAR LEAST SQUARES: "); if (llserrors) { System.Console.Write("FAILED"); System.Console.WriteLine(); } else { System.Console.Write("OK"); System.Console.WriteLine(); } System.Console.Write("NON-LINEAR LEAST SQUARES: "); if (nlserrors) { System.Console.Write("FAILED"); System.Console.WriteLine(); } else { System.Console.Write("OK"); System.Console.WriteLine(); } if (waserrors) { System.Console.Write("TEST FAILED"); System.Console.WriteLine(); } else { System.Console.Write("TEST PASSED"); System.Console.WriteLine(); } System.Console.WriteLine(); System.Console.WriteLine(); } // // end // result = !waserrors; return(result); }
public static int Main(string[] args) { int m = 0; int n = 0; int k = 0; double[] y = new double[0]; double[,] x = new double[0, 0]; double[] c = new double[0]; lsfit.lsfitreport rep = new lsfit.lsfitreport(); lsfit.lsfitstate state = new lsfit.lsfitstate(); int info = 0; double epsf = 0; double epsx = 0; int maxits = 0; int i = 0; int j = 0; double a = 0; double b = 0; System.Console.Write("Fitting 1-x^2 on [-1,+1] with cos(alpha*pi*x)+beta"); System.Console.WriteLine(); // // Fitting 1-x^2 on [-1,+1] with cos(alpha*pi*x)+beta: // * using Hessian // * using alpha=1 and beta=0 as initial values // * using 1000 uniformly distributed points to fit to // // Notes: // * N - number of points // * M - dimension of space where points reside // * K - number of parameters being fitted // n = 1000; m = 1; k = 2; a = -1; b = +1; // // Prepare task matrix // y = new double[n]; x = new double[n, m]; c = new double[k]; for (i = 0; i <= n - 1; i++) { x[i, 0] = a + (b - a) * i / (n - 1); y[i] = 1 - AP.Math.Sqr(x[i, 0]); } c[0] = 1.0; c[1] = 0.0; epsf = 0.0; epsx = 0.0001; maxits = 0; // // Solve // lsfit.lsfitnonlinearfgh(ref x, ref y, ref c, n, m, k, ref state); lsfit.lsfitnonlinearsetcond(ref state, epsf, epsx, maxits); while (lsfit.lsfitnonlineariteration(ref state)) { // // F(x) = Cos(alpha*pi*x)+beta // state.f = Math.Cos(state.c[0] * Math.PI * state.x[0]) + state.c[1]; // // F(x) = Cos(alpha*pi*x)+beta // dF/dAlpha = -pi*x*Sin(alpha*pi*x) // dF/dBeta = 1.0 // if (state.needfg | state.needfgh) { state.g[0] = -(Math.PI * state.x[0] * Math.Sin(state.c[0] * Math.PI * state.x[0])); state.g[1] = 1.0; } // // F(x) = Cos(alpha*pi*x)+beta // d2F/dAlpha2 = -(pi*x)^2*Cos(alpha*pi*x) // d2F/dAlphadBeta = 0 // d2F/dBeta2 = 0 // if (state.needfgh) { state.h[0, 0] = -(AP.Math.Sqr(Math.PI * state.x[0]) * Math.Cos(state.c[0] * Math.PI * state.x[0])); state.h[0, 1] = 0.0; state.h[1, 0] = 0.0; state.h[1, 1] = 0.0; } } lsfit.lsfitnonlinearresults(ref state, ref info, ref c, ref rep); System.Console.Write("alpha: "); System.Console.Write("{0,0:F3}", c[0]); System.Console.WriteLine(); System.Console.Write("beta: "); System.Console.Write("{0,0:F3}", c[1]); System.Console.WriteLine(); System.Console.Write("rms.err: "); System.Console.Write("{0,0:F3}", rep.rmserror); System.Console.WriteLine(); System.Console.Write("max.err: "); System.Console.Write("{0,0:F3}", rep.maxerror); System.Console.WriteLine(); System.Console.Write("Termination type: "); System.Console.Write("{0,0:d}", info); System.Console.WriteLine(); System.Console.WriteLine(); System.Console.WriteLine(); return(0); }