Advertisement
Guest User

7a7a

a guest
May 22nd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp1
  8. {
  9. class Matrix
  10. {
  11. private int[,] mat;
  12. public Matrix(int[,] tmp)
  13. {
  14. mat = (int[,])tmp.Clone();
  15. /*int sizeX,sizeY,i,j;
  16. sizeX = tmp.GetUpperBound(0) + 1;
  17. sizeY = tmp.GetUpperBound(1) + 1;
  18. mat = new int[sizeX, sizeY];
  19. for (int i = 0; i < sizeX; i++)
  20. for (int j = 0; j < sizeY; j++)
  21. mat[i, j] = tmp[i, j];
  22. */
  23. }
  24. public Matrix(int n,int m)
  25. {
  26. mat = new int[n, m];
  27.  
  28. }
  29.  
  30. public static Matrix operator *(Matrix m1,Matrix m2)
  31. {
  32. if(m1.mat.GetUpperBound(1)!=m2.mat.GetUpperBound(0))
  33. {
  34. throw new Exception("size is wrong!!");
  35. }
  36. else
  37. {
  38. int row = m1.mat.GetUpperBound(0) + 1;
  39. int col = m1.mat.GetUpperBound(1) + 1;
  40. int col2 = m2.mat.GetUpperBound(1) + 1;
  41. int i, j, k, sum = 0;
  42. int[,] tmp = new int[row, col2];
  43. for(i=0;i<row;i++)
  44. {
  45. for (j = 0; j < col2; j++)
  46. {
  47. for (k = 0, sum = 0; k < col; k++)
  48. sum += m1.mat[i, k] * m2.mat[k, j];
  49. tmp[i, j] = sum;
  50. }
  51. }
  52. m1 = new Matrix(tmp);
  53.  
  54. }
  55. return m1;
  56. }
  57. public void Print()
  58. {
  59. int i, j;
  60. int row = mat.GetUpperBound(0) + 1;
  61. int col = mat.GetUpperBound(1) + 1;
  62. for (i = 0; i < row; i++)
  63. {
  64. for (j = 0; j < col; j++)
  65. Console.Write(mat[i, j].ToString() + " ");
  66. Console.WriteLine();
  67.  
  68. }
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement