Switch to full style
Project under GPL source codes are posted here
Post a reply

Matrix multiplication

Tue Mar 18, 2008 11:45 pm

* Project Name:   Matrix multiplication using java
* Programmer:   msi_333
* Type:   Math
* Technology:  Java
* IDE:   NetBeans
* Description:   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.

java code
package matrix_multiplication;

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
Project code
(12.32 KiB) Downloaded 1316 times

Re: Matrix multiplication

Mon Jan 21, 2013 1:19 am

updated.

Post a reply
  Related Posts  to : Matrix multiplication
 Matrix multiplication (Product) using C++     -  
 Matrix Multiplication using 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     -  
 create Sparse Matrix in java     -  

Topic Tags

Java Math, Java Arrays