public AbstrSquareMatrix InverseMatrix() { double[,] res = new double[N, N * 2]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { res[i, j] = Get(i, j); } res[i, i + N] = 1; } double[,] prev = new double[res.GetLength(0), res.GetLength(1)]; for (int i = 0; i < res.GetLength(0); i++) { Array.Copy(res, prev, res.Length); for (int j = 0; j < res.GetLength(0); j++) { for (int k = 0; k < res.GetLength(1); k++) { if (j == i) { res[j, k] /= prev[i, i]; } else { res[j, k] -= (prev[j, i] * prev[i, k]) / prev[i, i]; } } } } AbstrSquareMatrix inversed = Create(N); for (int i = 0; i < res.GetLength(0); i++) { for (int j = 0; j < res.GetLength(1) / 2; j++) { inversed.Get(i, j) = res[i, j + res.GetLength(1) / 2]; } } return(inversed); }
public static AbstrSquareMatrix operator *(AbstrSquareMatrix m1, AbstrSquareMatrix m2) { if (m1.N != m2.N) { throw new SizeException(m1.N, m2.N); } AbstrSquareMatrix matrixC = m1.Create(m1.N); for (var i = 0; i < m1.N; i++) { for (var j = 0; j < m2.N; j++) { matrixC.Get(i, j) = 0; for (var k = 0; k < m1.N; k++) { matrixC.Get(i, j) += m1.Get(i, k) * m2.Get(k, j); } } } return(matrixC); }
private static void TestOrdinary() { //arrays initializaton double[,] arr1 = { { 3.5, 5.5, 7.5, 9.5 }, { 1.5, 2.5, 4.5, 9.5 }, { 7.5, 2.5, 1.5, 6.5 }, { 8.5, 7.5, 3.5, 5.5 } }; double[,] arr2 = { { 7.5, 0.5, 3.5, 1.5 }, { 8.5, 1.5, 9.5, 2.5 }, { 7.5, 5.5, 6.5, 2.5 }, { 1.5, 9.5, 7.5, 3.5 } }; double[,] arr3 = { { 8.5, 2.5, 7.5 }, { 9.5, 1.5, 3.5 }, { 1.7, 7.9, 3.1 } }; double[,] arr4 = { { 2.5, 10.5, 9.5 }, { 7.2, 2.4, 5 }, { 6.5, 7.5, 2.5 } }; OrdinaryMatrix a = new OrdinaryMatrix(arr1); OrdinaryMatrix b = new OrdinaryMatrix(arr2); OrdinaryMatrix c = new OrdinaryMatrix(arr3); OrdinaryMatrix d = new OrdinaryMatrix(arr4); //arrays printing Console.WriteLine("A:\n" + a + "B:\n" + b + "C:\n" + c + "D:\n" + d); //printing of determinants Console.WriteLine("Determinant of A: " + a.Determinant() + "\nDeterminant of B: " + b.Determinant() + "\nDeterminant of C: " + c.Determinant() + "\nDeterminant of D: " + d.Determinant()); //matrices multiplication Console.Write("\nA*B:\n" + a * b); Console.WriteLine("\nC*D:\n" + c * d); //inversed matrices AbstrSquareMatrix inv = a.InverseMatrix(); Console.WriteLine("Inversed A:\n" + inv); Console.WriteLine("Inversed A * A:\n" + (inv * a)); inv = b.InverseMatrix(); Console.WriteLine("Inversed B:\n" + inv); Console.WriteLine("Inversed B * B:\n" + (inv * b)); inv = c.InverseMatrix(); Console.WriteLine("Inversed C:\n" + inv); Console.WriteLine("Inversed C * C:\n" + (inv * c)); inv = d.InverseMatrix(); Console.WriteLine("Inversed D:\n" + inv); Console.WriteLine("Inversed D * D:\n" + (inv * d)); //exceptions catchning Console.WriteLine("**Exceptions catching*********************"); Console.WriteLine("Trying to create non-square matrix:"); try { double[,] non_sq = { { 3.5, 5.5, 7.5 }, { 1.5, 2.5, 4.5 } }; OrdinaryMatrix bad = new OrdinaryMatrix(non_sq); } catch (AbstrSquareMatrix.SizeException ex) { Console.WriteLine("Non-square matrix!!!"); } Console.WriteLine("Trying to multipply matrices with different N:"); try { Console.WriteLine("\nA*C:\n" + a * c); } catch (AbstrSquareMatrix.SizeException ex) { Console.WriteLine("Sizes of matrices are different!!!"); } }