summaryrefslogtreecommitdiff
path: root/lua_benchmark/tests/Lua-Benchmarks/spectral-norm.lua
diff options
context:
space:
mode:
authorMatthew Sotoudeh <matthew@masot.net>2024-05-17 15:57:30 -0700
committerMatthew Sotoudeh <matthew@masot.net>2024-05-17 15:57:30 -0700
commitd068f0b3c11348a50c18af1ee3b0d2e5f38c4faf (patch)
treedb777acca2336f8c279e9f09346f02de7ddaa0e9 /lua_benchmark/tests/Lua-Benchmarks/spectral-norm.lua
parent221b05e7a86faa38036429d5fbfc8b0779eb5382 (diff)
lua benchmarks
Diffstat (limited to 'lua_benchmark/tests/Lua-Benchmarks/spectral-norm.lua')
-rw-r--r--lua_benchmark/tests/Lua-Benchmarks/spectral-norm.lua43
1 files changed, 43 insertions, 0 deletions
diff --git a/lua_benchmark/tests/Lua-Benchmarks/spectral-norm.lua b/lua_benchmark/tests/Lua-Benchmarks/spectral-norm.lua
new file mode 100644
index 0000000..5ba652f
--- /dev/null
+++ b/lua_benchmark/tests/Lua-Benchmarks/spectral-norm.lua
@@ -0,0 +1,43 @@
+-- The Computer Language Benchmarks Game
+-- http://benchmarksgame.alioth.debian.org/
+-- contributed by Mike Pall
+
+local function A(i, j)
+ local ij = i+j-1
+ return 1.0 / (ij * (ij-1) * 0.5 + i)
+end
+
+local function Av(x, y, N)
+ for i=1,N do
+ local a = 0
+ for j=1,N do a = a + x[j] * A(i, j) end
+ y[i] = a
+ end
+end
+
+local function Atv(x, y, N)
+ for i=1,N do
+ local a = 0
+ for j=1,N do a = a + x[j] * A(j, i) end
+ y[i] = a
+ end
+end
+
+local function AtAv(x, y, t, N)
+ Av(x, t, N)
+ Atv(t, y, N)
+end
+
+local N = tonumber(arg and arg[1]) or 100
+local u, v, t = {}, {}, {}
+for i=1,N do u[i] = 1 end
+
+for i=1,10 do AtAv(u, v, t, N) AtAv(v, u, t, N) end
+
+local vBv, vv = 0, 0
+for i=1,N do
+ local ui, vi = u[i], v[i]
+ vBv = vBv + ui*vi
+ vv = vv + vi*vi
+end
+io.write(string.format("%0.9f\n", math.sqrt(vBv / vv)))
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback