1 // The Great Computer Language Shootout 2 // http://shootout.alioth.debian.org/ 3 // 4 // contributed by Ian Osgood 5 6 function A(i,j) { 7 return 1/((i+j)*(i+j+1)/2+i+1); 8 } 9 10 function Au(u,v) { 11 for (var i=0; i<u.length; ++i) { 12 var t = 0; 13 for (var j=0; j<u.length; ++j) 14 t += A(i,j) * u[j]; 15 v[i] = t; 16 } 17 } 18 19 function Atu(u,v) { 20 for (var i=0; i<u.length; ++i) { 21 var t = 0; 22 for (var j=0; j<u.length; ++j) 23 t += A(j,i) * u[j]; 24 v[i] = t; 25 } 26 } 27 28 function AtAu(u,v,w) { 29 Au(u,w); 30 Atu(w,v); 31 } 32 33 function spectralnorm(n) { 34 var i, u=[], v=[], w=[], vv=0, vBv=0; 35 for (i=0; i<n; ++i) { 36 u[i] = 1; v[i] = w[i] = 0; 37 } 38 for (i=0; i<10; ++i) { 39 AtAu(u,v,w); 40 AtAu(v,u,w); 41 } 42 for (i=0; i<n; ++i) { 43 vBv += u[i]*v[i]; 44 vv += v[i]*v[i]; 45 } 46 return Math.sqrt(vBv/vv); 47 } 48 49 for (var i = 6; i <= 48; i *= 2) { 50 spectralnorm(i); 51 } 52