Пример #1
0
        /**
         * Gets the block diagonal matrix D of the decomposition.
         * D is a block diagonal matrix.
         * Real eigenvalues are on the diagonal while complex values are on
         * 2x2 blocks { {real +imaginary}, {-imaginary, real} }.
         *
         * @return the D matrix.
         *
         * @see #getRealEigenvalues()
         * @see #getImagEigenvalues()
         */
        public RealMatrix getD()
        {
            if (cachedD == null)
            {
                // cache the matrix for subsequent calls
                cachedD = MatrixUtils.CreateRealDiagonalMatrix(realEigenvalues);

                for (int i = 0; i < imagEigenvalues.Length; i++)
                {
                    if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) > 0)
                    {
                        cachedD.setEntry(i, i + 1, imagEigenvalues[i]);
                    }
                    else if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) < 0)
                    {
                        cachedD.setEntry(i, i - 1, imagEigenvalues[i]);
                    }
                }
            }
            return(cachedD);
        }
Пример #2
0
        /**
         * Computes the square-root of the matrix.
         * This implementation assumes that the matrix is symmetric and positive
         * definite.
         *
         * @return the square-root of the matrix.
         * @throws MathUnsupportedOperationException if the matrix is not
         * symmetric or not positive definite.
         * @since 3.1
         */
        public RealMatrix getSquareRoot()
        {
            if (!isSymmetric)
            {
                throw new Exception("MathUnsupportedOperationException");
            }

            double[] sqrtEigenValues = new double[realEigenvalues.Length];
            for (int i = 0; i < realEigenvalues.Length; i++)
            {
                double eigen = realEigenvalues[i];
                if (eigen <= 0)
                {
                    throw new Exception("MathUnsupportedOperationException");
                }
                sqrtEigenValues[i] = Math.Sqrt(eigen);
            }
            RealMatrix sqrtEigen = MatrixUtils.CreateRealDiagonalMatrix(sqrtEigenValues);
            RealMatrix v         = getV();
            RealMatrix vT        = getVT();

            return(v.multiply(sqrtEigen).multiply(vT));
        }