/// <summary> /// Calculation inverse of matrix /// http://www.mathwords.com/i/inverse_of_a_matrix.htm using method 3 /// </summary> /// Coded by : ahmed osama ([email protected]) /// <summary> /// Parameters of entries: /// </summary> /// <param name="MAT">Array with numeration of elements[0..N-1, 0..N-1]</param> /// <param name="N">Dimension of matrix A</param> public double[,] InvMatrix(double[,] MAT, int N) { double[,] ResultMat = new double[N, N]; MatrixDeterminant MatDet = new MatrixDeterminant(); double DET = MatDet.MatrixDet(MAT, N); if (DET == 0) { throw new System.ArgumentException("Matrix is singular "); } MatrixCofactor MatCof = new MatrixCofactor(); ResultMat = MatCof.AdjointMatrix(MAT, N); for (int y = 0; y < N; y++) { for (int x = 0; x < N; x++) { ResultMat[x, y] = ResultMat[x, y] / DET; } } return(ResultMat); }
/// <summary> /// Calculation cofactor of matrix /// http://www.mathwords.com/c/cofactor_matrix.htm /// </summary> /// Coded by : ahmed osama ([email protected]) /// <summary> /// Parameters of entries: /// </summary> /// <param name="MAT">Array with numeration of elements[0..N-1, 0..N-1]</param> /// <param name="N">Dimension of matrix A</param> public double[,] CofacMatrix(double[,] MAT, int N) { double[,] tmp = new double[N - 1, N - 1]; double[,] ResultMat = new double[N, N]; for (int RY = 0; RY < N; RY++) { for (int RX = 0; RX < N; RX++) { int XX = 0; int YY = 0; for (int y = 0; y < N; y++) { for (int x = 0; x < N; x++) { if (x != RY & y != RX) { tmp[XX, YY] = MAT[x, y]; XX++; if (XX == N - 1) { XX = 0; YY++; } } } } MatrixDeterminant MatDet = new MatrixDeterminant(); ResultMat[RY, RX] = Math.Pow(-1, RX + RY + 2) * MatDet.MatrixDet(tmp, N - 1); } } return(ResultMat); }