Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.79 KB | None | 0 0
  1. namespace _06.MatrixClass
  2. {
  3.     class NotSameSizeException : System.Exception
  4.     {
  5.         public NotSameSizeException(string message)
  6.             : base(message)
  7.         {
  8.         }
  9.     }
  10.     class Matrix
  11.     {
  12.         private int[,] matrix;
  13.         public int row, col;
  14.         public Matrix(int[,] matrix)
  15.         {
  16.             this.matrix = matrix;
  17.             this.row = matrix.GetLength(0);
  18.             this.col = matrix.GetLength(1);
  19.         }
  20.         public Matrix(int row, int col)
  21.         {
  22.             this.row = row;
  23.             this.col = col;
  24.             this.matrix = new int[row, col];
  25.         }
  26.         public int this[int x, int y]
  27.         {
  28.             get { return this.matrix[x, y]; }
  29.             set { matrix[x, y] = value; }
  30.         }
  31.         public static Matrix operator +(Matrix m1, Matrix m2)
  32.         {
  33.  
  34.             Matrix m3 = new Matrix(m1.row, m1.col);
  35.  
  36.             if (m1.col != m2.col && m1.row != m2.row)
  37.             {
  38.                 throw new NotSameSizeException("Matrices must be with same dimesions");
  39.             }
  40.             else
  41.             {
  42.  
  43.                 for (int i = 0; i < m1.row; i++)
  44.                 {
  45.                     for (int j = 0; j < m1.col; j++)
  46.                     {
  47.                         m3[i, j] = m1[i, j] + m2[i, j];
  48.                     }
  49.                 }
  50.  
  51.             }
  52.             return m3;
  53.         }
  54.         public static Matrix operator -(Matrix m1, Matrix m2)
  55.         {
  56.             Matrix m3 = new Matrix(m1.row, m1.col);
  57.             if (m1.row != m2.row || m1.col != m2.col)
  58.             {
  59.                 throw new NotSameSizeException("Matrices must be with same dimesions");
  60.             }
  61.             else
  62.             {
  63.                 for (int i = 0; i < m1.row; i++)
  64.                 {
  65.                     for (int j = 0; j < m1.col; j++)
  66.                     {
  67.                         m3[i, j] = m1[i, j] - m2[i, j];
  68.                     }
  69.                 }
  70.             }
  71.             return m3;
  72.         }
  73.         public static Matrix operator *(Matrix m1, Matrix m2)
  74.         {
  75.             if (m1.col != m2.row)
  76.             {
  77.                 throw new NotSameSizeException("You cannot multiply " + m1.row + "x" + m1.col + " and " + m2.row + "x" + m2.col + " matrices");
  78.             }
  79.             Matrix m3 = new Matrix(m1.row, m2.col);
  80.             for (int i = 0; i < m1.row; i++)
  81.             {
  82.                 for (int j = 0; j < m2.col; j++)
  83.                 {
  84.                     int currentResult = 0;
  85.                     for (int m = 0; m < m2.row; m++)
  86.                     {
  87.                         currentResult += m2[m, j] * m1[i, m];
  88.                     }
  89.                     m3[i, j] = currentResult;
  90.                 }
  91.             }
  92.             return m3;
  93.         }
  94.         public override string ToString()
  95.         {
  96.             StringBuilder sb = new StringBuilder();
  97.             for (int i = 0; i < this.row; i++)
  98.             {
  99.                 for (int j = 0; j < col; j++)
  100.                 {
  101.                     sb.Append(this.matrix[i, j] + "  ");
  102.                 }
  103.                 sb.AppendLine();
  104.             }
  105.             return sb.ToString();
  106.         }
  107.     }
  108.     class Test
  109.     {
  110.         public static void Main()
  111.         {
  112.             int[,] mx1 = {
  113.                              {0,1,3},
  114.                              {3,4,5}
  115.                          };
  116.             int[,] mx2 = {
  117.                              {1,2},
  118.                              {3,4},
  119.                              {5,6}
  120.                          };
  121.             int[,] mx3 = new int[mx1.GetLength(0), mx2.GetLength(1)];
  122.  
  123.             Matrix m1 = new Matrix(mx1);
  124.             Matrix m2 = new Matrix(mx2);
  125.             Matrix m3 = m1 * m2;
  126.             Console.WriteLine(m3);
  127.  
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement