The Matrix (Multiplication)

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…

Related Posts:

  • Matrix Effect
  • Matlab vs Java
  • Multiply Numbers by Drawing Lines
  • Matrix Priesthood Poster
  • Publications
  • Distractions
  • Immortal
  • Dr. Keanu Osterman?
  • The best part of programming is…
  • To get Hi-Def DVD you must buy OEM

  • 2 Responses to “The Matrix (Multiplication)”

    1. Gravatar ZeWrestler UNITED STATES Says: Reply to this comment

      http://math.nist.gov/javanumerics/jama/

      http://sourceforge.net/projects/jmatrices/

      Posted using Mozilla Firefox Mozilla Firefox 1.5.0.7 on Windows Windows 2000
    2. Gravatar Luke UNITED STATES Says: Reply to this comment

      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…

      Posted using Mozilla Firefox Mozilla Firefox 1.0.8 on Ubuntu Linux Ubuntu Linux

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <pre lang=""> <em> <i> <strike> <strong>

    [Quote selected]