Facebook
From yuliia, 2 Weeks ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 141
  1. using System;
  2.  
  3. namespace MatrixLibrary
  4. {
  5.     public class MatrixException : Exception
  6.     {
  7.         public MatrixException() : base() { }
  8.  
  9.         public MatrixException(string message) : base(message) { }
  10.  
  11.         public MatrixException(string message, Exception innerException) : base(message, innerException) { }
  12.     }
  13.  
  14.     public class Matrix : ICloneable
  15.     {
  16.         private readonly int rows;
  17.         private readonly int columns;
  18.         private readonly double[,] array;
  19.  
  20.         public int Rows => rows;
  21.  
  22.         public int Columns => columns;
  23.  
  24.         public double[,] Array => array;
  25.  
  26.         public Matrix(int rows, int columns)
  27.         {
  28.             if (rows <= 0 || columns <= 0)
  29.                 throw new ArgumentOutOfRangeException(rows <= 0 ? nameof(rows) : nameof(columns), "Matrix dimensions must be greater than zero.");
  30.  
  31.             this.rows = rows;
  32.             this.columns = columns;
  33.             this.array = new double[rows, columns];
  34.         }
  35.  
  36.  
  37.         public Matrix(double[,] array)
  38.         {
  39.             if (array == null)
  40.                 throw new ArgumentNullException(nameof(array));
  41.  
  42.             this.rows = array.GetLength(0);
  43.             this.columns = array.GetLength(1);
  44.             this.array = array;
  45.         }
  46.  
  47.         public double this[int row, int column]
  48.         {
  49.             get
  50.             {
  51.                 if (row < 0 || row >= rows || column < 0 || column >= columns)
  52.                     throw new ArgumentException("Index is out of range.");
  53.  
  54.                 return array[row, column];
  55.             }
  56.             set
  57.             {
  58.                 if (row < 0 || row >= rows || column < 0 || column >= columns)
  59.                     throw new ArgumentException("Index is out of range.");
  60.  
  61.                 array[row, column] = value;
  62.             }
  63.         }
  64.  
  65.  
  66.         public object Clone()
  67.         {
  68.             return new Matrix((double[,])array.Clone());
  69.         }
  70.  
  71.         public static Matrix operator +(Matrix matrix1, Matrix matrix2)
  72.         {
  73.             if (matrix1 == null || matrix2 == null)
  74.                 throw new ArgumentNullException(matrix1 == null ? nameof(matrix1) : nameof(matrix2), "Matrix argument cannot be null.");
  75.  
  76.             if (matrix1.Rows != matrix2.Rows || matrix1.Columns != matrix2.Columns)
  77.                 throw new MatrixException("Matrices must have the same dimensions for addition.");
  78.  
  79.             double[,] resultArray = new double[matrix1.Rows, matrix1.Columns];
  80.             for (int i = 0; i < matrix1.Rows; i++)
  81.             {
  82.                 for (int j = 0; j < matrix1.Columns; j++)
  83.                 {
  84.                     resultArray[i, j] = matrix1[i, j] + matrix2[i, j];
  85.                 }
  86.             }
  87.  
  88.             return new Matrix(resultArray);
  89.         }
  90.  
  91.  
  92.         public static Matrix operator -(Matrix matrix1, Matrix matrix2)
  93.         {
  94.             if (matrix1 == null || matrix2 == null)
  95.                 throw new ArgumentNullException(matrix1 == null ? nameof(matrix1) : nameof(matrix2), "Matrix argument cannot be null.");
  96.  
  97.             if (matrix1.Rows != matrix2.Rows || matrix1.Columns != matrix2.Columns)
  98.                 throw new MatrixException("Matrices must have the same dimensions for subtraction.");
  99.  
  100.             double[,] resultArray = new double[matrix1.Rows, matrix1.Columns];
  101.             for (int i = 0; i < matrix1.Rows; i++)
  102.             {
  103.                 for (int j = 0; j < matrix1.Columns; j++)
  104.                 {
  105.                     resultArray[i, j] = matrix1[i, j] - matrix2[i, j];
  106.                 }
  107.             }
  108.  
  109.             return new Matrix(resultArray);
  110.         }
  111.  
  112.  
  113.         public static Matrix operator *(Matrix matrix1, Matrix matrix2)
  114.         {
  115.             if (matrix1 == null || matrix2 == null)
  116.                 throw new ArgumentNullException(matrix1 == null ? nameof(matrix1) : nameof(matrix2), "Matrix argument cannot be null.");
  117.  
  118.             if (matrix1.Columns != matrix2.Rows)
  119.                 throw new MatrixException("Number of columns in the first matrix must be equal to the number of rows in the second matrix for multiplication.");
  120.  
  121.             double[,] resultArray = new double[matrix1.Rows, matrix2.Columns];
  122.             for (int i = 0; i < matrix1.Rows; i++)
  123.             {
  124.                 for (int j = 0; j < matrix2.Columns; j++)
  125.                 {
  126.                     double sum = 0;
  127.                     for (int k = 0; k < matrix1.Columns; k++)
  128.                     {
  129.                         sum += matrix1[i, k] * matrix2[k, j];
  130.                     }
  131.                     resultArray[i, j] = sum;
  132.                 }
  133.             }
  134.  
  135.             return new Matrix(resultArray);
  136.         }
  137.  
  138.  
  139.         public Matrix Add(Matrix matrix)
  140.         {
  141.             if (matrix == null)
  142.                 throw new ArgumentNullException(nameof(matrix), "Matrix argument cannot be null.");
  143.  
  144.             return this + matrix;
  145.         }
  146.  
  147.  
  148.         public Matrix Subtract(Matrix matrix)
  149.         {
  150.             return this - matrix;
  151.         }
  152.  
  153.         public Matrix Multiply(Matrix matrix)
  154.         {
  155.             return this * matrix;
  156.         }
  157.  
  158.         public override bool Equals(object obj)
  159.         {
  160.             if (obj == null || GetType() != obj.GetType())
  161.                 return false;
  162.  
  163.             Matrix other = (Matrix)obj;
  164.             if (this.Rows != other.Rows || this.Columns != other.Columns)
  165.                 return false;
  166.  
  167.             for (int i = 0; i < this.Rows; i++)
  168.             {
  169.                 for (int j = 0; j < this.Columns; j++)
  170.                 {
  171.                     if (this[i, j] != other[i, j])
  172.                         return false;
  173.                 }
  174.             }
  175.  
  176.             return true;
  177.         }
  178.  
  179.         public override int GetHashCode()
  180.         {
  181.             return base.GetHashCode();
  182.         }
  183.     }
  184. }
  185.