Exemplo n.º 1
0
        public Vector GetEigenvalues(out Vector[] eigenvectors, out int iterations, double eps)
        {
            eigenvectors = new Vector[this.n];
            Vector eigenvalues = null;

            iterations = 0;
            if (this.IsQuadratic)
            {
                Matrix currentMatrix      = new Matrix(this);
                Matrix eigenVectorsMatrix = Matrix.IndentityMatrix(this.n);

                while (!currentMatrix.IsDiagonal(eps))
                {
                    int i, j;
                    currentMatrix.GetMaxElement(out i, out j);
                    double p = 2 * currentMatrix[i, j];
                    double q = currentMatrix[i, i] - currentMatrix[j, j];
                    double d = Math.Sqrt(p * p + q * q);
                    double c = Math.Sqrt(2) / 2;
                    double s = Math.Sqrt(2) / 2;
                    if (Math.Abs(q) > eps)
                    {
                        double r = Math.Abs(q) / (2 * d);
                        c = Math.Sqrt(0.5 + r);
                        s = Math.Sqrt(0.5 - r) * Math.Sign(p * q);
                    }

                    currentMatrix = MultiplyJacobiLeftAndRightMatrices(currentMatrix, c, s, i, j);

                    eigenVectorsMatrix = MultiplyJacobiRightMatrix(eigenVectorsMatrix, c, s, i, j);
                    iterations++;
                }

                eigenvalues = new Vector(this.m);
                for (int i = 0; i < this.m; i++)
                {
                    eigenvalues[i]  = currentMatrix[i, i];
                    eigenvectors[i] = new Vector(this.n);
                    for (int j = 0; j < this.n; j++)
                    {
                        eigenvectors[i][j] = eigenVectorsMatrix[j, i];
                    }
                }
            }

            return(eigenvalues);
        }