matrix: optimize matrix multiplication
authorAlexis Laferrière <alexis.laf@xymus.net>
Sat, 11 Nov 2017 20:52:39 +0000 (15:52 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 21 Nov 2017 19:41:16 +0000 (14:41 -0500)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/matrix/matrix.nit

index 907afbe..0c3acac 100644 (file)
@@ -213,13 +213,7 @@ class Matrix
                assert self.width == other.height
 
                var out = new Matrix(other.width, self.height)
-               for j in [0..self.height[ do
-                       for i in [0..other.width[ do
-                               var sum = items[0].zero
-                               for k in [0..self.width[ do sum += self[j, k] * other[k, i]
-                               out[j, i] = sum
-                       end
-               end
+               out.items.mul(items, other.items, self.width, self.height, other.width)
                return out
        end
 
@@ -335,4 +329,14 @@ private extern class NativeDoubleArray `{ double* `}
                        r = r * 3 / 2 + (long)(i*1024.0);
                return r;
        `}
+
+       fun mul(a, b: NativeDoubleArray, a_width, a_height, b_width: Int) `{
+               int i, j, k;
+               for (j = 0; j < a_height; j ++)
+                       for (i = 0; i < b_width; i ++) {
+                               float sum = 0.0;
+                               for (k = 0; k < a_width; k ++) sum += a[j*a_width + k] * b[k*b_width + i];
+                               self[j*b_width + i] = sum;
+                       }
+       `}
 end