rhai/scripts/mat_mul.rhai

65 lines
969 B
Plaintext
Raw Normal View History

2020-04-13 17:38:10 +02:00
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);
2021-04-17 10:25:39 +02:00
const tmp = 1.0 / n / n;
2020-04-13 17:38:10 +02:00
for i in range(0, n) {
for j in range(0, n) {
2021-04-09 16:48:47 +02:00
m[i][j] = tmp * (i - j) * (i + j);
2020-04-13 17:38:10 +02:00
}
}
m
}
fn mat_mul(a, b) {
2021-04-17 10:25:39 +02:00
let b2 = new_mat(a[0].len, b[0].len);
2020-04-13 17:38:10 +02:00
2021-04-17 10:25:39 +02:00
for i in range(0, a[0].len) {
for j in range(0, b[0].len) {
b2[j][i] = b[i][j];
2020-04-13 17:38:10 +02:00
}
}
2021-04-17 10:25:39 +02:00
let c = new_mat(a.len, b[0].len);
2020-04-13 17:38:10 +02:00
for i in range(0, c.len) {
2021-04-17 10:25:39 +02:00
for j in range(0, c[i].len) {
c[i][j] = 0.0;
2020-04-13 17:38:10 +02:00
for z in range(0, a[i].len) {
2020-04-13 17:38:10 +02:00
let x = a[i][z];
2021-04-17 10:25:39 +02:00
let y = b2[j][z];
c[i][j] += x * y;
2020-04-13 17:38:10 +02:00
}
}
}
c
}
2021-04-17 10:25:39 +02:00
const now = timestamp();
2020-04-13 17:38:10 +02:00
2021-04-17 10:25:39 +02:00
const a = mat_gen(SIZE);
const b = mat_gen(SIZE);
const c = mat_mul(a, b);
2020-04-13 17:38:10 +02:00
/*
2020-04-13 17:38:10 +02:00
for i in range(0, SIZE) {
print(c[i]);
}
*/
2020-04-13 17:38:10 +02:00
2021-04-04 17:22:45 +02:00
print(`Finished. Run time = ${now.elapsed} seconds.`);