The Computer Language
24.11 Benchmarks Game

spectral-norm Matz's Ruby #8 program

source code

# The Computer Language Benchmarks Game
#   https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
#
#   Naive transliteration from Sebastien Loisel's C program
#   contributed by Isaac Gouy
#

def eval_A(i, j) return 1.0/((i+j)*(i+j+1)/2+i+1) end

def eval_A_times_u(n, u, au)
   for i in 0...n  
      au[i]=0
      for j in 0...n do au[i]+=eval_A(i,j)*u[j] end
   end
end

def eval_At_times_u(n, u, au)
   for i in 0...n 
      au[i]=0
      for j in 0...n do au[i]+=eval_A(j,i)*u[j] end
   end      
end

def eval_AtA_times_u(n, u, atAu)
   v=[0]*n; eval_A_times_u(n,u,v); eval_At_times_u(n,v,atAu)
end

def main(n)
   u=[1]*n
   v=[0]*n    
   for i in 0...10 
      eval_AtA_times_u(n,u,v);
      eval_AtA_times_u(n,v,u);   
   end
   vBv=vv=0
   for i in 0...n do vBv+=u[i]*v[i]; vv+=v[i]*v[i] end
   puts "%.9f" % Math.sqrt(vBv/vv)    
end  

n = (ARGV[0] || 100).to_i 
main n
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
ruby 1.8.7 
(2014-01-28
patchlevel 376)
[x86_64-linux]



 Mon, 21 Oct 2024 23:08:58 GMT

COMMAND LINE:
 /usr/share/rvm/rubies/ruby-1.8.7-head/bin/ruby  spectralnorm.mri-8.mri 5500

PROGRAM OUTPUT:
1.274224153