Пример #1
0
        //

        #endregion

        #region StaticMembers

        public static bool AreCommutative(Matrix matrix1, Matrix matrix2)
        {
            if (!(matrix1.IsSquare() && matrix2.IsSquare()))
            {
                throw new NonSquareMatrixException();
            }

            return(matrix1 * matrix2 == matrix2 * matrix1);
        }
Пример #2
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];
                }
            }
        }