Beispiel #1
0
        public static RectangleMatrix operator *(RectangleMatrix m1, RectangleMatrix m2)
        {
            int aRows = m1.Matrix.Length;
            int aCols = m1.Matrix[0].Length;
            int bRows = m2.Matrix.Length;
            int bCols = m2.Matrix[0].Length;

            if (aCols != bRows)
            {
                throw new Exception("Number of columns matrix A not equal to number of rows Matrix B." +
                                    " Multiplication is impossible");
            }

            var result = new RectangleMatrix(aRows, bCols);

            Parallel.For(0, aRows, i => {
                for (int j = 0; j < bCols; ++j)
                {
                    for (int k = 0; k < aCols; ++k)
                    {
                        result.Matrix[i][j] += m1.Matrix[i][k] * m2.Matrix[k][j];
                    }
                }
            });

            return(result);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            var matrix1 = new RectangleMatrix(5, 10);

            matrix1.FillMatrixWithRandomValues(0, 20);
            Console.WriteLine("Matrix A: ");
            matrix1.Print();
            var matrix2 = new RectangleMatrix(10, 5);

            matrix2.FillMatrixWithRandomValues(0, 20);
            Console.WriteLine("Matrix B: ");
            matrix2.Print();
            var matrix3 = matrix1 * matrix2;

            Console.WriteLine("Matrix C: ");
            matrix3.Print();
        }