Advertisement
Guest User

Untitled

a guest
Jan 9th, 2013
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.78 KB | None | 0 0
  1. using System;
  2.  
  3. namespace MatrixClass
  4. {
  5.     class Matrix
  6.     {
  7.  
  8.         public int matrixDemention = 3;
  9.         public int[,] matrix;
  10.  
  11.         public Matrix(int size)
  12.         {
  13.             this.matrixDemention = size;
  14.             this.matrix = new int[size, size];
  15.         }
  16.  
  17.         public int this[int x, int y]
  18.         {
  19.             get
  20.             {
  21.                 return matrix[x, y];
  22.             }
  23.             set
  24.             {
  25.                 matrix[x, y] = value;
  26.             }
  27.         }
  28.  
  29.  
  30.         public static Matrix operator +(Matrix a, Matrix b)
  31.         {
  32.             Matrix newMatrix = new Matrix(a.matrixDemention);
  33.  
  34.             for (int i = 0; i < a.matrixDemention; i++)
  35.             {
  36.                 for (int j = 0; j < a.matrixDemention; j++)
  37.                 {
  38.                     newMatrix[i, j] = a[i, j] + b[i, j];
  39.                 }
  40.             }
  41.  
  42.             return newMatrix;
  43.         }
  44.  
  45.  
  46.         public static Matrix operator -(Matrix a, Matrix b)
  47.         {
  48.             Matrix newMatrix = new Matrix(a.matrixDemention);
  49.  
  50.             for (int i = 0; i < a.matrixDemention; i++)
  51.             {
  52.                 for (int j = 0; j < a.matrixDemention; j++)
  53.                 {
  54.                     newMatrix[i, j] = a[i, j] - b[i, j];
  55.                 }
  56.             }
  57.  
  58.             return newMatrix;
  59.         }
  60.  
  61.         public static Matrix operator *(Matrix a, Matrix b)
  62.         {
  63.             Matrix newMatrix = new Matrix(a.matrixDemention);
  64.  
  65.             for (int i = 0; i < a.matrixDemention; i++)
  66.             {
  67.                 for (int j = 0; j < a.matrixDemention; j++)
  68.                 {
  69.                     newMatrix[i, j] = a[i, j] * b[j, i];
  70.                 }
  71.             }
  72.  
  73.             return newMatrix;
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement