The Computer Language
24.11 Benchmarks Game

spectral-norm Node.js #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
//

function eval_A(i,j) { return 1.0/((i+j)*(i+j+1)/2+i+1) } 

function eval_A_times_u(N, u, Au)
  {
    i = j = 0
    for(i=0;i<N;i++)
      {
        Au[i]=0
        for(j=0;j<N;j++) Au[i]+=eval_A(i,j)*u[j]
      }
  }

function eval_At_times_u(N, u, Au)
  {
    i,j
    for(i=0;i<N;i++)
      {
        Au[i]=0
        for(j=0;j<N;j++) Au[i]+=eval_A(j,i)*u[j]
      }
  }

function eval_AtA_times_u(N, u, AtAu)
  { v = [N]; eval_A_times_u(N,u,v); eval_At_times_u(N,v,AtAu) }

function main(N) {
  var i = 0
  let u = [N], v = [N]
  for(i=0;i<N;i++) { u[i]=1; v[i]=0 }  
  for(i=0;i<10;i++)
    {
      eval_AtA_times_u(N,u,v)
      eval_AtA_times_u(N,v,u)
    }  
  vBv = vv = 0.0   
  for(i=0;i<N;i++) { vBv += u[i]*v[i]; vv += v[i]*v[i] }  
  console.log( Math.sqrt(vBv/vv).toFixed(9))
}

let n = (+process.argv.length > 2) 
   ? +process.argv[2] 
   : 100
   
main(n)   

    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
v23.0.0


 Wed, 23 Oct 2024 00:18:34 GMT

MAKE:
cp -L spectralnorm.node-8.node spectralnorm.js

0.16s to complete and log all make actions

COMMAND LINE:
 /opt/src/node-v23.0.0/bin/node spectralnorm.js 5500

PROGRAM OUTPUT:
1.274224153