Matrix Multiplication in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

Matrix Multiplication is a core concept in Computer Science. We can perform matrix multiplication in Java using a simple nested for loop approach. This approach has a time complexity of O(n3n^3).

The time complexity of matrix multiplication can be improved using Strassen algorithm which has O(nlog7n^{log7}) time complexity.

Introduction

You might have learned about multiplying two matrices in Linear Algebra before. Say we have two matrices and we try to multiply these using an algorithm that you can devise given some time.

First, let’s brush up on the fundamentals before we tackle the problem itself. To calculate the product we need to ensure two facts or else matrix multiplication is not possible. Let us assume we have two matrices A and B of dimensions m×nm\times n and p×qp\times q respectively.

  1. The number of columns in the first matrix must be equal to the number of rows in the second matrix. This implies n=pn = p must hold true.
  2. The resultant product has dimensions: number of rows in first matrix * number of columns in the second matrix. Thus the resultant matrix has the following dimension: m×qm\times q.

Example

Multiplying A and B where the shape of A is 3 x 2 (meaning it has 3 rows and 2 columns) and the shape of B is 2 x 4 (meaning it has 2 rows and 4 columns) is possible because both the conditions mentioned above hold. If the shape of A was 3 x 3 and the shape of B was 2 x 4, the matrix multiplication would not be possible.

Let’s look at an example :

Matrix multiplication in java

Here A = [{3, 4}, {2, 1}], B = [{1, 5}, {3, 7}]. The product of A and B is C. The C(i, j) entry in matrix C can be calculated as the dot product of row i of A and column j of B.

Example: C(1,1) = 3 * 1 + 4 * 3 = 15.

Mathematically, we can express the formula as:

Cr,c=ABr,c=i=1nAr,iBi,c C_{r,c} = AB_{r,c} = \sum_{i=1}^{n} A_{r,i} * B_{i,c}

where n = number of columns in the first matrix.

Key takeaway: To calculate the C(i, j) entry, we need to multiply the ith row values of A with the corresponding jth column values of B and sum them over.

Multiplication using For loops

As we’ve mentioned before, the C(i,j) entry is the dot product of row i of A and column j of B.

C(1,1) = dotProduct( row(A, 1), col(B, 1) ) = 3×13\times1 + 4×34\times3 = 1515

C(1,2) = dotProduct( row(A, 1), col(B, 2) ) = 3×53\times5 + 4×74\times7 = 4343

C(2,1) = dotProduct( row(A, 2), col(B, 1) ) = 2×12\times1 + 1×31\times3 = 55

C(2,2) = dotProduct( row(A, 2), col(B, 2) ) = 2×52\times5 + 1×71\times7 = 1717

Let’s assume the shape of A is n1×n2n1\times n2 and the shape of B is n2×n3n2\times n3. Thus the shape of the resultant matrix would be n1×n3n1\times n3.

If we would want to compute C(i, j) programmatically, we would need to iterate over the row i of A and column j of B (this will take O(n2n2) steps because that is the number of columns in A). Since we need to calculate C(i,j) for all possible values of i and j, the total number of such computations would be n1×n3n1\times n3.

Hence, the total time complexity is O(n1 * n2 * n3). Assuming n1 = n2 = n3 = n, the total time complexity is of the order of O(n3n^3).

Martix multiplication in Java using For loop

Now the question is: can we do better than O(n3)O(n^{3})?

Sure, matrix multiplication time complexity can be reduced by using divide-and-conquer approach. It is called the Strassen Algorithm of matrix multiplication. The time complexity would be reduced to O(nlog7)O(n^{log7}) which is approximately O(n2.8)O(n^{2.8}).

Key takeaway: Matrix multiplication is a costly operation and naive matrix multiplication offers a time complexity of O(n3)O(n^{3}). However, we can improve the time complexity of matrix multiplication to O(nlog7)O(n^{log7}) by using Strassen Algorithm.

Example Program for Multiplication of Matrices in Java Using For Loop

Now that we’ve devised a way to visualize how to compute the entries in the product we can use the formula that we devised earlier to come up with a looped version of matrix multiplication. We use three loops:

  • one to iterate over the rows of A
  • second to iterate over the columns of B
  • third to actually compute the dot product of these vectors.

Code:

Output:

Explanation:

  • What we basically do in this program is first take the input dimensions of both matrices. If the number of columns in A is not equal to the number of rows in B we exit immediately.
  • Else we take the inputs for A and B and create the result matrix C. All the values in the matrix are initialised to zero.
  • Then, we iterate over the rows of A, columns of B and values of row/column to calculate matrix product respectively.

Conclusion

  • Matrix Multiplication is a fundamental concept in Computer Science.
  • To multiply two matrices A and B, they must satisfy the following basic constraint: Number of columns in A = Number of Rows in B.
  • The time complexity of matrix multiplication using simple for loop is O(n3n^3).
  • The time complexity of matrix multiplication can be improved using Strassen Algorithm which is a divide-and-conquer-algorithm.
  • Strassen Algorithm has O(nlog7n^{log7}) time complexity to compute the matrix multiplication.