Powered by Blogger.

matrix multiplication using arrays in java

>> Tuesday, August 12, 2014

Matrix Multiplication using arrays is very basic practice to learn for beginners to understand the concept of multidimensional matrix.Before to this you can check different types of arrays in java and get to know how to declare and define the arrays and also get practice with adding two matrices. Let me explain the matrix multiplication to understand in mathematical way and then we do the programmatically in java. Here we have some rules to follow for matrix multiplication.
matrix multiplication using arrays in java_JavabynataraJ
 Principles to follow matrix multiplication:

  1. The number of columns of the 1st matrix must equal the number of rows of the 2nd matrix.
  2. And the result will have the same number of rows as the 1st matrix, and the same number of columns as the 2nd matrix.
  3. In General To multiply an m×n matrix by an pxq matrix, the n and p must be the same, and the result is an m×q matrix.
 Let me take a matrixA{1 2 3 4 5 6}with (2x3)2 rows and 3 columns and matrixB{7 8 9 10 11 12}with (3x2) size. Now multiply(dot product) the both matrices.

 (1, 2, 3) * (7, 9, 11) = 1×7 + 2×9 + 3×11 = 58
matrix multiplication using arrays in java_JavabynataraJ
In the same way first row and second column:
(1, 2, 3) * (8, 10, 12) = 1×8 + 2×10 + 3×12 = 64
matrix multiplication using arrays in java_JavabynataraJ

and We can do the same thing for the 2nd row and 1st column:
(4, 5, 6) • (7, 9, 11) = 4×7 + 5×9 + 6×11 = 139
And for the 2nd row and 2nd column:
(4, 5, 6) • (8, 10, 12) = 4×8 + 5×10 + 6×12 = 154
In the programmatic way it is different to multiply matrices using arrays.
First declare 'matrixA' and matrixB with the size of mxn and pxq
int[][] matrixA = new int[m][n];
int[][] matrixB = new int[p][q];
To read the two matrices values through keyboard at runtime. For this we can read the input using BufferedReader and InputStreamReader.Also we have advanced classes like java.util.Scanner class introduced in java5 and java.io.Console class introduced in java 6 to read the data from the console. These are different ways to read input from the keyboard at run-time.

Comming to logic , the first matrix columns size and  second matrix row size should be same to do a matrix multiplication.

The main point is here to multiply each matrixA row to each column with matrixB.And then agian with second row of first matrixA to total column of matrixB. This is as given below
for(int i=0;i<m;i++){ //matrix multiplication
 for(int j=0;j<q;j++){
  for(int k=0; k<n; k++) {
   matrixC[i][j] += matrixA[i][k] * matrixB[k][j];
  }
  System.out.print(matrixC[i][j]+" ");
 }System.out.println();
}
Let's start our matrix multiplication program using arrays:
package com.javabynataraj;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
// http://javabynataraj.blogspot.com

public class MatrixMultiplication {
     public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     System.out.println("Enter the size of the matrixA rows(m) & columns(n) :");
     int m = Integer.parseInt(br.readLine()); //matrixA row size 'm'
     int n = Integer.parseInt(br.readLine()); //matrixA column size 'n'
     System.out.println("Enter the size of the matrixB rows(p) & columns(q) :");
     int p = Integer.parseInt(br.readLine()); //matrixB row size 'p'
     int q = Integer.parseInt(br.readLine()); //matrixB column size 'q'
  
     int[][] matrixA = new int[m][n];
     int[][] matrixB = new int[p][q];
  
     if(n!=p){
          throw new IllegalArgumentException(
   "matrixA columns(n) and matrixB rows(p) should be equal: " + n + " != " + p);
     }
     int[][] matrixC = new int[m][q]; //create resultant matrixC with size mxq
     System.out.println("Enter matrixA values: (press enter-key for each value)");
     for(int i=0;i<m;i++){ //Read the values for matrixA
         for(int j=0;j<n;j++){
             matrixA[i][j] = Integer.parseInt(br.readLine());
         }
     }
  
     System.out.println("Enter matrixB values: (press enter-key for each value)");
     for(int i=0;i<p;i++){ //Read the values for matrixB
        for(int j=0;j<q;j++){
           matrixB[i][j] = Integer.parseInt(br.readLine());
        }
     }
  
     System.out.println("Entered matrixA["+m+"x"+n+"] is: ");
     System.out.println("---------------------------");
         for(int i=0;i<m;i++){ //display matrixA
             for(int j=0;j<n;j++){
                 System.out.print(matrixA[i][j]+" ");
             }System.out.println();
        }
  
     System.out.println("Entered matrixB["+p+"x"+q+"] is: ");
     System.out.println("---------------------------");
         for(int i=0;i<p;i++){ //display matrixB
            for(int j=0;j<q;j++){
              System.out.print(matrixB[i][j]+" ");
            }System.out.println();
         }
  
     System.out.println("Resultant matrixC["+m+"x"+q+"] is: ");
     System.out.println("---------------------------");
         for(int i=0;i<m;i++){  //matrix multiplication
           for(int j=0;j<q;j++){
             for(int k=0; k<n; k++) {
                 matrixC[i][j] += matrixA[i][k] * matrixB[k][j];
             }System.out.print(matrixC[i][j]+" ");
          }System.out.println();
     } 
   }
}
Output:
matrix multiplication using arrays in java_JavabynataraJ
If the size of the matrix is (n!=p) condition fails then we get an exception as given below:
matrix multiplication using arrays in java_JavabynataraJ

Please let me know if you found any issues to run this program in your console and drop the comments.
Reference Books: 

Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2022. All rights reserved.