Ejemplo n.º 1
0
            /**
             * Solves the linear equation A × X = B for symmetric matrices A.
             * <p>
             * This method only finds exact linear solutions, i.e. solutions for
             * which ||A &times; X - B|| is exactly 0.
             * </p>
             *
             * @param b Right-hand side of the equation A &times; X = B.
             * @return a Vector X that minimizes the two norm of A &times; X - B.
             *
             * @throws DimensionMismatchException if the matrices dimensions do not match.
             * @throws SingularMatrixException if the decomposed matrix is singular.
             */
            public RealVector solve(RealVector b)
            {
                if (!isNonSingular())
                {
                    throw new Exception("SingularMatrixException");
                }

                int m = realEigenvalues.Length;

                if (b.getDimension() != m)
                {
                    throw new Exception("DimensionMismatchException");
                }

                double[] bp = new double[m];
                for (int i = 0; i < m; ++i)
                {
                    ArrayRealVector v     = eigenvectors[i];
                    double[]        vData = v.getDataRef();
                    double          s     = v.dotProduct(b) / realEigenvalues[i];
                    for (int j = 0; j < m; ++j)
                    {
                        bp[j] += s * vData[j];
                    }
                }

                return(new ArrayRealVector(bp, false));
            }
Ejemplo n.º 2
0
            /** {@inheritDoc} */
            public RealMatrix solve(RealMatrix b)
            {
                if (!isNonSingular())
                {
                    throw new Exception("SingularMatrixException");
                }

                int m = realEigenvalues.Length;

                if (b.getRowDimension() != m)
                {
                    throw new Exception("DimensionMismatchException");
                }

                int nColB = b.getColumnDimension();

                double[][] bp     = Java.CreateArray <double[][]>(m, nColB);// new double[m][nColB];
                double[]   tmpCol = new double[m];
                for (int k = 0; k < nColB; ++k)
                {
                    for (int i = 0; i < m; ++i)
                    {
                        tmpCol[i] = b.getEntry(i, k);
                        bp[i][k]  = 0;
                    }
                    for (int i = 0; i < m; ++i)
                    {
                        ArrayRealVector v     = eigenvectors[i];
                        double[]        vData = v.getDataRef();
                        double          s     = 0;
                        for (int j = 0; j < m; ++j)
                        {
                            s += v.getEntry(j) * tmpCol[j];
                        }
                        s /= realEigenvalues[i];
                        for (int j = 0; j < m; ++j)
                        {
                            bp[j][k] += s * vData[j];
                        }
                    }
                }

                return(new Array2DRowRealMatrix(bp, false));
            }