Given two matrices, the task to multiply them. Matrices can either be square or rectangular.
Examples:
Input : mat1[][] = {{1, 2}, {3, 4}} mat2[][] = {{1, 1}, {1, 1}} Output : {{3, 3}, {7, 7}} Input : mat1[][] = {{2, 4}, {3, 4}} mat2[][] = {{1, 2}, {1, 3}} Output : {{6, 16}, {7, 18}}
Multiplication of Square Matrices :
The below program multiplies two square matrices of size 4*4, we can change N for different dimensions.
Java
// Java program to multiply two square // matrices. import java.io.*; class GFG { static int N = 4 ; // This function multiplies mat1[][] // and mat2[][], and stores the result // in res[][] static void multiply( int mat1[][], int mat2[][], int res[][]) { int i, j, k; for (i = 0 ; i < N; i++) { for (j = 0 ; j < N; j++) { res[i][j] = 0 ; for (k = 0 ; k < N; k++) res[i][j] += mat1[i][k] * mat2[k][j]; } } } // Driver code public static void main(String[] args) { int mat1[][] = { { 1 , 1 , 1 , 1 }, { 2 , 2 , 2 , 2 }, { 3 , 3 , 3 , 3 }, { 4 , 4 , 4 , 4 } }; int mat2[][] = { { 1 , 1 , 1 , 1 }, { 2 , 2 , 2 , 2 }, { 3 , 3 , 3 , 3 }, { 4 , 4 , 4 , 4 } }; // To store result int res[][] = new int [N][N]; int i, j; multiply(mat1, mat2, res); System.out.println( "Result matrix" + " is " ); for (i = 0 ; i < N; i++) { for (j = 0 ; j < N; j++) System.out.print(res[i][j] + " " ); System.out.println(); } } } // This code is contributed by anuj_67. |
Result matrix is 10 10 10 10 20 20 20 20 30 30 30 30 40 40 40 40
Time complexity: O(n3). It can be optimized using Strassen’s Matrix Multiplication
Auxiliary Space: O(n2)
Multiplication of Rectangular Matrices :
We use pointers in C to multiply to matrices. Please refer to the following post as a prerequisite of the code.
How to pass a 2D array as a parameter in C?
Java
// Java program to multiply two matrices. public class GFG { /** * to find out matrix multiplication * * @param matrix1 First matrix * @param rows1 Number of rows in matrix 1 * @param cols1 Number of columns in matrix 1 * @param matrix2 Second matrix * @param rows2 Number of rows in matrix 2 * @param cols2 Number of columns in matrix 2 * @return the result matrix (matrix 1 and matrix 2 * multiplication) */ public static int [][] matrixMultiplication( int [][] matrix1, int rows1, int cols1, int [][] matrix2, int rows2, int cols2) throws Exception { // Required condition for matrix multiplication if (cols1 != rows2) { throw new Exception( "Invalid matrix given." ); } // create a result matrix int resultMatrix[][] = new int [rows1][cols2]; // Core logic for 2 matrices multiplication for ( int i = 0 ; i < resultMatrix.length; i++) { for ( int j = 0 ; j < resultMatrix[i].length; j++) { for ( int k = 0 ; k < cols1; k++) { resultMatrix[i][j] += matrix1[i][k] * matrix2[k][j]; } } } return resultMatrix; } // Driver code public static void main(String[] args) throws Exception { // Initial matrix 1 and matrix 2 int matrix1[][] = { { 2 , 4 }, { 3 , 4 } }; int matrix2[][] = { { 1 , 2 }, { 1 , 3 } }; // Function call to get a matrix multiplication int resultMatrix[][] = matrixMultiplication( matrix1, 2 , 2 , matrix2, 2 , 2 ); // Display result matrix System.out.println( "Result Matrix is:" ); for ( int i = 0 ; i < resultMatrix.length; i++) { for ( int j = 0 ; j < resultMatrix[i].length; j++) { System.out.print(resultMatrix[i][j] + " " ); } System.out.println(); } } // This code is contributed by darshatandel1998 (Darshan // Tandel) } |
6 16 7 18
Time complexity: O(n3). It can be optimized using Strassen’s Matrix Multiplication
Auxiliary Space: O(m1 * n2)
Please refer complete article on Program to multiply two matrices for more details!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!