# Thread: Matrix multiplication program will not work properly. Printing result is wrong.

1. Member
Join Date
Oct 2012
Posts
35
Rep Power
0

## Matrix multiplication program will not work properly. Printing result is wrong.

Hi! I have here a java code for a matrix multiplication. The code runs fine but the third column displays zeros down through it which should be 83 and 34, using the matrices in the program. Can somebody pin-point where I am going wrong?? I have een trying for ages but I have no idea. Thank you!
Java Code:
```import javax.swing.JOptionPane;
import java.util.Scanner;
public class Test
{
public static void main(String[]args)
{
int[][]a = {{2,9},{7,3}};
int[][]b = {{3,4,1},{3,2,9}};
multiplyMatrix(a,b);
}
public static void multiplyMatrix(int[][] a, int[][] b)
{
int sum=0;
int[][] result = new int[a.length][b[0].length];
if(a[0].length == b.length){
for(int i=0; i < a[0].length; i++){
for(int j=0; j < b.length; j++){
for (int k = 0 ; k < b.length ; k++ )
{
sum += (a[i][k]*b[k][j]);
}
result[i][j] = sum;
sum = 0;
}
}
for(int i=0; i < result.length; i++){
for(int j=0; j < result[0].length; j++){
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
else
System.out.print("That is not a valid multiplication as the row length of Matrix A is not equal to the column length of Matrix B");
}
}```

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,839
Rep Power
10

## Re: Matrix multiplication program will not work properly. Printing result is wrong.

For one thing, your algorithm is wrong. b.length is 2 so you never get the third element of the b[x] array objects.

Regards,
Jim

3. Member
Join Date
Oct 2012
Posts
35
Rep Power
0

## Re: Matrix multiplication program will not work properly. Printing result is wrong.

Originally Posted by jim829
For one thing, your algorithm is wrong. b.length is 2 so you never get the third element of the b[x] array objects.

Regards,
Jim
And I fix this how??

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,839
Rep Power
10

## Re: Matrix multiplication program will not work properly. Printing result is wrong.

i goes to a.length
j goes to b[0].length
k goes to b.length

Regards,
Jim

5. Member
Join Date
Oct 2012
Posts
35
Rep Power
0

## Re: Matrix multiplication program will not work properly. Printing result is wrong.

Thanks so much Jim! :)

6. ## Re: Matrix multiplication program will not work properly. Printing result is wrong.

Matrix multiplication always makes me dizzy and I always mix up the row and column indexes; I prefer to unravel the problem into a series of dot product operations: C= A*B is a series of dot products: the dot product of a row of A and a column of B. A dot product with the i-th row of A and the j-th column of B ends up to be element i, j of the product matrix C. Creating a separate little method for that dot product also removes one nasty nested loop and moves it into that method:

Java Code:
```for (int i= 0; i < A.length; i++)
for (int j= 0; j < B[0].length; ++)
C[i][j]= dotproduct(A[i], B, j);```
kind regards,

Jos

7. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,839
Rep Power
10

## Re: Matrix multiplication program will not work properly. Printing result is wrong.

Yep. In fact, I did the same thing in my example (personal example). But I just posted the fix to comply with the OP's algorithm. Actually, I really prefer to break down stuff like this with smaller, private method calls. It really helps make things clearer and facilitates debugging.

Regards,
Jim

8. ## Re: Matrix multiplication program will not work properly. Printing result is wrong.

Indeed, and if performances becomes a bottle neck, one can always inline that dotproduct( ... ) method again (or even better: think of other solutions such as sparse matrices etc.)

kind regards,

Jos

9. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: Matrix multiplication program will not work properly. Printing result is wrong.

Originally Posted by JosAH
Indeed, and if performances becomes a bottle neck, one can always inline that dotproduct( ... ) method again
Pretty sure the JVM optimizer already does that for you. That's the magic of Java; you write the dumb, simple code and the optimizer does the rest. Optimization attempts on the Java platform should not be focused on the code but rather the optimizer: how can you write the code such that the optimizer can work with it? Otherwise you'll likely just keep butting heads with the JVM.

What I'm trying to say is: be careful with recommendations about manual micro optimizations, I probably don't have to tell you that it just doesn't work the same way it does in a C/C++ application.

10. ## Re: Matrix multiplication program will not work properly. Printing result is wrong.

Originally Posted by gimbal2
Pretty sure the JVM optimizer already does that for you. That's the magic of Java; you write the dumb, simple code and the optimizer does the rest. Optimization attempts on the Java platform should not be focused on the code but rather the optimizer: how can you write the code such that the optimizer can work with it? Otherwise you'll likely just keep butting heads with the JVM.

What I'm trying to say is: be careful with recommendations about manual micro optimizations, I probably don't have to tell you that it just doesn't work the same way it does in a C/C++ application.
All true; sometimes I fall for that trap, i.e. when there's nothing to refactor anymore (imho) I start micro-optimizing the code and therefore ruining it ;-) I did write "even better: think of other solutions ..." because performance (or accuracy) problems with these type of numerical problems are too dirty to handle with JIT optimization or simple refactoring ...

kind regards,

Jos

ps. the OP wants the product of two integer matrices so there won't be many accuracy problems here (except int overflow ;-)

11. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,839
Rep Power
10

## Re: Matrix multiplication program will not work properly. Printing result is wrong.

So let me understand something here. In C I recall you could have macros to aid inlining small bits of code. Are you saying that the Java optimizer will decide whether to take a method and inline it vs use a method call?

Regards,
Jim

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•