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="/usr/local/src/typescript/node.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:
32-bit Ubuntu one core
Version 2.0.3
node.js v6.9.0


Wed, 19 Oct 2016 19:59:44 GMT

MAKE:
mv spectralnorm.typescript spectralnorm.ts
/usr/local/src/node-v6.9.0-linux-x86/bin/tsc -t ES2015 spectralnorm.ts
../../../../../usr/local/src/typescript/node.d.ts(393,11): error TS2430: Interface 'NodeBuffer' incorrectly extends interface 'Uint8Array'.
  Types of property 'fill' are incompatible.
    Type '(value: any, offset?: number, end?: number) => Buffer' is not assignable to type '(value: number, start?: number, end?: number) => this'.
      Type 'Buffer' is not assignable to type 'this'.
/home/dunham/benchmarksgame/nanobench/makefiles/u32.programs.Makefile:601: recipe for target 'spectralnorm.typescript_run' failed
make: [spectralnorm.typescript_run] Error 2 (ignored)
/usr/local/src/node-v6.3.0-linux-x86/lib/node_modules/babel-cli/bin/babel.js --plugins transform-es2015-modules-commonjs spectralnorm.js -o spectralnorm.js
make: /usr/local/src/node-v6.3.0-linux-x86/lib/node_modules/babel-cli/bin/babel.js: Command not found
/home/dunham/benchmarksgame/nanobench/makefiles/u32.programs.Makefile:601: recipe for target 'spectralnorm.typescript_run' failed
make: [spectralnorm.typescript_run] Error 127 (ignored)
2.75s to complete and log all make actions

COMMAND LINE:
/usr/local/src/node-v6.9.0-linux-x86/bin/node --use_strict spectralnorm.js 5500

PROGRAM OUTPUT:
1.274224153