In mathematics, a matrix is a rectangular array of numbers arranged in rows and columns. Matrix multiplication is a vital operation in various applications like graphics transformations, solving systems of linear equations, and in algorithms across scientific computing, engineering, and deep learning.
However, multiplying matrices isn’t as straightforward as multiplying individual elements. It involves a specific method where the row elements of the first matrix are multiplied by the column elements of the second matrix and then summed up to produce a new matrix. Let’s understand this better with a detailed problem statement and an example.
Problem Statement
In this tutorial, we aim to write a Java program that can multiply two matrices and display the resulting matrix. The program should be able to handle matrices of compatible dimensions where the number of columns in the first matrix matches the number of rows in the second matrix.
Example:
Let’s consider two matrices A and B.
Matrix A:
1 2
3 4
Matrix B:
2 0
1 2
To multiply matrix A by matrix B, we perform the following operations:
- The element at the first row, first column of the result matrix C is calculated as: (1*2 + 2*1) = 4
- The element at the first row, second column of C is calculated as: (1*0 + 2*2) = 4
- The element at the second row, first column of C is calculated as: (3*2 + 4*1) = 10
- The element at the second row, second column of C is calculated as: (3*0 + 4*2) = 8
So, Matrix C, the result of the multiplication, looks like:
4 4
10 8
Java Programs for Matrix Multiplication
Program 1: Static Input Matrix Multiplication
In our first program, we’ll define two matrices in the code itself and perform the multiplication.
class MatrixMultiplication {
public static void main(String[] args) {
int[][] matrixA = {{1, 2}, {3, 4}};
int[][] matrixB = {{2, 0}, {1, 2}};
int[][] resultMatrix = new int[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
resultMatrix[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
System.out.println("Result of Matrix Multiplication:");
for (int[] row : resultMatrix) {
for (int col : row) {
System.out.print(col + " ");
}
System.out.println();
}
}
}
Output
Result of Matrix Multiplication:
4 4
10 8
Explanation:
This program initializes two matrices matrixA
and matrixB
with predefined values. The resultMatrix
is calculated by iterating over rows of matrixA
and columns of matrixB
, multiplying corresponding elements and accumulating the sum. The nested loops ensure each element is computed correctly.
Program 2: Dynamic Input Matrix Multiplication
Next, let’s write a program that allows the user to input the elements of both matrices.
import java.util.Scanner;
public class MatrixMultiplicationUserInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the size of the first matrix (rows and columns): ");
int rowsA = scanner.nextInt();
int colsA = scanner.nextInt();
System.out.println("Enter the size of the second matrix (rows and columns): ");
int rowsB = scanner.nextInt();
int colsB = scanner.nextInt();
if (colsA != rowsB) {
System.out.println("Matrix multiplication is not possible. Number of columns in the first matrix must be equal to the number of rows in the second matrix.");
scanner.close();
return;
}
int[][] matrixA = new int[rowsA][colsA];
int[][] matrixB = new int[rowsB][colsB];
int[][] resultMatrix = new int[rowsA][colsB];
System.out.println("Enter elements for the first matrix:");
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsA; j++) {
matrixA[i][j] = scanner.nextInt();
}
}
System.out.println("Enter elements for the second matrix:");
for (int i = 0; i < rowsB; i++) {
for (int j = 0; j < colsB; j++) {
matrixB[i][j] = scanner.nextInt();
}
}
// Perform matrix multiplication
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
for (int k = 0; k < colsA; k++) {
resultMatrix[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
System.out.println("Result of Matrix Multiplication:");
for (int[] row : resultMatrix) {
for (int col : row) {
System.out.print(col + " ");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the size of the first matrix (rows and columns):
2
2
Enter the size of the second matrix (rows and columns):
2
2
Enter elements for the first matrix:
1
2
3
4
Enter elements for the second matrix:
1
2
3
4
Result of Matrix Multiplication:
7 10
15 22
Explanation:
In this program, the user provides the size and elements of the matrices via the console. The logic for multiplication remains the same as in the static input example, but here the flexibility allows for matrices of varying dimensions.