summaryrefslogtreecommitdiff
path: root/benchmark/so_spectralnorm.yml
diff options
context:
space:
mode:
authork0kubun <k0kubun@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-07-08 17:36:26 +0000
committerk0kubun <k0kubun@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-07-08 17:36:26 +0000
commit929982ecea67dc60d4e555f8bacc72dd291b917b (patch)
treed0dbe8fb5b5431a6447216dd31390962a8b521cc /benchmark/so_spectralnorm.yml
parentd933fe9b85846f3cab5161bc1dfd2f456810808f (diff)
benchmark/*.yml: convert from benchmark/bm_*.rb
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63900 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'benchmark/so_spectralnorm.yml')
-rw-r--r--benchmark/so_spectralnorm.yml54
1 files changed, 54 insertions, 0 deletions
diff --git a/benchmark/so_spectralnorm.yml b/benchmark/so_spectralnorm.yml
new file mode 100644
index 0000000..45373b6
--- /dev/null
+++ b/benchmark/so_spectralnorm.yml
@@ -0,0 +1,54 @@
+prelude: |
+ # The Computer Language Shootout
+ # http://shootout.alioth.debian.org/
+ # Contributed by Sokolov Yura
+benchmark:
+ so_spectralnorm: |
+
+ 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
+loop_count: 1