Total members 11894 |It is currently Sat Nov 23, 2024 11:50 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka






* 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:
File comment: Project code
Matrix_multiplication.rar [12.32 KiB]
Downloaded 1346 times

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada
Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

updated.


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  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







Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com