forked from edubart/nelua-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatmul.nelua
47 lines (42 loc) · 918 Bytes
/
matmul.nelua
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
require 'sequence'
local Matrix = @sequence(sequence(number))
local function matrix_transpose(a: Matrix, n: integer): Matrix
local x: Matrix = {}
for i=1,n do
x[i] = {}
for j=1,n do
x[i][j] = a[j][i]
end
end
return x
end
local function matrix_multiply(a: Matrix, b: Matrix, n: integer): Matrix
local x: Matrix = {}
local c = matrix_transpose(b, n)
for i=1,n do
x[i] = {}
for j=1,n do
local sum = 0.0
for k=1,n do
sum = sum + a[i][k] * c[j][k]
end
x[i][j] = sum
end
end
return x
end
local function matrix_generate(n: integer): Matrix
local a: Matrix, tmp = {}, 1.0 / n / n
for i=1,n do
a[i] = {}
for j=1,n do
a[i][j] = tmp * (i - j - 2) * (i + j - 2)
end
end
return a
end
local n = 200
local a = matrix_generate(n)
local b = matrix_generate(n)
local res = matrix_multiply(a, b, n)
print(res[n//2+1][n//2+1])