1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/benchmark/bm_so_spectralnorm.rb
ko1 30b2cb380e * benchmark/driver.rb: fix notations.
* benchmark/bm_loop_whileloop.rb: ditto.
* benchmark/bm_loop_whileloop2.rb: ditto.
* benchmark/bm_app_uri.rb: added.
* benchmark/bm_vm1_ivar_set.rb: ditto.
* benchmark/bm_so_binary_trees.rb: added from Computer Language
  Benchmarks Game (http://shootout.alioth.debian.org/).
* benchmark/bm_so_fannkuch.rb: ditto.
* benchmark/bm_so_mandelbrot.rb: ditto.
* benchmark/bm_so_meteor_contest.rb: ditto.
* benchmark/bm_so_nbody.rb: ditto.
* benchmark/bm_so_nsieve.rb: ditto.
* benchmark/bm_so_nsieve_bits.rb: ditto.
* benchmark/bm_so_partial_sums.rb: ditto.
* benchmark/bm_so_pidigits.rb: ditto.
* benchmark/bm_so_spectralnorm.rb: ditto.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13548 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-09-28 10:18:53 +00:00

50 lines
920 B
Ruby

# 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