Пример #1
0
        /// <summary>
        /// Method is used to take inversed matrix.
        /// </summary>
        /// <param name="matrix">Initial matrix.</param>
        /// <returns>Inversed matrix.</returns>
        public static MatrixT <T> GetInverseMatrix(MatrixT <T> matrix)
        {
            double determinant = MatrixT <T> .GetMatrixDeterminant(matrix);

            if (determinant != 0)
            {
                MatrixT <T> reversedMatrix = new MatrixT <T>(matrix.Rows, matrix.Columns);

                for (int i = 0; i < matrix.Rows; i++)
                {
                    for (int j = 0; j < matrix.Columns; j++)
                    {
                        MatrixT <T> tempMatrix = MatrixT <T> .GetMinor(matrix, i, j);

                        reversedMatrix[i, j] = (dynamic)Math.Pow(-1.0, i + j + 2) * (dynamic)MatrixT <T> .GetMatrixDeterminant(tempMatrix) / (dynamic)determinant;
                    }
                }

                return(MatrixT <T> .TransponeMatrix(reversedMatrix));
            }

            return(null);
        }
Пример #2
0
        /// <summary>
        /// Method is used to take inversed matrix.
        /// </summary>
        /// <param name="matrix">Initial matrix.</param>
        /// <returns>Inversed matrix.</returns>
        public static MatrixT <T> GetInverseMatrix3(MatrixT <T> matrix)
        {
            if (matrix.Columns == matrix.Rows)
            {
                if (MatrixT <T> .GetMatrixDeterminant(matrix) != 0.0)
                {
                    MatrixT <T> matrixCopy = new MatrixT <T>(matrix.Rows, matrix.Columns);

                    for (int i = 0; i < matrix.Rows; i++)
                    {
                        for (int j = 0; j < matrix.Columns; j++)
                        {
                            matrixCopy[i, j] = matrix[i, j];
                        }
                    }

                    MatrixT <T> reverseMatrix = new MatrixT <T>(matrix.Rows, matrix.Columns);
                    MatrixT <T> .SetBaseMatrix(reverseMatrix);

                    for (int k = 0; k < matrix.Rows; k++)
                    {
                        T div = matrixCopy[k, k];
                        for (int m = 0; m < matrix.Columns; m++)
                        {
                            matrixCopy[k, m]    /= (dynamic)div;
                            reverseMatrix[k, m] /= (dynamic)div;
                        }

                        for (int i = k + 1; i < matrix.Rows; i++)
                        {
                            T multi = matrixCopy[i, k];
                            for (int j = 0; j < matrix.Columns; j++)
                            {
                                matrixCopy[i, j]    -= (dynamic)multi * (dynamic)matrixCopy[k, j];
                                reverseMatrix[i, j] -= (dynamic)multi * (dynamic)reverseMatrix[i, j];
                            }
                        }
                    }

                    for (int kk = matrix.Rows - 1; kk > 0; kk--)
                    {
                        matrixCopy[kk, matrix.Columns - 1]    /= (dynamic)matrixCopy[kk, kk];
                        reverseMatrix[kk, matrix.Columns - 1] /= (dynamic)matrixCopy[kk, kk];

                        for (int i = kk - 1; i + 1 > 0; i--)
                        {
                            T multi2 = matrixCopy[i, kk];
                            for (int j = 0; j < matrix.Columns; j++)
                            {
                                matrixCopy[i, j]    -= (dynamic)multi2 * (dynamic)matrixCopy[kk, j];
                                reverseMatrix[i, j] -= (dynamic)multi2 * (dynamic)reverseMatrix[kk, j];
                            }
                        }
                    }

                    return(MatrixT <T> .TransponeMatrix(reverseMatrix));
                }
            }

            return(null);
        }