github.com/coyove/nj@v0.0.0-20221110084952-c7f8db1065c3/tests/bench/spec.lua (about)

     1  -- The Computer Language Benchmarks Game
     2  -- https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
     3  -- contributed by Mike Pall
     4  
     5  local function A(i, j)
     6    local ij = i+j-1
     7    return 1.0 / (ij * (ij-1) * 0.5 + i)
     8  end
     9  
    10  local function Av(x, y, N)
    11    for i=1,N do
    12      local a = 0
    13      for j=1,N do a = a + x[j] * A(i, j) end
    14      y[i] = a
    15    end
    16  end
    17  
    18  local function Atv(x, y, N)
    19    for i=1,N do
    20      local a = 0
    21      for j=1,N do a = a + x[j] * A(j, i) end
    22      y[i] = a
    23    end
    24  end
    25  
    26  local function AtAv(x, y, t, N)
    27    Av(x, t, N)
    28    Atv(t, y, N)
    29  end
    30  
    31  local N = tonumber(arg and arg[1]) or 550
    32  local u, v, t = {}, {}, {}
    33  for i=1,N do u[i] = 1 end
    34  
    35  for i=1,10 do AtAv(u, v, t, N) AtAv(v, u, t, N) end
    36  
    37  local vBv, vv = 0, 0
    38  for i=1,N do
    39    local ui, vi = u[i], v[i]
    40    vBv = vBv + ui*vi
    41    vv = vv + vi*vi
    42  end
    43  print(math.sqrt(vBv / vv))