Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Monday, October 26, 2020

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

1. Overview

In this tutorial, you'll learn how to write the function for matrix multiplication. Let us pass the input and result matrices to this method and will run the core logic to multiply. Finally, the output is stored in the result array.

In the previous article, shown how to multiply two matrices without using function.

Example:

Matrix 1 order: 2 X 3 

Matrix 2 order: 3 X 2

Result matrix order: 2 X 2

key note in the matrix multiplication is always matrix 1 columns size and matrix two row size must be equal. Otherwise matrix multiplication is not possible.

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


2. Matrix Multiplication Traversal

First, let us take a generic example with the 2 X 2 size two matrices as below.

Matrix M1 : [A11, A12        Matrix M2 : [B11, B12

                      A21, A22]                            B21, B22]


Result Matrix R : [ A11*B11 +  A12*B21  A11*B12 + A12*B22

                               A21*B11 +  A22*B21  A21*B12 + A22*B22]

This is a simple approach to calculate the matrix multiplication.

3. Example Program To Multiply Two Matrices Using Function

Below code has two separate methods. 

The first one doMatricMultiplication() takes the input and output matrix arrays. Runs the core logic inside this method and updates the output array.

Second method doPrintResultMatric() takes the array as input and prints it values.

These two methods can be called from many other classes hence lead to code reuse. This is a good practice to write the code in a separate method.

package com.javaprogramto.programs.arrays.matrix;

public class MatrixMultiplicationFunction {

	public static void main(String[] args) {
		// creating the first matrix using arrays
		int[][] matrix1 = { { 1, 2, 3 }, { 4, 5, 6 } };

		// creating the second matrix using two dimension array
		int[][] matrix2 = { { 1, 1 }, { 2, 2 }, { 3, 3 } };

		// Matrix 1 rows and columns length
		int rows1 = matrix1.length;
		int columns1 = matrix1[0].length;

		// Matrix 2 rows and columns length
		int rows2 = matrix2.length;
		int columns2 = matrix2[1].length;

		// output array for storing the multiplication result
		int[][] result = new int[rows1][columns2];

		// Calling a function for matrix multiplication core logic
		doMatricMultiplication(matrix1, matrix2, rows1, columns1, columns2, result);

		// printing the result
		//
		doPrintResultMatric(result);

	}

	/**
	 * Calculates the matrix multiplication for given inputs arrays.
	 * 
	 * @param matrix1
	 * @param matrix2
	 * @param rows1
	 * @param columns1
	 * @param columns2
	 * @param result
	 */
	private static void doMatricMultiplication(int[][] matrix1, int[][] matrix2, int rows1, int columns1, int columns2,
			int[][] result) {

		// matrix addition core logic
		for (int i = 0; i < rows1; i++) {
			for (int j = 0; j < columns2; j++) {
				for (int k = 0; k < columns1; k++) {
					result[i][j] += matrix1[i][k] * matrix2[k][j];
				}
			}
		}

	}

	// prints the given matrix
	private static void doPrintResultMatric(int[][] result) {
		for (int i = 0; i < result.length; i++) {
			for (int j = 0; j < result[1].length; j++) {
				System.out.print(result[i][j] + " ");
			}
			System.out.println();
		}
	}

}
 

Output:

14 14 
32 32 
 

4. Conclusion

In this article, you've seen how to write a separate function to multiply two matrices.

As usual, shown example code is over GitHub

Ref

No comments:

Post a Comment

Please do not add any spam links in the comments section.