コード例 #1
0
        public static void LU(Matrix matrix, out Matrix lowerMatrix, out Matrix upperMatrix)
        {
            if (!matrix.IsSquare())
            {
                throw new NonSquareMatrixException();
            }

            lowerMatrix = new Matrix(matrix.NumberOfRows, matrix.NumberOfColumns);

            lowerMatrix.SetColumn(0, matrix.GetColumn(0));

            upperMatrix = Matrix.Identity(matrix.NumberOfRows);

            for (int i = 0; i < matrix.NumberOfRows; i++)
            {
                for (int j = i + 1; j < matrix.NumberOfColumns; j++)
                {
                    for (int k = 0; k <= i - 1; k++)
                    {
                        upperMatrix[i, j] += lowerMatrix[i, k] * upperMatrix[k, j];
                    }

                    upperMatrix[i, j] = (matrix[i, j] - upperMatrix[i, j]) / lowerMatrix[i, i];
                }

                if (i == matrix.NumberOfColumns - 1)
                {
                    continue;
                }

                for (int j = i + 1; j < matrix.NumberOfColumns; j++)
                {
                    for (int k = 0; k <= i + 1 - 1; k++)
                    {
                        lowerMatrix[j, i + 1] += lowerMatrix[j, k] * upperMatrix[k, i + 1];
                    }

                    lowerMatrix[j, i + 1] = matrix[j, i + 1] - lowerMatrix[j, i + 1];
                }
            }
        }
コード例 #2
0
        public EigenvaluesEigenvectors GetEigenvaluesEigenvectors()
        {
            if (!IsSymmetric())
            {
                throw new NotImplementedException();
            }

            //Q=wT * this * w
            Matrix Q = this.Clone();

            Matrix eigenvectors = Identity(this.NumberOfColumns);;

            double threshold = Q.CalculateSumOfNonDiagonalElemets();

            do
            {
                DoSweep(ref Q, ref eigenvectors);

                threshold = Q.CalculateAbsoluteSumOfNonDiagonalElements();
            } while (threshold > 0.0000000001);

            //return W;
            double[] eigenvalues = new double[eigenvectors.NumberOfColumns];

            for (int i = 0; i < eigenvectors.NumberOfColumns; i++)
            {
                Matrix tempEigenvector = new Matrix(NumberOfRows, 1);

                tempEigenvector.SetColumn(0, eigenvectors.GetColumn(i));

                Matrix tempMultiplication = this * tempEigenvector;

                eigenvalues[i] = tempMultiplication[0, 0] / tempEigenvector[0, 0];
            }

            return(new EigenvaluesEigenvectors(eigenvalues, eigenvectors));
        }