2022-07-24 17:03:35 +02:00
|
|
|
//! This script simulates multi-dimensional matrix calculations.
|
2022-01-20 05:06:36 +01:00
|
|
|
|
2020-04-13 17:38:10 +02:00
|
|
|
const SIZE = 50;
|
|
|
|
|
|
|
|
fn new_mat(x, y) {
|
2021-05-24 06:12:16 +02:00
|
|
|
let row = [];
|
|
|
|
row.pad(y, 0.0);
|
2022-01-20 05:06:36 +01:00
|
|
|
|
2021-05-24 06:12:16 +02:00
|
|
|
let matrix = [];
|
|
|
|
matrix.pad(x, row);
|
2022-01-20 05:06:36 +01:00
|
|
|
|
2021-05-24 06:12:16 +02:00
|
|
|
matrix
|
2020-04-13 17:38:10 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-12-15 05:06:17 +01:00
|
|
|
for i in 0..n {
|
|
|
|
for j in 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
|
|
|
}
|
|
|
|
}
|
2022-01-20 05:06:36 +01:00
|
|
|
|
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);
|
2022-01-20 05:06:36 +01:00
|
|
|
|
2021-12-15 05:06:17 +01:00
|
|
|
for i in 0..a[0].len {
|
|
|
|
for j in 0..b[0].len {
|
2020-04-26 12:37:41 +02:00
|
|
|
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
|
|
|
|
2021-12-15 05:06:17 +01:00
|
|
|
for i in 0..c.len {
|
|
|
|
for j in 0..c[i].len {
|
2021-05-24 06:12:16 +02:00
|
|
|
c[i][j] = 0.0;
|
2022-01-20 05:06:36 +01:00
|
|
|
|
2021-12-15 05:06:17 +01:00
|
|
|
for z in 0..a[i].len {
|
2021-05-24 06:12:16 +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-05-31 09:57:53 +02:00
|
|
|
/*
|
2021-12-15 05:06:17 +01:00
|
|
|
for i in 0..SIZE) {
|
2021-05-24 06:12:16 +02:00
|
|
|
print(c[i]);
|
2020-04-13 17:38:10 +02:00
|
|
|
}
|
2020-05-31 09:57:53 +02:00
|
|
|
*/
|
2020-04-13 17:38:10 +02:00
|
|
|
|
2021-04-04 17:22:45 +02:00
|
|
|
print(`Finished. Run time = ${now.elapsed} seconds.`);
|