sashomaga

Generic matrix override oprearators

Feb 22nd, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. //Define a class Matrix<T> to hold a matrix of numbers (e.g. integers, floats, decimals).
  6. //Implement an indexer this[row, col] to access the inner matrix cells.
  7. //Implement the operators + and - (addition and subtraction of matrices of the same size) and * for matrix multiplication.
  8. //Throw an exception when the operation cannot be performed. Implement the true operator (check for non-zero elements).
  9. class Matrix<T>
  10.     where T : IComparable, IFormattable, IConvertible, IComparable<T>, IEquatable<T>
  11. {
  12.     //fields
  13.     private T[,] matrix;
  14.     private readonly int rows;
  15.     private readonly int cols;
  16.     //properties
  17.     public dynamic Rows
  18.     {
  19.         get { return this.rows; }
  20.     }
  21.     public dynamic Cols
  22.     {
  23.         get { return this.cols; }
  24.     }
  25.     //constructor
  26.     public Matrix(int rows, int cols)
  27.     {
  28.         this.rows = rows;
  29.         this.cols = cols;
  30.         this.matrix = new T[cols, rows];
  31.     }
  32.     //index
  33.     public T this[int x, int y]
  34.     {
  35.         get
  36.         {
  37.             if (x < 0 || x >= this.rows || y < 0 || y >= this.cols)
  38.             {
  39.                 throw new IndexOutOfRangeException("index not exist");
  40.             }
  41.             return matrix[x, y];
  42.         }
  43.         set
  44.         {
  45.             matrix[x, y] = value;
  46.         }
  47.     }
  48.     //override operators
  49.     public static Matrix<T> operator +(Matrix<T> m1, Matrix<T> m2)
  50.     {
  51.         if (m1.rows != m2.rows || m1.cols != m2.cols)
  52.         {
  53.             throw new ArgumentException("matrix are not equal");
  54.         }
  55.         Matrix<T> result = new Matrix<T>(m1.rows,m1.cols);
  56.         for (int x = 0; x < m1.rows; x++)
  57.         {
  58.             for (int y = 0; y < m1.cols; y++)
  59.             {
  60.                 result[x, y] = (dynamic)m1[x, y] + m2[x, y];
  61.             }
  62.         }
  63.         return result;
  64.     }
  65.  
  66.     public static Matrix<T> operator -(Matrix<T> m1, Matrix<T> m2)
  67.     {
  68.         if (m1.rows != m2.rows || m1.cols != m2.cols)
  69.         {
  70.             throw new ArgumentException("matrix are not equal");
  71.         }
  72.         Matrix<T> result = new Matrix<T>(m1.rows, m1.cols);
  73.         for (int x = 0; x < m1.rows; x++)
  74.         {
  75.             for (int y = 0; y < m1.cols; y++)
  76.             {
  77.                 result[x, y] = (dynamic)m1[x, y] + m2[x, y];
  78.             }
  79.         }
  80.         return result;
  81.     }
  82.  
  83.     public static Matrix<T> operator *(Matrix<T> m1, Matrix<T> m2)
  84.     {
  85.         if (m1.rows != m2.cols || m1.cols != m2.rows)
  86.         {
  87.             throw new ArgumentException("matrix are not equal");
  88.         }
  89.         Matrix<T> result = new Matrix<T>(m1.rows, m2.cols);
  90.         for (int x = 0; x < result.rows; x++)
  91.         {
  92.             for (int y = 0; y < result.cols; y++)
  93.             {
  94.                 for (int k = 0; k < m1.cols; k++)
  95.                 {
  96.                     result[x, y] += (dynamic)m1[x, k] * m2[k, y];
  97.                 }
  98.                
  99.             }
  100.         }
  101.         return result;
  102.     }
  103.     //override ToString
  104.     public override string ToString()
  105.     {
  106.         StringBuilder builder = new StringBuilder();
  107.         for (int x = 0; x < this.rows; x++)
  108.         {
  109.             for (int y = 0; y < this.Cols; y++)
  110.             {
  111.                 builder.Append(matrix[x, y]).Append(' ');
  112.             }
  113.             builder.Append('\n');
  114.         }
  115.         return builder.ToString();
  116.     }
  117.     //true operator
  118.     public static bool operator true(Matrix<T> m)
  119.     {
  120.         for (int x = 0; x < m.rows; x++)
  121.         {
  122.             for (int y = 0; y < m.Cols; y++)
  123.             {
  124.                 if (m[x,y] == (dynamic)0)
  125.                 {
  126.                     return false;
  127.                 }
  128.             }
  129.            
  130.         }
  131.         return true;
  132.     }
  133.     //false operator
  134.     public static bool operator false(Matrix<T> m)
  135.     {
  136.         for (int x = 0; x < m.rows; x++)
  137.         {
  138.             for (int y = 0; y < m.Cols; y++)
  139.             {
  140.                 if (m[x, y] == (dynamic)0)
  141.                 {
  142.                     return true;
  143.                 }
  144.             }
  145.  
  146.         }
  147.         return false;
  148.     }
  149. }
  150. /***********************************************************************************/
  151. using System;
  152. using System.Collections.Generic;
  153. using System.Linq;
  154.  
  155. class Enter
  156. {
  157.     static void Main()
  158.     {
  159.         try
  160.         {
  161.             Matrix<int> m1 = new Matrix<int>(3, 3);
  162.             Matrix<int> m2 = new Matrix<int>(3, 3);
  163.  
  164.             Random randomGenerator = new Random();
  165.             for (int x = 0; x < m1.Rows; x++)
  166.             {
  167.                 for (int y = 0; y < m1.Cols; y++)
  168.                 {
  169.                     m1[x, y] = randomGenerator.Next(100);
  170.                     m2[x, y] = randomGenerator.Next(100);
  171.                 }
  172.             }
  173.  
  174.             Console.WriteLine("Matrix 1");
  175.             Console.WriteLine(m1);
  176.  
  177.             Console.WriteLine("Matrix 2");
  178.             Console.WriteLine(m2);
  179.  
  180.             Console.WriteLine("Matrix 1 + Matrix 2");
  181.             Console.WriteLine(m1 + m2);
  182.  
  183.             Console.WriteLine("Matrix 1 - Matrix 2");
  184.             Console.WriteLine(m1 - m2);
  185.  
  186.             Console.WriteLine("Matrix 1 * Matrix 2");
  187.             Console.WriteLine(m1 * m2);
  188.         }
  189.         catch (IndexOutOfRangeException err)
  190.         {
  191.             Console.WriteLine(err.Message);
  192.         }
  193.         catch (ArgumentException err)
  194.         {
  195.             Console.WriteLine(err.Message);
  196.         }
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment