-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathmatmul_v2.lua
More file actions
48 lines (41 loc) · 905 Bytes
/
Copy pathmatmul_v2.lua
File metadata and controls
48 lines (41 loc) · 905 Bytes
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
-- Written by Attractive Chaos; distributed under the MIT license
-- Adapted to LuaJIT FFI by Mike Pall.
local ffi = require("ffi")
local matrix = {}
function matrix.new(m, n)
return ffi.new("double["..m.."]["..n.."]")
end
function matrix.T(a, n)
local y = matrix.new(n, n)
for i = 0, n - 1 do
for j = 0, n - 1 do
y[i][j] = a[j][i]
end
end
return y
end
function matrix.mul(a, b, n)
local y = matrix.new(n, n)
local c = matrix.T(b, n)
for i = 1, n - 1 do
for j = 1, n - 1 do
local sum = 0
for k = 0, n - 1 do sum = sum + a[i][k] * c[j][k] end
y[i][j] = sum
end
end
return y
end
function matgen(n)
local y, tmp = matrix.new(n, n), 1 / n / n
for i = 0, n - 1 do
for j = 0, n - 1 do
y[i][j] = tmp * (i - j) * (i + j)
end
end
return y
end
local n = tonumber(arg[1]) or 100
n = math.floor(n/2) * 2
local a = matrix.mul(matgen(n), matgen(n), n)
print(a[n/2][n/2])