source code
/* The Computer Language Benchmarks Game
https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
contributed by Isaac Gouy
*/
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: number, 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 3.9.2
node.js v14.2.0
Wed, 13 May 2020 17:39:08 GMT
MAKE:
mv spectralnorm.typescript spectralnorm.ts
/opt/src/node-v14.2.0-linux-x64/bin/tsc --strict --noEmitOnError --removeComments spectralnorm.ts
5.74s to complete and log all make actions
COMMAND LINE:
/opt/src/node-v14.2.0-linux-x64/bin/node --use_strict spectralnorm.js 5500
PROGRAM OUTPUT:
1.274224153