forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspectralnorm.jl
54 lines (47 loc) · 947 Bytes
/
spectralnorm.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#
# The Computer Language Benchmarks Game
# spectral-norm benchmark
# http://shootout.alioth.debian.org/u32/performance.php?test=spectralnorm
#
# Based on the Scala program
#
# infinite matrix function and its transpose
A(i, j) = 1.0 / ((i+j)*(i+j+1)/2 +i+1)
At(j, i) = 1.0 / ((i+j)*(i+j+1)/2 +i+1)
# w <- M * v
function mult(N, v, w, M)
for i = 1:N
s = 0.0
for j = 1:N
s += M(i-1, j-1) * v[j]
end
w[i] = s
end
end
function approximate(N)
u = ones(N)
v = ones(N)
w = ones(N)
for i = 1:10
mult(N, u, w, A)
mult(N, w, v, At)
mult(N, v, w, A)
mult(N, w, u, At)
end
vbv = 0.0
vv = 0.0
for i = 1:N
vbv += u[i] * v[i]
vv += v[i] * v[i]
end
sqrt(vbv / vv)
end
function spectralnorm(N)
@printf("%.09f\n", approximate(N))
end
if length(ARGS) >= 1
N = int(ARGS[1])
else
N = 100
end
spectralnorm(N)