[RESOLVED] Matrix Multiplication

• February 9th, 2010, 11:38 PM
Dexter-fan
[RESOLVED] Matrix Multiplication
I am trying to make a program that will take an n x n matrix(2-dimensional array of size n) and multiply it by another n x n matrix(2 dimensional array of the same size). I need to do this without using the predefined method for multiplying matrices. To do this for a 2 x 2 array would be simple however my confusion is caused by figuring out how to do the multiplication for all n x n matrices. Here is the code I have written so far.

import java.util.*;
class matrixX
{
public static void main (String args[])
{
int n=0;
Scanner scan = new Scanner(System.in);
System.out.println("The array will be n x n. What do you want n to be?");
n=scan.nextInt();
while (n<2)
{
System.out.println("Error: Value not greater than 2. Try Again: ");
n=scan.nextInt();
}
int[][] number1 = new int[n][n];
for (int i=0; i<number1.length; i++)
{
for (int j=0; j<number1[i].length; j++)
{
number1[i][j]= (int) ( 0 + Math.random() *10);
System.out.print(number1[i][j]+" ");
}
System.out.println(" ");
}
System.out.println();
int[][] number2 = new int[n][n];
for (int i=0; i<number2.length; i++)
{
for (int j=0; j<number2[i].length; j++)
{
number2[i][j]= (int) ( 0 + Math.random() *10);
System.out.print(number2[i][j]+" ");
}
System.out.println(" ");
}

}
}
• February 10th, 2010, 09:03 AM
ProgramThis
Re: Matrix Multiplication
Alright, you've written code to populate to nxn matricies with random values.

Quote:

To do this for a 2 x 2 array would be simple however my confusion is caused by figuring out how to do the multiplication for all n x n matrices.
The math doesn't change. The algorithm for performing multiplication on a 2x2 matrix and that of an 8x8 matrix are the same.

answer array[i][j] = array1[row][M] * array2[M][col]

Where M is the current position in the multiplication. Illustrated example:

Quote:

| M 2 | * | M 6 | => 1st iteration of M
| 3 4 | * | 7 8 |

| 1 M | * | 5 6 | => 2nd iteration of M
| 3 4 | * | M 8 |
M will always go from the first index to n, so in Java array talk from 0 to (n - 1).
• February 10th, 2010, 02:04 PM
Dexter-fan
Re: Matrix Multiplication
Okay, your last post helped me a lot in understanding this algorithm. My next question is how do I keep track of M. I get that it needs to be the location of the multiplication that is taking place. Also, what about the addition of the numbers after multiplication, when is that taken care of?
Take these 2 x 2 matrices for instance:
[ 4, 1 ] * [ 1, -2 ] = [ 4*1+1*4, 4*-2+1*-2] = [ 8, -10 ]
[ 5, 3 ] * [ 4, -2 ] = [ 5*1+3*4, 5*-2+3*-2] = [ 17, -16 ]

Note: This code does not currently compile due to the fact that I have yet to set up M as a variable.

import java.util.*;
import java.lang.Object;
class matrixX
{
public static void main (String args[])
{
int n=0;
Scanner scan = new Scanner(System.in);
System.out.println("The array will be n x n. What do you want n to be?");
n=scan.nextInt();
while (n<2)
{
System.out.println("Error: Value not greater than 2. Try Again: ");
n=scan.nextInt();
}
int[][] number1 = new int[n][n];
for (int i=0; i<number1.length; i++)
{
for (int j=0; j<number1[i].length; j++)
{
number1[i][j]= (int) ( 0 + Math.random() *10);
System.out.print(number1[i][j]+" ");
}
System.out.println(" ");
}
int x=number1.length;
System.out.println();
int[][] number2 = new int[n][n];
for (int i=0; i<number2.length; i++)
{
for (int j=0; j<number2[i].length; j++)
{
number2[i][j]= (int) ( 0 + Math.random() *10);
System.out.print(number2[i][j]+" ");
}
System.out.println(" ");
}
int y=number2.length;
{
{