matrices computations using operator overloading

The following example shows the computations of matrices using operator overloading:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication8
{

   class Matrix
    {
        int[,] matrix;

        public Matrix()
        {

        }
        public Matrix(int[,] matrix)
        {
            this.matrix = matrix;
        }

        public void ShowMatrix()
        {
            Console.WriteLine();
            for(int i=0; i<matrix.GetLength(0); i++)
            {
                for(int j=0; j<matrix.GetLength(1); j++)
                {
                    Console.Write(this.matrix[i, j] + " ");
                }
                Console.WriteLine();
            }
        }

        public static Matrix operator + (Matrix m1, Matrix m2)
        {
           
           Matrix ans = new Matrix(new int[2, 2]);

            for(int i=0; i<m1.matrix.GetLength(0); i++)
            {
                for(int j=0; j<m1.matrix.GetLength(1); j++)
                {
                    ans.matrix[i, j] = m1.matrix[i, j] + m2.matrix[i, j];
                }
            }

            return ans;
        }



        public static Matrix operator -(Matrix m1, Matrix m2)
        {

            Matrix ans = new Matrix(new int[2, 2]);

            for (int i = 0; i < m1.matrix.GetLength(0); i++)
            {
                for (int j = 0; j < m1.matrix.GetLength(1); j++)
                {
                    ans.matrix[i, j] = m1.matrix[i, j] - m2.matrix[i, j];
                }
            }

            return ans;
        }


        //public static Matrix Multiply(Matrix m1, Matrix m2)

        public static Matrix operator *(Matrix m1, Matrix m2)       
        {

            Matrix ans = new Matrix(new int[m1.matrix.GetLength(0), m2.matrix.GetLength(1)]);

            if(m1.matrix.GetLength(1) != m2.matrix.GetLength(0))
            {
                Console.WriteLine("Cannot multiply, incorrect dimension");
                return ans;
            }

            for (int r = 0; r < m1.matrix.GetLength(0); r++)
            {
                for (int c = 0; c < m2.matrix.GetLength(1); c++)
                {
                    ans.matrix[r, c] = 0;   // Very Important Step

                    for (int d = 0; d < m1.matrix.GetLength(0); d++)

                    {
                        ans.matrix[r, c] = ans.matrix[r, c] + m1.matrix[r, d] * m2.matrix[d, c];
                    }
                }
            }

            return ans;
        }

    }

    class Program
    {     

        static void Main(string[] args)
        {
            int[,] matrix1 =
                            {
                                {1, 2},
                                {3, 4}
                            };

            int[,] matrix2 =
                            {
                                {2, 1},
                                {1, 3}
                            };


            int[,] matrix3 =
                            {
                                {2, 1},
                                {1, 3}
                            };

            int[,] matrix4 = 
                              {
                                 {2,3,4, 5 }, 
                                 {4,5,2, 6 }
                                

                               };

            /*
             4    7
             10   15
             */

            Matrix m1 = new Matrix(matrix1);
            Matrix m2 = new Matrix(matrix2);
            Matrix m3 = new Matrix(matrix3);
            Matrix m4 = new Matrix(matrix4);

            Matrix ans = m1 * m2;
            
            ans.ShowMatrix();
            Console.WriteLine();
           
            
           //  Matrix ans = Matrix.Multiply(m1, m2);

                        

            ans = m1 + m2 - m3;
            ans.ShowMatrix();

            ans = m2 * m3;
            ans.ShowMatrix();
            
        }


        }

    }

 

Add a Comment

Your email address will not be published. Required fields are marked *