Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Matrix Multiplication using java

Fri May 04, 2007 12:14 am

The matrix mutliplication of any size to any size.We are using here thread of java for multiplication.

How to work :
User enter the number of rows and columns in matrix A and matrix B.
The number of columns in matrix A must equal the number of rows in matrix B.
Then,
The user enter the elements of matrix A.
The user enter the elements of matrix B.
Code:
/*
 * Main.java
 *
 * Created on November 6, 2006, 6:25 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */



import java.util.Scanner;

/**
 *
 
 */
public class Main {
    
    
/** Creates a new instance of Main */
    public Main() 
    
{
        Scanner cin=new Scanner(System.in);
        System.out.println("Enter the number of rows in matrix A : ");
        A_row=cin.nextInt();
        System.out.println("Enter the number of columns in matrix A : ");
        A_col=cin.nextInt();
        System.out.println("Enter the number of rows in matrix B : ");
        B_row= cin.nextInt();
        System.out.println("Enter the number of columns in matrix B : ");
        B_col=cin.nextInt();
        
        if
(A_col==B_row)
        {
            matrix_A=new int[A_row][A_col];
            matrix_B=new int[B_row][B_col];
            mult_ans=new int[A_row][B_col];
          System.out.println("Enter the elements of matrix A : ");
             for(int i=0;i<A_row;i++)
                 for(int j=0;j<A_col;j++)
                 {
                    matrix_A[i][j]=cin.nextInt();
                 }
          System.out.println("Enter the elements of matrix B : ");
            for(int i=0;i<B_row;i++)
                 for(int j=0;j<B_col;j++)
                 {
                    matrix_B[i][j]=cin.nextInt();  
                 
}
             thread_pool=new mythread[A_row];
             
            for
(int i=0;i<A_row;i++)
            {
                thread_pool[i]=new mythread(i);
                thread_pool[i].start();
            }
           System.out.println("The answer of the matrix A*B : "); 
             printer_thread
=new threadprint[3];

                printer_thread[2]=new threadprint(mult_ans);
                printer_thread[2].start();

}
        else
        
{
           System.out.print("!!! Can't multiply this matrixes !!! "); 
        
}
         
    
}
    
    
/**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

new Main();

// TODO code application logic here
    }

private class mythread extends Thread
    
{
        int index;
         
         mythread
(int index)
         {
             this.index=index;
         }
        public void run() 
        
{
            for(int i=0;i<B_col;i++)
            {
                for(int j=0;j<B_col;j++)
                {
                    mult_ans[index][i]+=matrix_A[index][j]*matrix_B[j][i];
                }
            }
        }
    }
    private class threadprint extends Thread
    
{
        threadprint (int[][] matrix)
        {
            this.matrix=matrix;
        }
        public synchronized  void run()
        {
            for(int i=0;i<matrix.length;i++)
            {
                System.out.print("| ");
                for(int j=0;j<matrix[0].length;j++)
                {
                  System.out.print(matrix[i][j]+"  ");
                }
                System.out.println(" |");
            }
        }
        int[][] matrix;
    }
            
    private int A_row
;
    private int A_col;
    private int B_row;
    private int B_col;
    private int[][] matrix_A;
    private int[][] matrix_B;
    private int[][] mult_ans;
    private mythread[] thread_pool;
    private threadprint[] printer_thread;
}
 



Attachments
Matrix_multiplication.rar
Matrix multiplay netBeans Project
(12.21 KiB) Downloaded 2717 times

Post a reply
  Related Posts  to : Matrix Multiplication using java
 Matrix multiplication     -  
 Matrix multiplication (Product) using C++     -  
 create Sparse Matrix in java     -  
 Perform Multiplication by Shift Left Operations     -  
 Matrix Operations     -  
 Sparse matrix using a class     -  
 How to do a array matrix program     -  
 print element in 2d matrix     -  
 2d game in java-Monster-Java 2D Game Graphics and Animation     -  
 need help in java     -  

Topic Tags

Java Arrays