How come Java does not have a matrix datatype? It has everything under the sun, and a bag of chips but no matrix datatype or object in the API? Sure, I can easily simulate it using a 2D array, but that’s not the most convenient thing in the world. So I started writing my own matrix class which would allow me to do something like this:
dp = a.traspose().multiply(b.add(c)).dotproduct(d)
That thing above is totally meaningless, and I made it up on the spot, but these are the kind of calculations I’m currently working at. If I could just call methods instead of writing nexted loops all ver the place both writability and readability of my code would improve greately.
But then I remembered that I will have to pay in blood for every single method call inside my monstrous loops. First thing I needed to do with my old code was to remove every method call that was not necessary from the part of the code that iterates for a long friken time.
So instead I decided to just leave it alone, and keep decorating my code with things like:
for(int i=0; i<A_ROWS; i++)
for(int j=0; j<B_COLS; j++)
for(int k=0; k<A_COLS; k++)
result[i][j] += a[i][k] * b[k][j];
The sizes of the matrices are known from the start and do not change, so I make them into constants. This way the values are inlined at compile time, and I shave off few millisecods of a memory lookup. Then I try to squeeze more than one, unrelated matrix multiplication into the same loop. :P
So once again, you can either modularize or optimize but you can rarely have both at the same time…
[tags]matrix, matrices, programming, java, modularization, optimization[/tags]
http://math.nist.gov/javanumerics/jama/
http://sourceforge.net/projects/jmatrices/
Yeah, there are some 3rd party packages out there, and I was considering using them, but once again – to many method calls. I don’t think it’s worth brining in one of those, if in the end I will have to rewrite the code to work with raw arrays just to shave of few milliseconds here and there…