Matrix Mulitplacation Method

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

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 on other sites
well if those matrices are fixed sizes you could unroll the loops (does java do this with constants?)

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 on other sites
If you're looking for a faster multiplication algorithm, you could take a look at this: Clicky

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 on other sites
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)

• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 30
• 9
• 16
• 22