Exemple #1
0
        static void Main(string[] args)
        {
            int rowsMatrixA = RequestNumber("number of rows for 1 matrix");
            int colsMatrixA = RequestNumber("number of columns for 1 matrix");
            int rowsMatrixB = colsMatrixA;
            int colsMatrixB = RequestNumber("number of columns for 2 matrix");

            MatrixMultiplier multiplier = new MatrixMultiplier();

            int[][] matrixA = MatrixUtils.CreateMatrix(rowsMatrixA, colsMatrixA, false);
            Console.WriteLine("First Matrix:");
            MatrixUtils.PrintMatrix(matrixA);
            int[][] matrixB = MatrixUtils.CreateMatrix(rowsMatrixB, colsMatrixB, false);
            Console.WriteLine("Second Matrix:");
            MatrixUtils.PrintMatrix(matrixB);
            try
            {
                int[][] result = multiplier.Multiply(matrixA, matrixB);
                Console.WriteLine("Matrix Product:");
                MatrixUtils.PrintMatrix(result);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.ReadKey();
        }
Exemple #2
0
        public int[][] Multiply(int[][] matrixA, int[][] matrixB)
        {
            int rowsA = matrixA.Length;
            int rowsB = matrixB.Length;
            int colsA = matrixA[0].Length;
            int colsB = matrixB[0].Length;

            if (colsA != rowsB)
            {
                throw new ArgumentException("Theese matrices can not be multiplied");
            }
            int[][] result = MatrixUtils.CreateMatrix(rowsA, colsB, true);
            Parallel.For(0, rowsA, i =>
            {
                Parallel.For(0, colsB, j =>
                {
                    int[] col    = matrixB.Select(row => row[j]).ToArray();
                    result[i][j] = VectorProduct(matrixA[i], col);
                });
            });
            return(result);
        }