2022-01-20 12:06:36 +08:00
|
|
|
// This script simulates multi-dimensional matrix calculations.
|
|
|
|
|
2020-04-13 23:38:10 +08:00
|
|
|
const SIZE = 50;
|
|
|
|
|
|
|
|
fn new_mat(x, y) {
|
2021-05-24 12:12:16 +08:00
|
|
|
let row = [];
|
|
|
|
row.pad(y, 0.0);
|
2022-01-20 12:06:36 +08:00
|
|
|
|
2021-05-24 12:12:16 +08:00
|
|
|
let matrix = [];
|
|
|
|
matrix.pad(x, row);
|
2022-01-20 12:06:36 +08:00
|
|
|
|
2021-05-24 12:12:16 +08:00
|
|
|
matrix
|
2020-04-13 23:38:10 +08:00
|
|
|
}
|
|
|
|
|
2021-04-19 22:40:20 +08:00
|
|
|
fn mat_gen() {
|
|
|
|
const n = global::SIZE;
|
2021-04-17 16:25:39 +08:00
|
|
|
const tmp = 1.0 / n / n;
|
2021-04-19 22:40:20 +08:00
|
|
|
let m = new_mat(n, n);
|
2020-04-13 23:38:10 +08:00
|
|
|
|
2021-12-15 12:06:17 +08:00
|
|
|
for i in 0..n {
|
|
|
|
for j in 0..n {
|
2021-04-09 22:48:47 +08:00
|
|
|
m[i][j] = tmp * (i - j) * (i + j);
|
2020-04-13 23:38:10 +08:00
|
|
|
}
|
|
|
|
}
|
2022-01-20 12:06:36 +08:00
|
|
|
|
2020-04-13 23:38:10 +08:00
|
|
|
m
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mat_mul(a, b) {
|
2021-04-17 16:25:39 +08:00
|
|
|
let b2 = new_mat(a[0].len, b[0].len);
|
2022-01-20 12:06:36 +08:00
|
|
|
|
2021-12-15 12:06:17 +08:00
|
|
|
for i in 0..a[0].len {
|
|
|
|
for j in 0..b[0].len {
|
2020-04-26 18:37:41 +08:00
|
|
|
b2[j][i] = b[i][j];
|
2020-04-13 23:38:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-17 16:25:39 +08:00
|
|
|
let c = new_mat(a.len, b[0].len);
|
2020-04-13 23:38:10 +08:00
|
|
|
|
2021-12-15 12:06:17 +08:00
|
|
|
for i in 0..c.len {
|
|
|
|
for j in 0..c[i].len {
|
2021-05-24 12:12:16 +08:00
|
|
|
c[i][j] = 0.0;
|
2022-01-20 12:06:36 +08:00
|
|
|
|
2021-12-15 12:06:17 +08:00
|
|
|
for z in 0..a[i].len {
|
2021-05-24 12:12:16 +08:00
|
|
|
c[i][j] += a[i][z] * b2[j][z];
|
|
|
|
}
|
2020-04-13 23:38:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c
|
|
|
|
}
|
|
|
|
|
2021-04-17 16:25:39 +08:00
|
|
|
const now = timestamp();
|
2020-04-13 23:38:10 +08:00
|
|
|
|
2021-04-19 22:40:20 +08:00
|
|
|
const a = mat_gen();
|
|
|
|
const b = mat_gen();
|
2021-04-17 16:25:39 +08:00
|
|
|
const c = mat_mul(a, b);
|
2020-04-13 23:38:10 +08:00
|
|
|
|
2020-05-31 15:57:53 +08:00
|
|
|
/*
|
2021-12-15 12:06:17 +08:00
|
|
|
for i in 0..SIZE) {
|
2021-05-24 12:12:16 +08:00
|
|
|
print(c[i]);
|
2020-04-13 23:38:10 +08:00
|
|
|
}
|
2020-05-31 15:57:53 +08:00
|
|
|
*/
|
2020-04-13 23:38:10 +08:00
|
|
|
|
2021-04-04 23:22:45 +08:00
|
|
|
print(`Finished. Run time = ${now.elapsed} seconds.`);
|