Write a Java Program to Multiply two Matrices by Passing Matrix to a Function

Multiplying matrices is one of the most common operations in linear algebra.

In this tutorial, we will be discussing how to multiply two matrices by passing them to a function in Java.


To begin with, let’s understand what a matrix is. A matrix is a rectangular array of numbers arranged in rows and columns.

The number of rows and columns in a matrix defines its dimensions.

For example, a matrix with 2 rows and 3 columns is called a 2 x 3 matrix.

Matrix multiplication is a binary operation that takes two matrices and produces another matrix as a result.

The resulting matrix has the number of rows of the first matrix and the number of columns of the second matrix.

To multiply two matrices, the number of columns in the first matrix should be equal to the number of rows in the second matrix.

Let’s move on to the implementation of matrix multiplication in Java.

We will create a function that takes two matrices as arguments and returns their product.

public class MatrixMultiplication {
    
    public static void main(String[] args) {
        int[][] matrix1 = {{1, 2}, {3, 4}};
        int[][] matrix2 = {{5, 6}, {7, 8}};
        
        int[][] product = multiplyMatrices(matrix1, matrix2);
        
        // Print the resulting matrix
        for (int[] row : product) {
            for (int num : row) {
                System.out.print(num + " ");
            }
            System.out.println();
        }
    }
    
    public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2) {
        int m1Rows = matrix1.length;
        int m1Columns = matrix1[0].length;
        int m2Rows = matrix2.length;
        int m2Columns = matrix2[0].length;
        
        if (m1Columns != m2Rows) {
            throw new IllegalArgumentException("Matrices cannot be multiplied: Incorrect dimensions");
        }
        
        int[][] product = new int[m1Rows][m2Columns];
        
        for (int i = 0; i < m1Rows; i++) {
            for (int j = 0; j < m2Columns; j++) {
                for (int k = 0; k < m1Columns; k++) {
                    product[i][j] += matrix1[i][k] * matrix2[k][j];
                }
            }
        }
        
        return product;
    }
}

In this implementation, we have created a function called multiplyMatrices that takes two matrices as arguments and returns their product.

The function first checks whether the dimensions of the matrices are correct for multiplication.

If the dimensions are correct, it creates a new matrix to store the product and uses nested loops to calculate the product.

In the main method, we have created two matrices, matrix1 and matrix2, and passed them to the multiplyMatrices function.

We then print the resulting matrix using nested loops.


In conclusion, matrix multiplication is a fundamental operation in linear algebra and can be easily implemented in Java.

By passing two matrices to a function, we can compute their product efficiently and effectively.