Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By JosAH

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

  1. #1
    Denis2k11 is offline Member
    Join Date
    Oct 2012
    Posts
    35
    Rep Power
    0

    Question 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. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,782
    Rep Power
    5

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Denis2k11 is offline Member
    Join Date
    Oct 2012
    Posts
    35
    Rep Power
    0

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

    Quote Originally Posted by jim829 View Post
    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. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,782
    Rep Power
    5

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Denis2k11 is offline Member
    Join Date
    Oct 2012
    Posts
    35
    Rep Power
    0

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

    Thanks so much Jim! :)

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default 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
    jim829 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,782
    Rep Power
    5

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

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

    Quote Originally Posted by JosAH View Post
    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. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

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

    Quote Originally Posted by gimbal2 View Post
    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 ;-)
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,782
    Rep Power
    5

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. The <sup> tag doesn't work properly
    By JosAH in forum AWT / Swing
    Replies: 26
    Last Post: 02-18-2012, 07:55 AM
  2. Non-Square Matrix Multiplication
    By j_arif123 in forum New To Java
    Replies: 4
    Last Post: 01-22-2012, 06:32 PM
  3. remove duplicates in array gives wrong result
    By hacikho in forum New To Java
    Replies: 1
    Last Post: 11-25-2011, 03:31 AM
  4. Help in printing the result of the for loop
    By maas in forum Java Servlet
    Replies: 1
    Last Post: 05-12-2011, 09:30 AM
  5. Implement a multi-threaded matrix multiplication program.
    By redasu in forum Threads and Synchronization
    Replies: 0
    Last Post: 11-17-2010, 09:00 AM

Posting Permissions

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