rhai/scripts/mat_mul.rhai
2021-04-17 16:25:39 +08:00

65 lines
969 B
Plaintext

const SIZE = 50;
fn new_mat(x, y) {
let row = [];
row.pad(y, 0.0);
let matrix = [];
matrix.pad(x, row);
matrix
}
fn mat_gen(n) {
let m = new_mat(n, n);
const tmp = 1.0 / n / n;
for i in range(0, n) {
for j in range(0, n) {
m[i][j] = tmp * (i - j) * (i + j);
}
}
m
}
fn mat_mul(a, b) {
let b2 = new_mat(a[0].len, b[0].len);
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(a.len, b[0].len);
for i in range(0, c.len) {
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 = b2[j][z];
c[i][j] += x * y;
}
}
}
c
}
const now = timestamp();
const a = mat_gen(SIZE);
const b = mat_gen(SIZE);
const c = mat_mul(a, b);
/*
for i in range(0, SIZE) {
print(c[i]);
}
*/
print(`Finished. Run time = ${now.elapsed} seconds.`);