コード例 #1
0
            /// <summary>
            /// Solves the linear equation A &times; X = B for symmetric matrices A.
            /// <para>
            /// This method only finds exact linear solutions, i.e. solutions for
            /// which ||A &times; X - B|| is exactly 0.
            /// </para>
            /// </summary>
            /// <param name="b">Right-hand side of the equation A &times; X = B.</param>
            /// <returns>Vector X that minimizes the two norm of A &times; X - B.</returns>
            /// <exception cref=DimensionMismatchException""> if the matrices dimensions do not
            /// match.</exception>
            /// <exception cref="SingularMatrixException"> if the decomposed matrix is singular.
            /// </exception>
            public RealVector solve(RealVector b)
            {
                if (!isNonSingular())
                {
                    throw new SingularMatrixException();
                }

                int m = realEigenvalues.Length;

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

                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));
            }
コード例 #2
0
            /// <inheritdoc/>
            public RealMatrix solve(RealMatrix b)
            {
                if (!isNonSingular())
                {
                    throw new SingularMatrixException();
                }

                int m = realEigenvalues.Length;

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

                int nColB = b.getColumnDimension();

                double[][] bp     = new double[m][];
                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));
            }