public static GenericMatrix <T> operator +(GenericMatrix <T> firstMatrix, GenericMatrix <T> secondMatrix)  // Problem 10
        {
            if (firstMatrix.Rows != secondMatrix.Rows || firstMatrix.Cols != secondMatrix.Cols)
            {
                throw new ArgumentException("Matrices must have same size.");
            }

            GenericMatrix <T> result = new GenericMatrix <T>(firstMatrix.rows, firstMatrix.cols);

            for (int row = 0; row < firstMatrix.Rows; row++)
            {
                for (int col = 0; col < firstMatrix.Cols; col++)
                {
                    result[row, col] = (dynamic)firstMatrix[row, col] + (dynamic)secondMatrix[row, col];
                }
            }

            return(result);
        }
        public static GenericMatrix <T> operator *(GenericMatrix <T> firstMatrix, GenericMatrix <T> secondMatrix)
        {
            if (firstMatrix.Cols != secondMatrix.Rows)
            {
                throw new ArgumentException("First matrix cols size and second matrix rows must be the same!");
            }

            GenericMatrix <T> result = new GenericMatrix <T>(firstMatrix.Rows, secondMatrix.Cols);

            for (int row = 0; row < firstMatrix.Rows; row++)
            {
                for (int col = 0; col < secondMatrix.Cols; col++)
                {
                    for (int i = 0; i < firstMatrix.Cols; i++)
                    {
                        result[row, col] += (dynamic)firstMatrix[row, i] * secondMatrix[i, col];
                    }
                }
            }

            return(result);
        }
Example #3
0
        public static void Main()
        {
            GenericMatrix <int> firstMatrix = new GenericMatrix <int>(3, 2);

            firstMatrix[0, 0] = 1;       // Filling the matrix
            firstMatrix[0, 1] = 2;
            firstMatrix[1, 0] = 3;
            firstMatrix[1, 1] = 4;
            firstMatrix[2, 0] = 5;
            firstMatrix[2, 1] = 0;

            Console.WriteLine("first matrix with zero element:\n");
            Console.WriteLine(firstMatrix);
            Console.Write("The matrix does not contain zero element: ");    // Testing true / false

            if (firstMatrix)
            {
                Console.WriteLine(true);
            }
            else
            {
                Console.WriteLine(false);
            }

            firstMatrix[0, 0] = 1;       // Filling the matrix
            firstMatrix[0, 1] = 2;
            firstMatrix[1, 0] = 3;
            firstMatrix[1, 1] = 4;
            firstMatrix[2, 0] = 5;
            firstMatrix[2, 1] = 6;

            Console.WriteLine("\nfirst matrix without zero element:\n");
            Console.WriteLine(firstMatrix);
            Console.Write("The matrix does not contain zero element: ");

            if (firstMatrix)
            {
                Console.WriteLine(true);
            }
            else
            {
                Console.WriteLine(false);
            }

            GenericMatrix <int> secondMatrix = new GenericMatrix <int>(3, 2);

            secondMatrix[0, 0] = 10;
            secondMatrix[0, 1] = 10;
            secondMatrix[1, 0] = 10;
            secondMatrix[1, 1] = 10;
            secondMatrix[2, 0] = 10;
            secondMatrix[2, 1] = 10;

            Console.WriteLine("\nsecond matrix:\n");
            Console.WriteLine(secondMatrix);

            Console.WriteLine("first matrix + second matrix:\n");
            Console.WriteLine(firstMatrix + secondMatrix);        // Matrices addition

            Console.WriteLine("first matrix - second matrix:\n");
            Console.WriteLine(firstMatrix - secondMatrix);        // Matrices subtraction

            GenericMatrix <int> thirdMatrix = new GenericMatrix <int>(2, 4);

            thirdMatrix[0, 0] = 10;
            thirdMatrix[0, 1] = 10;
            thirdMatrix[0, 2] = 10;
            thirdMatrix[0, 3] = 10;
            thirdMatrix[1, 0] = 10;
            thirdMatrix[1, 1] = 10;
            thirdMatrix[1, 2] = 10;
            thirdMatrix[1, 3] = 10;

            Console.WriteLine("third matrix:\n");
            Console.WriteLine(thirdMatrix);

            Console.WriteLine("second matrix * third matrix:\n");
            Console.WriteLine(secondMatrix * thirdMatrix);        // Matrices multiplication
        }