Jump to content
  • Advertisement
Sign in to follow this  
MrPoopypants

Matrix Mulitplacation Method

This topic is 4812 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I wrote this matrix multiplacation method in Java. I'm pretty proud of it and I think it is an elegant little solution. I was just wondering if there is a more optimized version of it.
	public static int [] [] MatrixMultiply ( double [] [] A , double [] [] B ){
		
		int y0 = A[0].length;
		int x0 = A.length;
		int y1 = B[0].length;
		int x1 = B.length;
		
		if ( x0 == y1 ){
			int [] [] ProductMatrix = new int [ y0 ] [ x1 ];
			
			for ( int a = 0 ; a < y0 ; a++ ){
				for ( int b = 0 ; b < x1 ; b++ ){
					for ( int c = 0 ; c < y1 ; c++ ){
						ProductMatrix [a] += A[c][a] * B[c];
					}
				}
			}
			return ProductMatrix;
		}
		
		int [] [] InvalidReturn = { { -1 } };
		return InvalidReturn;
		
	}




Share this post


Link to post
Share on other sites
Advertisement
well if those matrices are fixed sizes you could unroll the loops (does java do this with constants?)

Share this post


Link to post
Share on other sites
the only oddity i see is storing the result in an integer matrix, but youre consistent about it so i guess its ok.

on the other hand java didnt do implict float->int casts last time i used it, so that code isnt going to compile.

Share this post


Link to post
Share on other sites
As java 1.5 now supports generics, perhaps something like expression templates is now possible on Java too. That would allow you to fuse the loops in more complex expressions (among other things). Altough i'm not sure if generics do provide as much freedom as do templates in C++. Here's a link to the original (?) text on ETs: Expression Templates. And here's another (there's java-stuff also on that site): Angelika Langer: ET.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Well, are you sure the base matrices(?) have the same size all over
(could be that A[0].length!=A[1].length) you got to test for that
(receiveing variables on the stack is "like a box of chocolates" you never know what you are going to get in)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!