Advertisement
Tsvetomir

Matrix class

Jan 13th, 2013
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Globalization;
  3. using System.Text;
  4.  
  5. namespace ClassMatrix
  6. {
  7.     internal class Matrix
  8.     {
  9.         public int[,] Content { private set; get; }
  10.        
  11.         /// <summary>
  12.         /// Creates new empty instanse of matrix class.
  13.         /// </summary>
  14.         public Matrix()
  15.         {
  16.             Content = new int[0, 0];
  17.         }
  18.  
  19.         /// <summary>
  20.         /// Creates new instanse of matrix class with specific rows and cols size.
  21.         /// </summary>
  22.         public Matrix(int rows, int cols)
  23.         {
  24.             if(rows < 0 || cols < 0)
  25.             {
  26.                 throw new Exception("Invalid dimentions size.");
  27.             }
  28.  
  29.             Content = new int[rows, cols];
  30.         }
  31.  
  32.         #region Static methods
  33.  
  34.         /// <summary>
  35.         /// Sum two matrix objects.
  36.         /// </summary>
  37.         public static Matrix operator +(Matrix matrix1, Matrix matrix2)
  38.         {
  39.             if(matrix1.RowCount != matrix2.RowCount
  40.                 || matrix1.ColsCount != matrix2.ColsCount)
  41.             {
  42.                 throw new Exception("Different matrixes sizes.");
  43.             }
  44.  
  45.             var resultMatrix = new Matrix(matrix1.RowCount, matrix1.ColsCount);
  46.             for (int i = 0; i < matrix1.RowCount; i++)
  47.             {
  48.                 for (int j = 0; j < matrix1.ColsCount; j++)
  49.                 {
  50.                     int value = matrix1.GetValue(i, j) + matrix2.GetValue(i, j);
  51.                     resultMatrix.SetValue(i, j, value);
  52.                 }
  53.             }
  54.  
  55.             return resultMatrix;
  56.         }
  57.  
  58.         /// <summary>
  59.         /// Substract two matrix objects.
  60.         /// </summary>
  61.         public static Matrix operator -(Matrix matrix1, Matrix matrix2)
  62.         {
  63.             if(matrix1.RowCount != matrix2.RowCount
  64.                 || matrix1.ColsCount != matrix2.ColsCount)
  65.             {
  66.                 throw new Exception("Different matrixes sizes.");
  67.             }
  68.  
  69.             var resultMatrix = new Matrix(matrix1.RowCount, matrix1.ColsCount);
  70.             for (int i = 0; i < matrix1.RowCount; i++)
  71.             {
  72.                 for (int j = 0; j < matrix1.ColsCount; j++)
  73.                 {
  74.                     int value = matrix1.GetValue(i, j) - matrix2.GetValue(i, j);
  75.                     resultMatrix.SetValue(i, j, value);
  76.                 }
  77.             }
  78.  
  79.             return resultMatrix;
  80.         }
  81.  
  82.         /// <summary>
  83.         /// Multiply two matrix objects.
  84.         /// </summary>
  85.         public static Matrix operator *(Matrix matrix1, Matrix matrix2)
  86.         {
  87.             if(matrix1.RowCount != matrix2.ColsCount
  88.                 || matrix1.ColsCount != matrix2.RowCount)
  89.             {
  90.                 throw new Exception("Unable to multiply matrixes when first matrix rows count differs than second matrix cols count.");
  91.             }
  92.  
  93.             var resultMatrix = new Matrix(matrix1.RowCount, matrix2.ColsCount);
  94.  
  95.             for (int i = 0; i < matrix1.RowCount; ++i)
  96.             {
  97.                 for (int j = 0; j < matrix2.ColsCount; ++j)
  98.                 {
  99.                     int sum = 0;
  100.                     for (int k = 0; k < matrix1.ColsCount; ++k)
  101.                     {
  102.                            
  103.                         sum += matrix1.GetValue(i, k) * matrix2.GetValue(k, j);
  104.                     }
  105.  
  106.                     resultMatrix.SetValue(i, j, sum);
  107.                 }
  108.             }
  109.  
  110.             return resultMatrix;
  111.         }
  112.  
  113.         #endregion
  114.  
  115.         #region Public methods
  116.  
  117.         public void SetContent(int[,] content)
  118.         {
  119.             if(content == null)
  120.             {
  121.                 throw new ArgumentNullException("content");
  122.             }
  123.  
  124.             if(content.GetLength(0) != this.RowCount
  125.                 || content.GetLength(1) != this.ColsCount)
  126.             {
  127.                 throw new Exception("Wring content dimentions size.");
  128.             }
  129.  
  130.             Content = content;
  131.         }
  132.  
  133.         /// <summary>
  134.         /// Set a value in a specific cell in the matrix
  135.         /// </summary>
  136.         public void SetValue(int row, int col, int value)
  137.         {
  138.             if(row < 0 || row >= RowCount)
  139.             {
  140.                 throw new ArgumentOutOfRangeException("row");
  141.             }
  142.  
  143.             if(col < 0 || col >= ColsCount)
  144.             {
  145.                 throw new ArgumentOutOfRangeException("col");
  146.             }
  147.  
  148.             Content[row, col] = value;
  149.         }
  150.  
  151.         /// <summary>
  152.         /// Set a value in a specific cell in the matrix
  153.         /// </summary>
  154.         public int GetValue(int row, int col)
  155.         {
  156.             if(row < 0 || row >= RowCount)
  157.             {
  158.                 throw new ArgumentOutOfRangeException("row");
  159.             }
  160.  
  161.             if(col < 0 || col >= ColsCount)
  162.             {
  163.                 throw new ArgumentOutOfRangeException("col");
  164.             }
  165.  
  166.             return Content[row, col];
  167.         }
  168.  
  169.         /// <summary>
  170.         /// Gets the number of rows in the matrix.
  171.         /// </summary>
  172.         public int RowCount
  173.         {
  174.             get { return Content.GetLength(0); }
  175.         }
  176.  
  177.         /// <summary>
  178.         /// Gets the number of cols in the matrix.
  179.         /// </summary>
  180.         public int ColsCount
  181.         {
  182.             get { return Content.GetLength(0); }
  183.         }
  184.  
  185.         /// <summary>
  186.         /// Adds a matrix object to the current matrix.
  187.         /// </summary>
  188.         public void Add(Matrix matrix)
  189.         {
  190.             try
  191.             {
  192.                 var newMatrix = this + matrix;
  193.                 this.Content = newMatrix.Content;
  194.             }
  195.             catch (Exception)
  196.             {
  197.                 throw;
  198.             }
  199.         }
  200.  
  201.         /// <summary>
  202.         /// Substract a matrix object from the current matrix.
  203.         /// </summary>
  204.         public void Substract(Matrix matrix)
  205.         {
  206.             try
  207.             {
  208.                 var newMatrix = this - matrix;
  209.                 this.Content = newMatrix.Content;
  210.             }
  211.             catch (Exception)
  212.             {
  213.                 throw;
  214.             }
  215.         }
  216.  
  217.         /// <summary>
  218.         /// Multiplying two matrix objects.
  219.         /// </summary>
  220.         public Matrix Multiply(Matrix matrix)
  221.         {
  222.             try
  223.             {
  224.                 var result = this * matrix;
  225.                 return result;
  226.             }
  227.             catch (Exception)
  228.             {
  229.                 throw;
  230.             }
  231.         }
  232.  
  233.         public override string ToString()
  234.         {
  235.             int maxLength = 0;
  236.             for (int i = 0; i < RowCount; i++)
  237.             {
  238.                 for (int j = 0; j < ColsCount; j++)
  239.                 {
  240.                     int currentLength = this.Content[i, j].ToString(CultureInfo.InvariantCulture).Length;
  241.                     if(currentLength > maxLength)
  242.                     {
  243.                         maxLength = currentLength;
  244.                     }
  245.                 }
  246.             }
  247.  
  248.             int padding = maxLength + 1;
  249.  
  250.             var stringBuilder = new StringBuilder();
  251.             for (int i = 0; i < RowCount; i++)
  252.             {
  253.                 stringBuilder.Append("{");
  254.                 for (int j = 0; j < ColsCount; j++)
  255.                 {
  256.                     string value = this.Content[i, j].ToString();
  257.                     if(j < ColsCount-1)
  258.                     {
  259.                         value += ",";
  260.                     }
  261.  
  262.                     stringBuilder.Append(value.PadLeft(padding));
  263.                 }
  264.  
  265.                 stringBuilder.Append("}");
  266.  
  267.                 if(i < RowCount-1)
  268.                 {
  269.                     stringBuilder.Append(",");
  270.                 }
  271.  
  272.                 stringBuilder.Append(Environment.NewLine);
  273.             }
  274.  
  275.             return stringBuilder.ToString();
  276.         }
  277.        
  278.         #endregion
  279.     }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement