rhai/scripts/mat_mul.rhai

64 lines
958 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
}
2021-04-19 16:40:20 +02:00
fn mat_gen() {
const n = global::SIZE;
2021-04-17 10:25:39 +02:00
const tmp = 1.0 / n / n;
2021-04-19 16:40:20 +02:00
let m = new_mat(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) {
2021-04-19 16:40:20 +02:00
c[i][j] += a[i][z] * b2[j][z];
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-19 16:40:20 +02:00
const a = mat_gen();
const b = mat_gen();
2021-04-17 10:25:39 +02:00
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.`);