rhai/scripts/mat_mul.rhai

72 lines
1.1 KiB
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);
let tmp = 1.0 / n.to_float() / n.to_float();
for i in range(0, n) {
for j in range(0, n) {
m[i][j] = tmp * (i.to_float() - j.to_float()) * (i.to_float() + j.to_float());
2020-04-13 17:38:10 +02:00
}
}
m
}
fn mat_mul(a, b) {
let m = a.len;
let n = a[0].len;
let p = b[0].len;
2020-04-13 17:38:10 +02:00
let b2 = new_mat(n, p);
for i in range(0, n) {
for j in range(0, p) {
b2[j][i] = b[i][j];
2020-04-13 17:38:10 +02:00
}
}
let c = new_mat(m, p);
for i in range(0, c.len) {
2020-04-13 17:38:10 +02:00
let ci = c[i];
for j in range(0, ci.len) {
2020-04-13 17:38:10 +02:00
let b2j = b2[j];
ci[j] = 0.0;
for z in range(0, a[i].len) {
2020-04-13 17:38:10 +02:00
let x = a[i][z];
let y = b2j[z];
ci[j] += x * y;
}
}
c[i] = ci;
}
c
}
let now = timestamp();
let a = mat_gen(SIZE);
let b = mat_gen(SIZE);
let c = mat_mul(a, b);
/*
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
print("Finished. Run time = " + now.elapsed + " seconds.");