matrix: optimize loops
authorAlexis Laferrière <alexis.laf@xymus.net>
Tue, 17 Oct 2017 00:24:23 +0000 (20:24 -0400)
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/gamnit/programs.nit
lib/matrix/matrix.nit

index aeb3c06..9df20cd 100644 (file)
@@ -566,8 +566,8 @@ redef extern class NativeGLfloatArray
        # Overwrite this matrix with the identity matrix
        fun set_identity
        do
-               for i in 4.times do
-                       for j in 4.times do
+               for i in [0..4[ do
+                       for j in [0..4[ do
                                matrix_set(i, j, if i == j then 1.0 else 0.0)
                        end
                end
@@ -584,8 +584,8 @@ redef class Matrix
        # Copy content of this matrix to a `NativeGLfloatArray`
        fun fill_native(native: NativeGLfloatArray)
        do
-               for i in width.times do
-                       for j in height.times do
+               for i in [0..width[ do
+                       for j in [0..height[ do
                                native.matrix_set(i, j, self[i, j])
                        end
                end
index 5d9a89d..907afbe 100644 (file)
@@ -52,8 +52,8 @@ class Matrix
 
                for j in height.times do assert items[j].length == width
 
-               for j in height.times do
-                       for i in width.times do
+               for j in [0..height[ do
+                       for i in [0..width[ do
                                self[j, i] = items[j][i]
                        end
                end
@@ -123,8 +123,8 @@ class Matrix
                assert size >= 0
 
                var matrix = new Matrix(size, size)
-               for i in size.times do
-                       for j in size.times do
+               for i in [0..size[ do
+                       for j in [0..size[ do
                                matrix[j, i] = if i == j then 1.0 else 0.0
                        end
                end
@@ -213,10 +213,10 @@ class Matrix
                assert self.width == other.height
 
                var out = new Matrix(other.width, self.height)
-               for j in self.height.times do
-                       for i in other.width.times do
+               for j in [0..self.height[ do
+                       for i in [0..other.width[ do
                                var sum = items[0].zero
-                               for k in self.width.times do sum += self[j, k] * other[k, i]
+                               for k in [0..self.width[ do sum += self[j, k] * other[k, i]
                                out[j, i] = sum
                        end
                end