summaryrefslogtreecommitdiff
path: root/jni/ruby/benchmark/bm_so_spectralnorm.rb
diff options
context:
space:
mode:
authorJari Vetoniemi <jari.vetoniemi@indooratlas.com>2020-03-16 18:49:26 +0900
committerJari Vetoniemi <jari.vetoniemi@indooratlas.com>2020-03-30 00:39:06 +0900
commitfcbf63e62c627deae76c1b8cb8c0876c536ed811 (patch)
tree64cb17de3f41a2b6fef2368028fbd00349946994 /jni/ruby/benchmark/bm_so_spectralnorm.rb
Fresh start
Diffstat (limited to 'jni/ruby/benchmark/bm_so_spectralnorm.rb')
-rw-r--r--jni/ruby/benchmark/bm_so_spectralnorm.rb50
1 files changed, 50 insertions, 0 deletions
diff --git a/jni/ruby/benchmark/bm_so_spectralnorm.rb b/jni/ruby/benchmark/bm_so_spectralnorm.rb
new file mode 100644
index 0000000..6b97206
--- /dev/null
+++ b/jni/ruby/benchmark/bm_so_spectralnorm.rb
@@ -0,0 +1,50 @@
+# The Computer Language Shootout
+# http://shootout.alioth.debian.org/
+# Contributed by Sokolov Yura
+
+def eval_A(i,j)
+ return 1.0/((i+j)*(i+j+1)/2+i+1)
+end
+
+def eval_A_times_u(u)
+ v, i = nil, nil
+ (0..u.length-1).collect { |i|
+ v = 0
+ for j in 0..u.length-1
+ v += eval_A(i,j)*u[j]
+ end
+ v
+ }
+end
+
+def eval_At_times_u(u)
+ v, i = nil, nil
+ (0..u.length-1).collect{|i|
+ v = 0
+ for j in 0..u.length-1
+ v += eval_A(j,i)*u[j]
+ end
+ v
+ }
+end
+
+def eval_AtA_times_u(u)
+ return eval_At_times_u(eval_A_times_u(u))
+end
+
+n = 500 # ARGV[0].to_i
+
+u=[1]*n
+for i in 1..10
+ v=eval_AtA_times_u(u)
+ u=eval_AtA_times_u(v)
+end
+vBv=0
+vv=0
+for i in 0..n-1
+ vBv += u[i]*v[i]
+ vv += v[i]*v[i]
+end
+
+str = "%0.9f" % (Math.sqrt(vBv/vv)), "\n"
+# print str