From 9a409b5b4903cdacbd46228fd1638fab2e2927f6 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 17 Apr 2021 16:25:39 +0800 Subject: [PATCH] Simplify script. --- scripts/mat_mul.rhai | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/scripts/mat_mul.rhai b/scripts/mat_mul.rhai index 65743a37..11830ff2 100644 --- a/scripts/mat_mul.rhai +++ b/scripts/mat_mul.rhai @@ -12,7 +12,7 @@ fn new_mat(x, y) { fn mat_gen(n) { let m = new_mat(n, n); - let tmp = 1.0 / n / n; + const tmp = 1.0 / n / n; for i in range(0, n) { for j in range(0, n) { @@ -24,43 +24,36 @@ fn mat_gen(n) { } fn mat_mul(a, b) { - let m = a.len; - let n = a[0].len; - let p = b[0].len; - - let b2 = new_mat(n, p); + let b2 = new_mat(a[0].len, b[0].len); - for i in range(0, n) { - for j in range(0, p) { + for i in range(0, a[0].len) { + for j in range(0, b[0].len) { b2[j][i] = b[i][j]; } } - let c = new_mat(m, p); + let c = new_mat(a.len, b[0].len); for i in range(0, c.len) { - let ci = c[i]; - for j in range(0, ci.len) { - let b2j = b2[j]; - ci[j] = 0.0; + for j in range(0, c[i].len) { + c[i][j] = 0.0; for z in range(0, a[i].len) { let x = a[i][z]; - let y = b2j[z]; - ci[j] += x * y; + let y = b2[j][z]; + c[i][j] += x * y; } } - c[i] = ci; } c } -let now = timestamp(); +const now = timestamp(); -let a = mat_gen(SIZE); -let b = mat_gen(SIZE); -let c = mat_mul(a, b); +const a = mat_gen(SIZE); +const b = mat_gen(SIZE); +const c = mat_mul(a, b); /* for i in range(0, SIZE) {