-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.fc
52 lines (45 loc) · 1.37 KB
/
2.fc
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
{-
TASK 2 - Matrix multiplier
Write the method that multiplies one matrix by another.
The 1st and 2nd parameter of the function will contain
a matrix implemented using tuples (eg: [[1,2], [3,4]]).
Matrices can be of different sizes, ranging from 4x4
up to 32x32.
Matrix can be rectangular where matrixA must to be of size n*m & matrixB of size m*p.
After calculating the result matrix, the function should return final result (as tuple).
-}
forall X -> (tuple, X) ~tpop (tuple t) asm "TPOP";
forall X -> int is_tuple (X x) asm "ISTUPLE";
forall X -> tuple cast_to_tuple (X x) asm "NOP";
forall X -> int cast_to_int (X x) asm "NOP";
forall X -> (tuple) to_tuple (X x) asm "NOP";
(int) tlen (tuple t) asm "TLEN";
() recv_internal() {
}
;; testable
(tuple) matrix_multiplier(tuple matrixA, tuple matrixB) method_id {
tuple result = empty_tuple();
int n = matrixA.tlen();
int m = matrixB.tlen();
int p = matrixB.first().tlen();
int i = 0;
while (i < n) {
tuple row = empty_tuple();
int j = 0;
tuple first = cast_to_tuple(matrixA.at(i));
while (j < p) {
int elem = 0;
int k = 0;
while (k < m) {
tuple second = cast_to_tuple(matrixB.at(k));
elem += cast_to_int(first.at(k)) * cast_to_int(second.at(j));
k += 1;
}
row~tpush(elem);
j += 1;
}
result~tpush(row);
i += 1;
}
return result;
}