title: Faster Matrix Multiplication in C#
body: <p>I have as small c# project that involves matrices.  I am processing large amounts of data by splitting it into n-length chunks, treating the chucks as vectors, and multiplying by a Vandermonde** matrix.  The problem is, depending on the conditions, the size of the chucks and corresponding Vandermonde** matrix can vary.  I have a general solution which is easy to read, but way too slow:</p>
  
  <pre><code>    public byte[] addBlockRedundancy(byte[] data) {
          if (data.Length!=numGood) D.error("Expecting data to be just "+numGood+" bytes long");
  
          aMatrix d=aMatrix.newColumnMatrix(this.mod, data);
          var r=vandermonde.multiplyBy(d);
          return r.ToByteArray();
      }//method
  </code></pre>
  
  <p>This can process about 1/4 megabytes per second on my i5 U470 @ 1.33GHz.  I can make this faster by manually inlining the matrix multiplication:</p>
  
  <pre><code>        int o=0;
          int d=0;
          for (d=0; d&lt;data.Length-numGood; d+=numGood) {
              for (int r=0; r&lt;numGood+numRedundant; r++) {
                  Byte value=0;
                  for (int c=0; c&lt;numGood; c++) {
                      value=mod.Add(value, mod.Multiply(vandermonde.get(r, c), data[d+c]));
                  }//for
                  output[r][o]=value;
              }//for
              o++;
          }//for
  </code></pre>
  
  <p>This can process about 1 meg a second. </p>
  
  <p>(Please note the "mod" is performing operations over GF(2^8) modulo my favorite irreducible polynomial.)</p>
  
  <p>I know this can get a lot faster:  After all, the Vandermonde** matrix is mostly zeros.  I should be able to make a routine, or find a routine, that can take my matrix and return a <em>optimized</em> method which will effectively multiply vectors by the given matrix, but faster.  Then, when I give this routine a 5x5 Vandermonde matrix (the identity matrix), there is simply no arithmetic to perform, and the original data is just copied.</p>
  
  <p>** Please note:  What I use the term "Vandermonde", I actually mean an Identity matrix with some number of rows from the Vandermonde matrix appended (see comments). This matrix is wonderful because of all the zeros, and because if you remove enough rows (of your choosing) to make it square, it is an invertible matrix.  And, of course, I would like to use this same routine to convert any one of those inverted matrices into an optimized series of instructions.  </p>
  
  <p>How can I make this matrix multiplication faster? </p>
  
  <p>Thanks!</p>
  
  <p>(edited to correct my mistake with Vandermonde matrix)</p>