Venciity

[ Telerik C#] MultidimArrays 06-MatrixClass

Feb 28th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1. using System;
  2.  
  3. // https://github.com/jasssonpet/TelerikAcademy/blob/master/Programming/3.ObjectOrientedProgramming/2.DefiningClassesPartTwo/8.GenericMatrix/Matrix.cs
  4. class Matrix
  5. {
  6.     public int Rows, Cols;
  7.     private int[,] matrix;
  8.  
  9.     // Constructor
  10.     public Matrix(int x, int y)
  11.     {
  12.         matrix = new int[x, y];
  13.         Rows = x;
  14.         Cols = y;
  15.     }
  16.  
  17.     // Getter/Setter
  18.     public int this[int x, int y]
  19.     {
  20.         get { return matrix[x, y]; }
  21.         set { matrix[x, y] = value; }
  22.     }
  23.  
  24.     // Addition
  25.     public static Matrix operator +(Matrix m1, Matrix m2)
  26.     {
  27.         Matrix m = new Matrix(m1.Rows, m1.Cols);
  28.  
  29.         for (int i = 0; i < m.Rows; i++)
  30.             for (int j = 0; j < m.Cols; j++)
  31.                 m[i, j] = m1[i, j] + m2[i, j];
  32.  
  33.         return m;
  34.     }
  35.  
  36.     // Subtraction
  37.     public static Matrix operator -(Matrix m1, Matrix m2)
  38.     {
  39.         Matrix m = new Matrix(m1.Rows, m1.Cols);
  40.  
  41.         for (int i = 0; i < m.Rows; i++)
  42.             for (int j = 0; j < m.Cols; j++)
  43.                 m[i, j] = m1[i, j] - m2[i, j];
  44.  
  45.         return m;
  46.     }
  47.  
  48.     // Naive multiplication
  49.     public static Matrix operator *(Matrix m1, Matrix m2)
  50.     {
  51.         Matrix m = new Matrix(m1.Rows, m2.Cols);
  52.  
  53.         for (int i = 0; i < m.Rows; i++)
  54.             for (int j = 0; j < m.Cols; j++)
  55.                 for (int k = 0; k < m1.Cols; k++)
  56.                     m[i, j] += m1[i, k] * m2[k, j];
  57.  
  58.         return m;
  59.     }
  60.  
  61.     // Print
  62.     public override string ToString()
  63.     {
  64.         int max = this.matrix[0, 0];
  65.         foreach (int cell in this.matrix) max = Math.Max(max, cell);
  66.         int cellSize = Convert.ToString(max).Length;
  67.  
  68.         string s = String.Empty;
  69.  
  70.         for (int i = 0; i < this.Rows; i++)
  71.             for (int j = 0; j < this.Cols; j++)
  72.                 s += (Convert.ToString(this.matrix[i, j]).PadRight(cellSize, ' ') + (j != this.Cols - 1 ? " " : "\n"));
  73.  
  74.         return s;
  75.     }
  76. }
  77.  
  78. class Program
  79. {
  80.     static void Main()
  81.     {
  82.         Matrix m1 = new Matrix(3, 3);
  83.         Matrix m2 = new Matrix(3, 3);
  84.  
  85.         // Fill with random numbers
  86.         Random randomGenerator = new Random();
  87.         for (int i = 0; i < m1.Rows; i++)
  88.         {
  89.             for (int j = 0; j < m1.Cols; j++)
  90.             {
  91.                 m1[i, j] = randomGenerator.Next(20);
  92.                 m2[i, j] = randomGenerator.Next(20);
  93.             }
  94.         }
  95.  
  96.         Console.WriteLine("Matrix 1");
  97.         Console.WriteLine(m1);
  98.  
  99.         Console.WriteLine("Matrix 2");
  100.         Console.WriteLine(m2);
  101.  
  102.         Console.WriteLine("Matrix 1 + Matrix 2");
  103.         Console.WriteLine(m1 + m2);
  104.  
  105.         Console.WriteLine("Matrix 1 - Matrix 2");
  106.         Console.WriteLine(m1 - m2);
  107.  
  108.         Console.WriteLine("Matrix 1 * Matrix 2");
  109.         Console.WriteLine(m1 * m2);
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment