Page 1 of 1

3 Replies - 5157 Views - Last Post: 21 November 2009 - 12:55 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=140644&amp;s=2af0be205690dc552e6dc46115460415&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 butchi

Reputation: 0
• Posts: 3
• Joined: 21-November 09

Posted 21 November 2009 - 11:05 AM

Concurrent Matrix Multiplication
Purpose
Using threads to perform the matrix multiplication operation.
Description
Given 2 matrices M1 (n*m), M2 (m*k), its required to multiply M1, M2 and generate
the result R(n*k).
Let the cell at i , j in R = R[i,j]
To calculate R[I,j]:
R[i,j] =  =
=
k m
k 1 M1[i,k] * M2[k,j]
Where i = 1 : n
j = 1 : m
Example:
M1 = [ 1 2
1 4]
M2 = [ 3 1
2 5]
R[1,1] = M1[1,1]*M2[1,1] + M1[1,2] * M2[2,1] = 1*3 + 2*2 = 7
R[1,2] = M1[1,1]*M2[1,2] + M1[1,2] * M2[2,2] = 1*1 + 2*5 = 11
R[2,1] = M1[2,1] *M2[1,1] + M1[2,2]*M2[2,1]= 1*3+4*2 = 11
R[2,2] = M1[2,1]*M2[1,2] + M1[2,2]*M2[2,2] = 1*1 + 4*5 = 21
R = [7 11
11 21]
For example:
1. You can calculate each column in R using a separate thread OR
2. You can calculate each row in R using a separate thread OR
3. You can make a thread for each cell in R
Your program should print the resulted matrix R.

by java pleeeeeeeeese due tommorw
thanks

Is This A Good Question/Topic? 0

Replies To: mltiply 2 matrix by thread

#2 butchi

Reputation: 0
• Posts: 3
• Joined: 21-November 09

Re: mltiply 2 matrix by thread

Posted 21 November 2009 - 11:26 AM

this is code i don't know why while compile all the result = 0
pleeeeeeeeeas help me thnks

[code]

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_
matrix_B[i][j]=cin.nextInt();

This post has been edited by butchi: 21 November 2009 - 12:42 PM

#3 butchi

Reputation: 0
• Posts: 3
• Joined: 21-November 09

Re: mltiply 2 matrix by thread

Posted 21 November 2009 - 12:44 PM

matrix evry time the result = 0
```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();
}

for(int i=0;i<A_row;i++)
{
}
System.out.println("The answer of the matrix A*B : ");

}
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
}

{
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];
}
}
}
}
{
{
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;
}
```

This post has been edited by butchi: 21 November 2009 - 12:46 PM

#4 pbl

• There is nothing you can't do with a JTable

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

Re: mltiply 2 matrix by thread

Posted 21 November 2009 - 12:55 PM

Duplicated topics merged