コード例 #1
0
        /*************************************************************************
        Weighted  fitting  by  Chebyshev  polynomial  in  barycentric  form,  with
        constraints on function values or first derivatives.

        Small regularizing term is used when solving constrained tasks (to improve
        stability).

        Task is linear, so linear least squares solver is used. Complexity of this
        computational scheme is O(N*M^2), mostly dominated by least squares solver

        SEE ALSO:
            PolynomialFit()

        INPUT PARAMETERS:
            X   -   points, array[0..N-1].
            Y   -   function values, array[0..N-1].
            W   -   weights, array[0..N-1]
                    Each summand in square  sum  of  approximation deviations from
                    given  values  is  multiplied  by  the square of corresponding
                    weight. Fill it by 1's if you don't  want  to  solve  weighted
                    task.
            N   -   number of points, N>0.
            XC  -   points where polynomial values/derivatives are constrained,
                    array[0..K-1].
            YC  -   values of constraints, array[0..K-1]
            DC  -   array[0..K-1], types of constraints:
                    * DC[i]=0   means that P(XC[i])=YC[i]
                    * DC[i]=1   means that P'(XC[i])=YC[i]
                    SEE BELOW FOR IMPORTANT INFORMATION ON CONSTRAINTS
            K   -   number of constraints, 0<=K<M.
                    K=0 means no constraints (XC/YC/DC are not used in such cases)
            M   -   number of basis functions (= polynomial_degree + 1), M>=1

        OUTPUT PARAMETERS:
            Info-   same format as in LSFitLinearW() subroutine:
                    * Info>0    task is solved
                    * Info<=0   an error occured:
                                -4 means inconvergence of internal SVD
                                -3 means inconsistent constraints
                                -1 means another errors in parameters passed
                                   (N<=0, for example)
            P   -   interpolant in barycentric form.
            Rep -   report, same format as in LSFitLinearW() subroutine.
                    Following fields are set:
                    * RMSError      rms error on the (X,Y).
                    * AvgError      average error on the (X,Y).
                    * AvgRelError   average relative error on the non-zero Y
                    * MaxError      maximum error
                                    NON-WEIGHTED ERRORS ARE CALCULATED

        IMPORTANT:
            this subroitine doesn't calculate task's condition number for K<>0.

        SETTING CONSTRAINTS - DANGERS AND OPPORTUNITIES:

        Setting constraints can lead  to undesired  results,  like ill-conditioned
        behavior, or inconsistency being detected. From the other side,  it allows
        us to improve quality of the fit. Here we summarize  our  experience  with
        constrained regression splines:
        * even simple constraints can be inconsistent, see  Wikipedia  article  on
          this subject: http://en.wikipedia.org/wiki/Birkhoff_interpolation
        * the  greater  is  M (given  fixed  constraints),  the  more chances that
          constraints will be consistent
        * in the general case, consistency of constraints is NOT GUARANTEED.
        * in the one special cases, however, we can  guarantee  consistency.  This
          case  is:  M>1  and constraints on the function values (NOT DERIVATIVES)

        Our final recommendation is to use constraints  WHEN  AND  ONLY  when  you
        can't solve your task without them. Anything beyond  special  cases  given
        above is not guaranteed and may result in inconsistency.

          -- ALGLIB PROJECT --
             Copyright 10.12.2009 by Bochkanov Sergey
        *************************************************************************/
        public static void polynomialfitwc(double[] x,
            double[] y,
            ref double[] w,
            int n,
            double[] xc,
            double[] yc,
            ref int[] dc,
            int k,
            int m,
            ref int info,
            ref ratint.barycentricinterpolant p,
            ref polynomialfitreport rep)
        {
            double xa = 0;
            double xb = 0;
            double sa = 0;
            double sb = 0;
            double[] xoriginal = new double[0];
            double[] yoriginal = new double[0];
            double[] y2 = new double[0];
            double[] w2 = new double[0];
            double[] tmp = new double[0];
            double[] tmp2 = new double[0];
            double[] tmpdiff = new double[0];
            double[] bx = new double[0];
            double[] by = new double[0];
            double[] bw = new double[0];
            double[,] fmatrix = new double[0,0];
            double[,] cmatrix = new double[0,0];
            int i = 0;
            int j = 0;
            double mx = 0;
            double decay = 0;
            double u = 0;
            double v = 0;
            double s = 0;
            int relcnt = 0;
            lsfit.lsfitreport lrep = new lsfit.lsfitreport();
            int i_ = 0;

            x = (double[])x.Clone();
            y = (double[])y.Clone();
            xc = (double[])xc.Clone();
            yc = (double[])yc.Clone();

            if( m<1 | n<1 | k<0 | k>=m )
            {
                info = -1;
                return;
            }
            for(i=0; i<=k-1; i++)
            {
                info = 0;
                if( dc[i]<0 )
                {
                    info = -1;
                }
                if( dc[i]>1 )
                {
                    info = -1;
                }
                if( info<0 )
                {
                    return;
                }
            }
            
            //
            // weight decay for correct handling of task which becomes
            // degenerate after constraints are applied
            //
            decay = 10000*AP.Math.MachineEpsilon;
            
            //
            // Scale X, Y, XC, YC
            //
            lsfit.lsfitscalexy(ref x, ref y, n, ref xc, ref yc, ref dc, k, ref xa, ref xb, ref sa, ref sb, ref xoriginal, ref yoriginal);
            
            //
            // allocate space, initialize/fill:
            // * FMatrix-   values of basis functions at X[]
            // * CMatrix-   values (derivatives) of basis functions at XC[]
            // * fill constraints matrix
            // * fill first N rows of design matrix with values
            // * fill next M rows of design matrix with regularizing term
            // * append M zeros to Y
            // * append M elements, mean(abs(W)) each, to W
            //
            y2 = new double[n+m];
            w2 = new double[n+m];
            tmp = new double[m];
            tmpdiff = new double[m];
            fmatrix = new double[n+m, m];
            if( k>0 )
            {
                cmatrix = new double[k, m+1];
            }
            
            //
            // Fill design matrix, Y2, W2:
            // * first N rows with basis functions for original points
            // * next M rows with decay terms
            //
            for(i=0; i<=n-1; i++)
            {
                
                //
                // prepare Ith row
                // use Tmp for calculations to avoid multidimensional arrays overhead
                //
                for(j=0; j<=m-1; j++)
                {
                    if( j==0 )
                    {
                        tmp[j] = 1;
                    }
                    else
                    {
                        if( j==1 )
                        {
                            tmp[j] = x[i];
                        }
                        else
                        {
                            tmp[j] = 2*x[i]*tmp[j-1]-tmp[j-2];
                        }
                    }
                }
                for(i_=0; i_<=m-1;i_++)
                {
                    fmatrix[i,i_] = tmp[i_];
                }
            }
            for(i=0; i<=m-1; i++)
            {
                for(j=0; j<=m-1; j++)
                {
                    if( i==j )
                    {
                        fmatrix[n+i,j] = decay;
                    }
                    else
                    {
                        fmatrix[n+i,j] = 0;
                    }
                }
            }
            for(i_=0; i_<=n-1;i_++)
            {
                y2[i_] = y[i_];
            }
            for(i_=0; i_<=n-1;i_++)
            {
                w2[i_] = w[i_];
            }
            mx = 0;
            for(i=0; i<=n-1; i++)
            {
                mx = mx+Math.Abs(w[i]);
            }
            mx = mx/n;
            for(i=0; i<=m-1; i++)
            {
                y2[n+i] = 0;
                w2[n+i] = mx;
            }
            
            //
            // fill constraints matrix
            //
            for(i=0; i<=k-1; i++)
            {
                
                //
                // prepare Ith row
                // use Tmp for basis function values,
                // TmpDiff for basos function derivatives
                //
                for(j=0; j<=m-1; j++)
                {
                    if( j==0 )
                    {
                        tmp[j] = 1;
                        tmpdiff[j] = 0;
                    }
                    else
                    {
                        if( j==1 )
                        {
                            tmp[j] = xc[i];
                            tmpdiff[j] = 1;
                        }
                        else
                        {
                            tmp[j] = 2*xc[i]*tmp[j-1]-tmp[j-2];
                            tmpdiff[j] = 2*(tmp[j-1]+xc[i]*tmpdiff[j-1])-tmpdiff[j-2];
                        }
                    }
                }
                if( dc[i]==0 )
                {
                    for(i_=0; i_<=m-1;i_++)
                    {
                        cmatrix[i,i_] = tmp[i_];
                    }
                }
                if( dc[i]==1 )
                {
                    for(i_=0; i_<=m-1;i_++)
                    {
                        cmatrix[i,i_] = tmpdiff[i_];
                    }
                }
                cmatrix[i,m] = yc[i];
            }
            
            //
            // Solve constrained task
            //
            if( k>0 )
            {
                
                //
                // solve using regularization
                //
                lsfit.lsfitlinearwc(y2, ref w2, ref fmatrix, cmatrix, n+m, m, k, ref info, ref tmp, ref lrep);
            }
            else
            {
                
                //
                // no constraints, no regularization needed
                //
                lsfit.lsfitlinearwc(y, ref w, ref fmatrix, cmatrix, n, m, 0, ref info, ref tmp, ref lrep);
            }
            if( info<0 )
            {
                return;
            }
            
            //
            // Generate barycentric model and scale it
            // * BX, BY store barycentric model nodes
            // * FMatrix is reused (remember - it is at least MxM, what we need)
            //
            // Model intialization is done in O(M^2). In principle, it can be
            // done in O(M*log(M)), but before it we solved task with O(N*M^2)
            // complexity, so it is only a small amount of total time spent.
            //
            bx = new double[m];
            by = new double[m];
            bw = new double[m];
            tmp2 = new double[m];
            s = 1;
            for(i=0; i<=m-1; i++)
            {
                if( m!=1 )
                {
                    u = Math.Cos(Math.PI*i/(m-1));
                }
                else
                {
                    u = 0;
                }
                v = 0;
                for(j=0; j<=m-1; j++)
                {
                    if( j==0 )
                    {
                        tmp2[j] = 1;
                    }
                    else
                    {
                        if( j==1 )
                        {
                            tmp2[j] = u;
                        }
                        else
                        {
                            tmp2[j] = 2*u*tmp2[j-1]-tmp2[j-2];
                        }
                    }
                    v = v+tmp[j]*tmp2[j];
                }
                bx[i] = u;
                by[i] = v;
                bw[i] = s;
                if( i==0 | i==m-1 )
                {
                    bw[i] = 0.5*bw[i];
                }
                s = -s;
            }
            ratint.barycentricbuildxyw(ref bx, ref by, ref bw, m, ref p);
            ratint.barycentriclintransx(ref p, 2/(xb-xa), -((xa+xb)/(xb-xa)));
            ratint.barycentriclintransy(ref p, sb-sa, sa);
            
            //
            // Scale absolute errors obtained from LSFitLinearW.
            // Relative error should be calculated separately
            // (because of shifting/scaling of the task)
            //
            rep.taskrcond = lrep.taskrcond;
            rep.rmserror = lrep.rmserror*(sb-sa);
            rep.avgerror = lrep.avgerror*(sb-sa);
            rep.maxerror = lrep.maxerror*(sb-sa);
            rep.avgrelerror = 0;
            relcnt = 0;
            for(i=0; i<=n-1; i++)
            {
                if( (double)(yoriginal[i])!=(double)(0) )
                {
                    rep.avgrelerror = rep.avgrelerror+Math.Abs(ratint.barycentriccalc(ref p, xoriginal[i])-yoriginal[i])/Math.Abs(yoriginal[i]);
                    relcnt = relcnt+1;
                }
            }
            if( relcnt!=0 )
            {
                rep.avgrelerror = rep.avgrelerror/relcnt;
            }
        }
コード例 #2
0
        /*************************************************************************
        Internal spline fitting subroutine

          -- ALGLIB PROJECT --
             Copyright 08.09.2009 by Bochkanov Sergey
        *************************************************************************/
        private static void spline1dfitinternal(int st,
            double[] x,
            double[] y,
            ref double[] w,
            int n,
            double[] xc,
            double[] yc,
            ref int[] dc,
            int k,
            int m,
            ref int info,
            ref spline1dinterpolant s,
            ref spline1dfitreport rep)
        {
            double[,] fmatrix = new double[0,0];
            double[,] cmatrix = new double[0,0];
            double[] y2 = new double[0];
            double[] w2 = new double[0];
            double[] sx = new double[0];
            double[] sy = new double[0];
            double[] sd = new double[0];
            double[] tmp = new double[0];
            double[] xoriginal = new double[0];
            double[] yoriginal = new double[0];
            lsfit.lsfitreport lrep = new lsfit.lsfitreport();
            double v0 = 0;
            double v1 = 0;
            double v2 = 0;
            double mx = 0;
            spline1dinterpolant s2 = new spline1dinterpolant();
            int i = 0;
            int j = 0;
            int relcnt = 0;
            double xa = 0;
            double xb = 0;
            double sa = 0;
            double sb = 0;
            double bl = 0;
            double br = 0;
            double decay = 0;
            int i_ = 0;

            x = (double[])x.Clone();
            y = (double[])y.Clone();
            xc = (double[])xc.Clone();
            yc = (double[])yc.Clone();

            System.Diagnostics.Debug.Assert(st==0 | st==1, "Spline1DFit: internal error!");
            if( st==0 & m<4 )
            {
                info = -1;
                return;
            }
            if( st==1 & m<4 )
            {
                info = -1;
                return;
            }
            if( n<1 | k<0 | k>=m )
            {
                info = -1;
                return;
            }
            for(i=0; i<=k-1; i++)
            {
                info = 0;
                if( dc[i]<0 )
                {
                    info = -1;
                }
                if( dc[i]>1 )
                {
                    info = -1;
                }
                if( info<0 )
                {
                    return;
                }
            }
            if( st==1 & m%2!=0 )
            {
                
                //
                // Hermite fitter must have even number of basis functions
                //
                info = -2;
                return;
            }
            
            //
            // weight decay for correct handling of task which becomes
            // degenerate after constraints are applied
            //
            decay = 10000*AP.Math.MachineEpsilon;
            
            //
            // Scale X, Y, XC, YC
            //
            lsfit.lsfitscalexy(ref x, ref y, n, ref xc, ref yc, ref dc, k, ref xa, ref xb, ref sa, ref sb, ref xoriginal, ref yoriginal);
            
            //
            // allocate space, initialize:
            // * SX     -   grid for basis functions
            // * SY     -   values of basis functions at grid points
            // * FMatrix-   values of basis functions at X[]
            // * CMatrix-   values (derivatives) of basis functions at XC[]
            //
            y2 = new double[n+m];
            w2 = new double[n+m];
            fmatrix = new double[n+m, m];
            if( k>0 )
            {
                cmatrix = new double[k, m+1];
            }
            if( st==0 )
            {
                
                //
                // allocate space for cubic spline
                //
                sx = new double[m-2];
                sy = new double[m-2];
                for(j=0; j<=m-2-1; j++)
                {
                    sx[j] = (double)(2*j)/((double)(m-2-1))-1;
                }
            }
            if( st==1 )
            {
                
                //
                // allocate space for Hermite spline
                //
                sx = new double[m/2];
                sy = new double[m/2];
                sd = new double[m/2];
                for(j=0; j<=m/2-1; j++)
                {
                    sx[j] = (double)(2*j)/((double)(m/2-1))-1;
                }
            }
            
            //
            // Prepare design and constraints matrices:
            // * fill constraints matrix
            // * fill first N rows of design matrix with values
            // * fill next M rows of design matrix with regularizing term
            // * append M zeros to Y
            // * append M elements, mean(abs(W)) each, to W
            //
            for(j=0; j<=m-1; j++)
            {
                
                //
                // prepare Jth basis function
                //
                if( st==0 )
                {
                    
                    //
                    // cubic spline basis
                    //
                    for(i=0; i<=m-2-1; i++)
                    {
                        sy[i] = 0;
                    }
                    bl = 0;
                    br = 0;
                    if( j<m-2 )
                    {
                        sy[j] = 1;
                    }
                    if( j==m-2 )
                    {
                        bl = 1;
                    }
                    if( j==m-1 )
                    {
                        br = 1;
                    }
                    spline1dbuildcubic(sx, sy, m-2, 1, bl, 1, br, ref s2);
                }
                if( st==1 )
                {
                    
                    //
                    // Hermite basis
                    //
                    for(i=0; i<=m/2-1; i++)
                    {
                        sy[i] = 0;
                        sd[i] = 0;
                    }
                    if( j%2==0 )
                    {
                        sy[j/2] = 1;
                    }
                    else
                    {
                        sd[j/2] = 1;
                    }
                    spline1dbuildhermite(sx, sy, sd, m/2, ref s2);
                }
                
                //
                // values at X[], XC[]
                //
                for(i=0; i<=n-1; i++)
                {
                    fmatrix[i,j] = spline1dcalc(ref s2, x[i]);
                }
                for(i=0; i<=k-1; i++)
                {
                    System.Diagnostics.Debug.Assert(dc[i]>=0 & dc[i]<=2, "Spline1DFit: internal error!");
                    spline1ddiff(ref s2, xc[i], ref v0, ref v1, ref v2);
                    if( dc[i]==0 )
                    {
                        cmatrix[i,j] = v0;
                    }
                    if( dc[i]==1 )
                    {
                        cmatrix[i,j] = v1;
                    }
                    if( dc[i]==2 )
                    {
                        cmatrix[i,j] = v2;
                    }
                }
            }
            for(i=0; i<=k-1; i++)
            {
                cmatrix[i,m] = yc[i];
            }
            for(i=0; i<=m-1; i++)
            {
                for(j=0; j<=m-1; j++)
                {
                    if( i==j )
                    {
                        fmatrix[n+i,j] = decay;
                    }
                    else
                    {
                        fmatrix[n+i,j] = 0;
                    }
                }
            }
            y2 = new double[n+m];
            w2 = new double[n+m];
            for(i_=0; i_<=n-1;i_++)
            {
                y2[i_] = y[i_];
            }
            for(i_=0; i_<=n-1;i_++)
            {
                w2[i_] = w[i_];
            }
            mx = 0;
            for(i=0; i<=n-1; i++)
            {
                mx = mx+Math.Abs(w[i]);
            }
            mx = mx/n;
            for(i=0; i<=m-1; i++)
            {
                y2[n+i] = 0;
                w2[n+i] = mx;
            }
            
            //
            // Solve constrained task
            //
            if( k>0 )
            {
                
                //
                // solve using regularization
                //
                lsfit.lsfitlinearwc(y2, ref w2, ref fmatrix, cmatrix, n+m, m, k, ref info, ref tmp, ref lrep);
            }
            else
            {
                
                //
                // no constraints, no regularization needed
                //
                lsfit.lsfitlinearwc(y, ref w, ref fmatrix, cmatrix, n, m, k, ref info, ref tmp, ref lrep);
            }
            if( info<0 )
            {
                return;
            }
            
            //
            // Generate spline and scale it
            //
            if( st==0 )
            {
                
                //
                // cubic spline basis
                //
                for(i_=0; i_<=m-2-1;i_++)
                {
                    sy[i_] = tmp[i_];
                }
                spline1dbuildcubic(sx, sy, m-2, 1, tmp[m-2], 1, tmp[m-1], ref s);
            }
            if( st==1 )
            {
                
                //
                // Hermite basis
                //
                for(i=0; i<=m/2-1; i++)
                {
                    sy[i] = tmp[2*i];
                    sd[i] = tmp[2*i+1];
                }
                spline1dbuildhermite(sx, sy, sd, m/2, ref s);
            }
            spline1dlintransx(ref s, 2/(xb-xa), -((xa+xb)/(xb-xa)));
            spline1dlintransy(ref s, sb-sa, sa);
            
            //
            // Scale absolute errors obtained from LSFitLinearW.
            // Relative error should be calculated separately
            // (because of shifting/scaling of the task)
            //
            rep.taskrcond = lrep.taskrcond;
            rep.rmserror = lrep.rmserror*(sb-sa);
            rep.avgerror = lrep.avgerror*(sb-sa);
            rep.maxerror = lrep.maxerror*(sb-sa);
            rep.avgrelerror = 0;
            relcnt = 0;
            for(i=0; i<=n-1; i++)
            {
                if( (double)(yoriginal[i])!=(double)(0) )
                {
                    rep.avgrelerror = rep.avgrelerror+Math.Abs(spline1dcalc(ref s, xoriginal[i])-yoriginal[i])/Math.Abs(yoriginal[i]);
                    relcnt = relcnt+1;
                }
            }
            if( relcnt!=0 )
            {
                rep.avgrelerror = rep.avgrelerror/relcnt;
            }
        }
コード例 #3
0
        /*************************************************************************
        Internal Floater-Hormann fitting subroutine for fixed D
        *************************************************************************/
        private static void barycentricfitwcfixedd(double[] x,
            double[] y,
            ref double[] w,
            int n,
            double[] xc,
            double[] yc,
            ref int[] dc,
            int k,
            int m,
            int d,
            ref int info,
            ref barycentricinterpolant b,
            ref barycentricfitreport rep)
        {
            double[,] fmatrix = new double[0,0];
            double[,] cmatrix = new double[0,0];
            double[] y2 = new double[0];
            double[] w2 = new double[0];
            double[] sx = new double[0];
            double[] sy = new double[0];
            double[] sbf = new double[0];
            double[] xoriginal = new double[0];
            double[] yoriginal = new double[0];
            double[] tmp = new double[0];
            lsfit.lsfitreport lrep = new lsfit.lsfitreport();
            double v0 = 0;
            double v1 = 0;
            double mx = 0;
            barycentricinterpolant b2 = new barycentricinterpolant();
            int i = 0;
            int j = 0;
            int relcnt = 0;
            double xa = 0;
            double xb = 0;
            double sa = 0;
            double sb = 0;
            double decay = 0;
            int i_ = 0;

            x = (double[])x.Clone();
            y = (double[])y.Clone();
            xc = (double[])xc.Clone();
            yc = (double[])yc.Clone();

            if( n<1 | m<2 | k<0 | k>=m )
            {
                info = -1;
                return;
            }
            for(i=0; i<=k-1; i++)
            {
                info = 0;
                if( dc[i]<0 )
                {
                    info = -1;
                }
                if( dc[i]>1 )
                {
                    info = -1;
                }
                if( info<0 )
                {
                    return;
                }
            }
            
            //
            // weight decay for correct handling of task which becomes
            // degenerate after constraints are applied
            //
            decay = 10000*AP.Math.MachineEpsilon;
            
            //
            // Scale X, Y, XC, YC
            //
            lsfit.lsfitscalexy(ref x, ref y, n, ref xc, ref yc, ref dc, k, ref xa, ref xb, ref sa, ref sb, ref xoriginal, ref yoriginal);
            
            //
            // allocate space, initialize:
            // * FMatrix-   values of basis functions at X[]
            // * CMatrix-   values (derivatives) of basis functions at XC[]
            //
            y2 = new double[n+m];
            w2 = new double[n+m];
            fmatrix = new double[n+m, m];
            if( k>0 )
            {
                cmatrix = new double[k, m+1];
            }
            y2 = new double[n+m];
            w2 = new double[n+m];
            
            //
            // Prepare design and constraints matrices:
            // * fill constraints matrix
            // * fill first N rows of design matrix with values
            // * fill next M rows of design matrix with regularizing term
            // * append M zeros to Y
            // * append M elements, mean(abs(W)) each, to W
            //
            sx = new double[m];
            sy = new double[m];
            sbf = new double[m];
            for(j=0; j<=m-1; j++)
            {
                sx[j] = (double)(2*j)/((double)(m-1))-1;
            }
            for(i=0; i<=m-1; i++)
            {
                sy[i] = 1;
            }
            barycentricbuildfloaterhormann(ref sx, ref sy, m, d, ref b2);
            mx = 0;
            for(i=0; i<=n-1; i++)
            {
                barycentriccalcbasis(ref b2, x[i], ref sbf);
                for(i_=0; i_<=m-1;i_++)
                {
                    fmatrix[i,i_] = sbf[i_];
                }
                y2[i] = y[i];
                w2[i] = w[i];
                mx = mx+Math.Abs(w[i])/n;
            }
            for(i=0; i<=m-1; i++)
            {
                for(j=0; j<=m-1; j++)
                {
                    if( i==j )
                    {
                        fmatrix[n+i,j] = decay;
                    }
                    else
                    {
                        fmatrix[n+i,j] = 0;
                    }
                }
                y2[n+i] = 0;
                w2[n+i] = mx;
            }
            if( k>0 )
            {
                for(j=0; j<=m-1; j++)
                {
                    for(i=0; i<=m-1; i++)
                    {
                        sy[i] = 0;
                    }
                    sy[j] = 1;
                    barycentricbuildfloaterhormann(ref sx, ref sy, m, d, ref b2);
                    for(i=0; i<=k-1; i++)
                    {
                        System.Diagnostics.Debug.Assert(dc[i]>=0 & dc[i]<=1, "BarycentricFit: internal error!");
                        barycentricdiff1(ref b2, xc[i], ref v0, ref v1);
                        if( dc[i]==0 )
                        {
                            cmatrix[i,j] = v0;
                        }
                        if( dc[i]==1 )
                        {
                            cmatrix[i,j] = v1;
                        }
                    }
                }
                for(i=0; i<=k-1; i++)
                {
                    cmatrix[i,m] = yc[i];
                }
            }
            
            //
            // Solve constrained task
            //
            if( k>0 )
            {
                
                //
                // solve using regularization
                //
                lsfit.lsfitlinearwc(y2, ref w2, ref fmatrix, cmatrix, n+m, m, k, ref info, ref tmp, ref lrep);
            }
            else
            {
                
                //
                // no constraints, no regularization needed
                //
                lsfit.lsfitlinearwc(y, ref w, ref fmatrix, cmatrix, n, m, k, ref info, ref tmp, ref lrep);
            }
            if( info<0 )
            {
                return;
            }
            
            //
            // Generate interpolant and scale it
            //
            for(i_=0; i_<=m-1;i_++)
            {
                sy[i_] = tmp[i_];
            }
            barycentricbuildfloaterhormann(ref sx, ref sy, m, d, ref b);
            barycentriclintransx(ref b, 2/(xb-xa), -((xa+xb)/(xb-xa)));
            barycentriclintransy(ref b, sb-sa, sa);
            
            //
            // Scale absolute errors obtained from LSFitLinearW.
            // Relative error should be calculated separately
            // (because of shifting/scaling of the task)
            //
            rep.taskrcond = lrep.taskrcond;
            rep.rmserror = lrep.rmserror*(sb-sa);
            rep.avgerror = lrep.avgerror*(sb-sa);
            rep.maxerror = lrep.maxerror*(sb-sa);
            rep.avgrelerror = 0;
            relcnt = 0;
            for(i=0; i<=n-1; i++)
            {
                if( (double)(yoriginal[i])!=(double)(0) )
                {
                    rep.avgrelerror = rep.avgrelerror+Math.Abs(barycentriccalc(ref b, xoriginal[i])-yoriginal[i])/Math.Abs(yoriginal[i]);
                    relcnt = relcnt+1;
                }
            }
            if( relcnt!=0 )
            {
                rep.avgrelerror = rep.avgrelerror/relcnt;
            }
        }
コード例 #4
0
        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;
        }