forked from rhaiscript/rhai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmat_mul.rhai
65 lines (48 loc) · 1 KB
/
mat_mul.rhai
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! This script simulates multi-dimensional matrix calculations.
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() {
const n = global::SIZE;
const tmp = 1.0 / n / n;
let m = new_mat(n, n);
for i in 0..n {
for j in 0..n {
m[i][j] = tmp * (i - j) * (i + j);
}
}
m
}
fn mat_mul(a, b) {
let b2 = new_mat(a[0].len, b[0].len);
for i in 0..a[0].len {
for j in 0..b[0].len {
b2[j][i] = b[i][j];
}
}
let c = new_mat(a.len, b[0].len);
for i in 0..c.len {
for j in 0..c[i].len {
c[i][j] = 0.0;
for z in 0..a[i].len {
c[i][j] += a[i][z] * b2[j][z];
}
}
}
c
}
const now = timestamp();
const a = mat_gen();
const b = mat_gen();
const c = mat_mul(a, b);
/*
for i in 0..SIZE) {
print(c[i]);
}
*/
print(`Finished. Run time = ${now.elapsed} seconds.`);