The Computer Language
Benchmarks Game

spectral-norm TypeScript program

source code

/* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/
   contributed by Isaac Gouy 
*/


/// <reference path="./Include/node/index.d.ts" />

function approximate(n: number): number {
   let u = Array(n), v = Array(n)
   for (let i=0; i<n; ++i) {
      u[i] = 1.0     
   }
   for (let i=0; i<10; ++i) {
      multiplyAtAv(n,u,v)
      multiplyAtAv(n,v,u)
   }
   let vBv = 0.0, vv = 0.0
   for (let i=0; i<10; ++i) {
      vBv += u[i]*v[i]
      vv  += v[i]*v[i]
   }
   return Math.sqrt(vBv/vv)
}

function a(i,j: number): number {
   return 1.0 / ( (i+j) * ((i+j) +1)/2 + i+1 ) 
}

function multiplyAv(n: number, v: number[], av: number[]) {
   for (let i=0; i<n-1; ++i) {
      av[i] = 0.0
      for (let j=0; j<n-1; ++j) {
         av[i] += a(i,j) * v[j] 
      }
   }
}

function multiplyAtv(n: number, v: number[], atv: number[]) {
   for (let i=0; i<n-1; ++i) {
      atv[i] = 0.0
      for (let j=0; j<n-1; ++j) {
         atv[i] += a(j,i) * v[j] 
      }
   }
}

function multiplyAtAv(n: number, v: number[], atAv: number[]) {
   let u = new Array(n) 
   multiplyAv(n,v,u)
   multiplyAtv(n,u,atAv)
}


console.log( approximate(+process.argv[2]).toFixed(9) )

    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
Version 2.6.1
node.js v9.0.0


Wed, 01 Nov 2017 00:07:09 GMT

MAKE:
mv spectralnorm.typescript spectralnorm.ts
/opt/src/node-v9.0.0-linux-x64/bin/tsc --alwaysStrict -t ESNEXT  spectralnorm.ts

2.83s to complete and log all make actions

COMMAND LINE:
/opt/src/node-v9.0.0-linux-x64/bin/node --use_strict spectralnorm.js 5500

PROGRAM OUTPUT:
1.274224153