-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy path4.rb
63 lines (52 loc) · 1.19 KB
/
4.rb
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
55
56
57
58
59
60
61
62
63
# The Computer Language Benchmarks Game
# https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
# Contributed by Sokolov Yura
# Modified by Chris Houhoulis (April 2013):
# - made loops uglier to avoid the unnecessary overhead of blocks
# - nicer naming for readability
ARRAY_LENGTH = ARGV[0].to_i
u = Array.new(ARRAY_LENGTH, 1)
v = []
def eval_A(i,j)
1.0/((i+j)*(i+j+1)/2+i+1)
end
def vector_times_array(vector)
arr, i = [], 0
while i < ARRAY_LENGTH
sum, j = 0, 0
while j < ARRAY_LENGTH
sum += eval_A(i,j) * vector[j]
j += 1
end
arr << sum
i += 1
end
arr
end
def vector_times_array_transposed(vector)
arr, i = [], 0
while i < ARRAY_LENGTH
sum, j = 0, 0
while j < ARRAY_LENGTH
sum += eval_A(j,i) * vector[j]
j += 1
end
arr << sum
i += 1
end
arr
end
def vector_times_array_times_array_transposed(vector)
vector_times_array_transposed(vector_times_array(vector))
end
10.times do
v = vector_times_array_times_array_transposed(u)
u = vector_times_array_times_array_transposed(v)
end
vBv, vv, i = 0, 0, 0
while i < ARRAY_LENGTH
vBv += u[i]*v[i]
vv += v[i]*v[i]
i += 1
end
print "%0.9f" % (Math.sqrt(vBv/vv)), "\n"