Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- //Define a class Matrix<T> to hold a matrix of numbers (e.g. integers, floats, decimals).
- //Implement an indexer this[row, col] to access the inner matrix cells.
- //Implement the operators + and - (addition and subtraction of matrices of the same size) and * for matrix multiplication.
- //Throw an exception when the operation cannot be performed. Implement the true operator (check for non-zero elements).
- class Matrix<T>
- where T : IComparable, IFormattable, IConvertible, IComparable<T>, IEquatable<T>
- {
- //fields
- private T[,] matrix;
- private readonly int rows;
- private readonly int cols;
- //properties
- public dynamic Rows
- {
- get { return this.rows; }
- }
- public dynamic Cols
- {
- get { return this.cols; }
- }
- //constructor
- public Matrix(int rows, int cols)
- {
- this.rows = rows;
- this.cols = cols;
- this.matrix = new T[cols, rows];
- }
- //index
- public T this[int x, int y]
- {
- get
- {
- if (x < 0 || x >= this.rows || y < 0 || y >= this.cols)
- {
- throw new IndexOutOfRangeException("index not exist");
- }
- return matrix[x, y];
- }
- set
- {
- matrix[x, y] = value;
- }
- }
- //override operators
- public static Matrix<T> operator +(Matrix<T> m1, Matrix<T> m2)
- {
- if (m1.rows != m2.rows || m1.cols != m2.cols)
- {
- throw new ArgumentException("matrix are not equal");
- }
- Matrix<T> result = new Matrix<T>(m1.rows,m1.cols);
- for (int x = 0; x < m1.rows; x++)
- {
- for (int y = 0; y < m1.cols; y++)
- {
- result[x, y] = (dynamic)m1[x, y] + m2[x, y];
- }
- }
- return result;
- }
- public static Matrix<T> operator -(Matrix<T> m1, Matrix<T> m2)
- {
- if (m1.rows != m2.rows || m1.cols != m2.cols)
- {
- throw new ArgumentException("matrix are not equal");
- }
- Matrix<T> result = new Matrix<T>(m1.rows, m1.cols);
- for (int x = 0; x < m1.rows; x++)
- {
- for (int y = 0; y < m1.cols; y++)
- {
- result[x, y] = (dynamic)m1[x, y] + m2[x, y];
- }
- }
- return result;
- }
- public static Matrix<T> operator *(Matrix<T> m1, Matrix<T> m2)
- {
- if (m1.rows != m2.cols || m1.cols != m2.rows)
- {
- throw new ArgumentException("matrix are not equal");
- }
- Matrix<T> result = new Matrix<T>(m1.rows, m2.cols);
- for (int x = 0; x < result.rows; x++)
- {
- for (int y = 0; y < result.cols; y++)
- {
- for (int k = 0; k < m1.cols; k++)
- {
- result[x, y] += (dynamic)m1[x, k] * m2[k, y];
- }
- }
- }
- return result;
- }
- //override ToString
- public override string ToString()
- {
- StringBuilder builder = new StringBuilder();
- for (int x = 0; x < this.rows; x++)
- {
- for (int y = 0; y < this.Cols; y++)
- {
- builder.Append(matrix[x, y]).Append(' ');
- }
- builder.Append('\n');
- }
- return builder.ToString();
- }
- //true operator
- public static bool operator true(Matrix<T> m)
- {
- for (int x = 0; x < m.rows; x++)
- {
- for (int y = 0; y < m.Cols; y++)
- {
- if (m[x,y] == (dynamic)0)
- {
- return false;
- }
- }
- }
- return true;
- }
- //false operator
- public static bool operator false(Matrix<T> m)
- {
- for (int x = 0; x < m.rows; x++)
- {
- for (int y = 0; y < m.Cols; y++)
- {
- if (m[x, y] == (dynamic)0)
- {
- return true;
- }
- }
- }
- return false;
- }
- }
- /***********************************************************************************/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- class Enter
- {
- static void Main()
- {
- try
- {
- Matrix<int> m1 = new Matrix<int>(3, 3);
- Matrix<int> m2 = new Matrix<int>(3, 3);
- Random randomGenerator = new Random();
- for (int x = 0; x < m1.Rows; x++)
- {
- for (int y = 0; y < m1.Cols; y++)
- {
- m1[x, y] = randomGenerator.Next(100);
- m2[x, y] = randomGenerator.Next(100);
- }
- }
- Console.WriteLine("Matrix 1");
- Console.WriteLine(m1);
- Console.WriteLine("Matrix 2");
- Console.WriteLine(m2);
- Console.WriteLine("Matrix 1 + Matrix 2");
- Console.WriteLine(m1 + m2);
- Console.WriteLine("Matrix 1 - Matrix 2");
- Console.WriteLine(m1 - m2);
- Console.WriteLine("Matrix 1 * Matrix 2");
- Console.WriteLine(m1 * m2);
- }
- catch (IndexOutOfRangeException err)
- {
- Console.WriteLine(err.Message);
- }
- catch (ArgumentException err)
- {
- Console.WriteLine(err.Message);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment