static public matrix operator *(matrix m1, matrix m2) { if (m1.c != m2.r) { throw new WrongMatrixSizeException($"\nError: \nCan't multiply matricies.\nThe number of columns of the first matrix must be equal to the number of rows of the second matrix ({m1.c} != {m2.r})\n"); } double[][] z = new double[m1.x.Length][]; for (int i = 0; i < m1.r; i++) { z[i] = new double[m2.c]; for (int j = 0; j < m2.c; j++) { for (int k = 0; k < m1.c; k++) { z[i][j] += m1.x[i][k] * m2.x[k][j]; } } } matrix m = new matrix(z); return(m); }
static void Main(string[] args) { Console.WriteLine("Enter the size of first matrix:"); string[] n1 = Console.ReadLine().Trim().Split(); matrix m1 = new matrix(formMatrix(int.Parse(n1[0]), int.Parse(n1[1]))); Console.WriteLine("\nEnter the size of second matrix:"); string[] n2 = Console.ReadLine().Trim().Split(); matrix m2 = new matrix(formMatrix(int.Parse(n2[0]), int.Parse(n2[1]))); try { Console.WriteLine($"\nm1 + m2 = \n{m1 + m2}"); } catch (WrongMatrixSizeException ex) { Console.WriteLine(ex.Message); } try { Console.WriteLine($"m1 - m2 = \n{m1 - m2}"); } catch (WrongMatrixSizeException ex) { Console.WriteLine(ex.Message); } try { Console.WriteLine($"m1 * m2 = \n{m1 * m2}"); } catch (WrongMatrixSizeException ex) { Console.WriteLine(ex.Message); } }