Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

matrix multiplication of (m,n) x (n,p)

807605Aug 18 2007 — edited Aug 18 2007
hi guys. i have a problem with matrix multiplication. the code i have here wworks fine with square matrices eg (2,2) x (2,2) but have problems when i wan to multiply matrices of different dimensions such as (3,2) x (2,2). below is the code. the contents of the two files that im trying to multiply is listed right at the end and im trying to multiply matrix1 with matrix. thanks guys
 
import java.io.*;
import java.util.Scanner;

class Matrix
{
	double[][] element;
}
public class Multiply
{
	public double[][] m1,m2,m3;
	public String fileName1,fileName2;

		public static void main(String[] args)
		{
			System.out.println("*******************");
			System.out.println("      Multiply     ");
			System.out.println("*******************");
			System.out.println();

			Multiply m = new Multiply();

			System.out.println("Enter the first file name:");
			m.fileName1 = m.fromKeyboard();
			m.m1 = m.readFromFile(m.fileName1);
			//start of 2nd file operation
			//enter second file name
			System.out.println("Enter the second file name:");
			m.fileName2 = m.fromKeyboard();
			m.m2 = m.readFromFile(m.fileName2);

			m.m3 = m.doMultiplication(m.m1,m.m2);
			char times = '*';
			m.printResult(m.m1, m.m2, m.m3, times);
			System.out.println("    ");
			System.out.println("Multiplication complete. Back to main menu");
			System.out.println("    ");
		}
		
		//method to read input entered by user for this case the name of the txt files
		//which contains the matrices
		public String fromKeyboard()
		{
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

			String input = " ";

			try
			{
				input = br.readLine();
			}
			catch(IOException io)
			{
				System.out.println(io);
			}

			return input;
		}


		//method read the matrices from txt files
		public double[][] readFromFile(String fileName1)
		{
			System.out.println("File name is "+fileName1);

			Matrix mat= null;

			try
			{
				File f1 = new File(fileName1);
				Scanner input = new Scanner(f1);

				String v1;

				v1=input.next();
				v1=input.next();
				v1=input.next();

				int row1 = input.nextInt();
				System.out.println("file has "+row1+" rows");
				v1=input.next();
				v1=input.next();
				int column1 = input.nextInt();
				System.out.println("file has "+column1+" columns");
				mat = new Matrix();
				mat.element = new double[row1][column1];

				for(int i=0;i<mat.element.length;i++)
				{
					for(int j=0; j<mat.element.length; j++)
{
mat.element[i][j] = input.nextInt();
//System.out.println("the element array is "+mat.element[i][j]);
}

}
}
catch(IOException io)
{
System.out.println(io);
//doMenu(1);

}
return mat.element;
}
//multiplication process
public double[][] doMultiplication(double[][] a1, double[][]a2)
{
//get wats the dimension of the matrices first
int row1,row2, column1, column2;
double[][] m3 = null;
double result = 0.0;

row1 = a1.length;
column1 = a1[0].length;
row2 = a2.length;
column2 = a2[0].length;
System.out.println("row is "+row1);
System.out.println("column is "+column2);

m3 = new double[row1][column2];

System.out.println("multiply length "+m3.length);

try
{


if (column1 != row2)
System.out.println("row and column sizes do not match");

//double [][] product = new double[a.rows][b.cols];

for (int i = 0; i < row1; ++i)
{
for (int j = 0; j < column2; ++j)
{
m3[i][j] = 0.0;
for (int k = 0; k < column1; ++k)
{
m3[i][j] += m1[i][k]*m2[k][j];
}
}
}


}
catch(ArrayIndexOutOfBoundsException ex){
System.out.println(ex);
}

return m3;
}

//print results on screen
public void printResult(double[][]m1, double[][]m2, double[][]m3, char operation)
{
for(int i = 0; i<m1.length; i++)
{
for(int j = 0; j<m1[i].length; j++)
System.out.print(" "+m1[i][j]);

if(i == m1.length/2)
System.out.print(" "+operation+" ");
else
System.out.print(" ");


for(int j=0; j<m2[i].length; j++)
System.out.print(" "+m2[i][j]);


if(i == m1.length/2)
System.out.print(" = ");
else
System.out.print(" ");

for(int j = 0; j<m3[i].length; j++)
System.out.print(" "+m3[i][j]);

System.out.println("");

}
}
}


<matrix1>
rows = 3
cols = 2

1 2
2 4
1 1
</matrix1>

<matrix>
rows = 2
cols = 2

1 2
2 4
</matrix>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 15 2007
Added on Aug 18 2007
8 comments
372 views