github.com/coyove/nj@v0.0.0-20221110084952-c7f8db1065c3/tests/bench/spec.lua.txt (about) 1 -- The Computer Language Benchmarks Game 2 -- https://salsa.debian.org/benchmarksgame-team/benchmarksgame/ 3 -- contributed by Mike Pall 4 5 local N = tonumber(arg and arg[1]) or 550 6 local _, ...u = array(N) 7 local _, ...v = array(N) 8 local _, ...t = array(N) 9 10 for i=1,N do u[i] = 1 end 11 12 local function A(i, j) 13 local ij = i+j-1 14 return 1.0 / (ij * (ij-1) * 0.5 + i) 15 end 16 17 local function Av(x, y, N) 18 x = x == "u" and u or (x == "v" and v or t) 19 y = y == "u" and u or (y == "v" and v or t) 20 for i=1,N do 21 local a = 0 22 for j=1,N do a = a + x[j] * A(i, j) end 23 y[i] = a 24 end 25 end 26 27 local function Atv(x, y, N) 28 x = x == "u" and u or (x == "v" and v or t) 29 y = y == "u" and u or (y == "v" and v or t) 30 for i=1,N do 31 local a = 0 32 for j=1,N do a = a + x[j] * A(j, i) end 33 y[i] = a 34 end 35 end 36 37 local function AtAv(x, y, t, N) 38 Av(x, t, N) 39 Atv(t, y, N) 40 end 41 42 for i=1,10 do AtAv("u", "v", "t", N) AtAv("v", "u", "t", N) end 43 44 local vBv, vv = 0, 0 45 for i=1,N do 46 local ui, vi = u[i], v[i] 47 vBv = vBv + ui*vi 48 vv = vv + vi*vi 49 end 50 51 print(sqrt(vBv / vv))