/// <summary>
        /// Cubic spline is obtained by solving a linear problem Ax=b where A is a square matrix and x,b are vector
        /// This can be done by LU decomposition </summary>
        /// <param name="doubMat"> Matrix A </param>
        /// <param name="doubVec"> Vector B </param>
        /// <returns> Solution to the linear equation, x </returns>
        protected internal virtual double[] matrixEqnSolver(double[][] doubMat, double[] doubVec)
        {
            LUDecompositionResult result = _luObj.apply(DoubleMatrix.copyOf(doubMat));

            double[][]  lMat       = result.L.toArray();
            double[][]  uMat       = result.U.toArray();
            DoubleArray doubVecMod = ((DoubleArray)OG_ALGEBRA.multiply(result.P, DoubleArray.copyOf(doubVec)));

            return(backSubstitution(uMat, forwardSubstitution(lMat, doubVecMod)));
        }
Пример #2
0
        private GeneralizedLeastSquareResults <T> solveImp <T>(IList <T> x, IList <double> y, IList <double> sigma, IList <System.Func <T, double> > basisFunctions, double lambda, int differenceOrder)
        {
            int n = x.Count;

            int m = basisFunctions.Count;

            double[] b = new double[m];

            double[] invSigmaSqr = new double[n];
//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][] f = new double[m][n];
            double[][] f = RectangularArrays.ReturnRectangularDoubleArray(m, n);
            int        i, j, k;

            for (i = 0; i < n; i++)
            {
                double temp = sigma[i];
                ArgChecker.isTrue(temp > 0, "sigma must be greater than zero");
                invSigmaSqr[i] = 1.0 / temp / temp;
            }

            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    f[i][j] = basisFunctions[i](x[j]);
                }
            }

            double sum;

            for (i = 0; i < m; i++)
            {
                sum = 0;
                for (k = 0; k < n; k++)
                {
                    sum += y[k] * f[i][k] * invSigmaSqr[k];
                }
                b[i] = sum;
            }

            DoubleArray  mb = DoubleArray.copyOf(b);
            DoubleMatrix ma = getAMatrix(f, invSigmaSqr);

            if (lambda > 0.0)
            {
                DoubleMatrix d = getDiffMatrix(m, differenceOrder);
                ma = (DoubleMatrix)_algebra.add(ma, _algebra.scale(d, lambda));
            }

            DecompositionResult decmp = _decomposition.apply(ma);
            DoubleArray         w     = decmp.solve(mb);
            DoubleMatrix        covar = decmp.solve(DoubleMatrix.identity(m));

            double chiSq = 0;

            for (i = 0; i < n; i++)
            {
                double temp = 0;
                for (k = 0; k < m; k++)
                {
                    temp += w.get(k) * f[k][i];
                }
                chiSq += FunctionUtils.square(y[i] - temp) * invSigmaSqr[i];
            }

            return(new GeneralizedLeastSquareResults <T>(basisFunctions, chiSq, w, covar));
        }