Class: Quasi::LowDiscrepancySequence

Inherits:
Object
  • Object
show all
Defined in:
lib/LowDiscrepancySequence.rb

Direct Known Subclasses

Halton, NX

Instance Method Summary collapse

Constructor Details

#initialize(dim) ⇒ LowDiscrepancySequence

Returns a new instance of LowDiscrepancySequence.



13
14
15
16
17
18
# File 'lib/LowDiscrepancySequence.rb', line 13

def initialize(dim)
@dim = dim
@index = 1 
@x = NArray.float(dim)
@y = NArray.float(dim)
end

Instance Method Details

#L2_discrepancy(n, r) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/LowDiscrepancySequence.rb', line 50

def L2_discrepancy(n, r)
a,b =1.0,1.0
for k in 0..@dim
   a/=2
   b/=3
end

sum_1,sum_2 =0.0,0.0

loops=0
for i in 0..n
  for j in i+1..n
    sum_1+=product(i,j,r)
  end
end

sum_1*=2

for i in 0..n
  sum_1+=product(i,i,r)
end

sum_1/=(n*n)

#second sum
for i in 0..n
  sum_2+=productSQ(i,r)
end

sum_2/=n 
sum_2*=(2.0*a)
return Math.sqrt(sum_1-sum_2+b)

end

#n_inverse(x) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/LowDiscrepancySequence.rb', line 106

def n_inverse(x)
sqrt_two_PI=2.5066282746310

e_1 = -3.969683028665376e+01
e_2 =  2.209460984245205e+02
e_3 = -2.759285104469687e+02
e_4 =  1.383577518672690e+02
e_5 = -3.066479806614716e+01
e_6 =  2.506628277459239e+00

f_1 = -5.447609879822406e+01
f_2 =  1.615858368580409e+02
f_3 = -1.556989798598866e+02
f_4 =  6.680131188771972e+01
f_5 = -1.328068155288572e+01



g_1 = -7.784894002430293e-03
g_2 = -3.223964580411365e-01
g_3 = -2.400758277161838e+00
g_4 = -2.549732539343734e+00
g_5 =  4.374664141464968e+00
g_6 =  2.938163982698783e+00


h_1 =  7.784695709041462e-03
h_2 =  3.224671290700398e-01
h_3 =  2.445134137142996e+00
h_4 =  3.754408661907416e+00

x_l   = 0.02425
x_u  = 1.0 - x_l


#lower

if( x < x_l )
  z = Math.sqrt(-2.0*Math.log(x))
  z = (((((g_1*z+g_2)*z+g_3)*z+g_4)*z+g_5)*z+g_6) / ((((h_1*z+h_2)*z+h_3)*z+h_4)*z+1.0)
elsif( x <= x_u )
#central
  z = x - 0.5; r = z*z
  z = (((((e_1*r+e_2)*r+e_3)*r+e_4)*r+e_5)*r+e_6)*z / (((((f_1*r+f_2)*r+f_3)*r+f_4)*r+f_5)*r+1.0)

#upper
else 
  z = Math.sqrt(-2.0*Math.log(1.0-x))
  z = -(((((g_1*z+g_2)*z+g_3)*z+g_4)*z+g_5)*z+g_6) /  ((((h_1*z+h_2)*z+h_3)*z+h_4)*z+1.0)

end

return z


end

#nextPoint(r) ⇒ Object



24
25
26
27
28
29
# File 'lib/LowDiscrepancySequence.rb', line 24

def nextPoint(r)
x= nextPoint
for k in 0..@dim
  r[k] = x[k]
end
end

#nextQuasiNormalVectorObject

TRANSFORM UNIFORM -> MULTINORMAL need thi, maybe narray



91
92
93
94
95
96
97
98
# File 'lib/LowDiscrepancySequence.rb', line 91

def nextQuasiNormalVector
x=nextPoint()
z = Array.new(@dim)
for k in 0..@dim-1
  z[k]=n_inverse(x[k].to_f);
end
return z
end

#product(i, j, r) ⇒ Object

L^2-DISCREPANCY



33
34
35
36
37
38
39
# File 'lib/LowDiscrepancySequence.rb', line 33

def product(i,j,r)
f=1.0
for k in 0..@dim
  f*=1.0-[r[i][k],r[j][k]].max
end
return f
end

#productSQ(i, r) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/LowDiscrepancySequence.rb', line 41

def productSQ( i, r)
   f=1.0 
     for k in 0..@dim
       f*=(1.0-r[i][k]*r[i][k])
     end
     return f
end

#projectionOut(i, j, nPoints) ⇒ Object



100
101
102
103
104
# File 'lib/LowDiscrepancySequence.rb', line 100

def projectionOut(i,j, nPoints)
  name=getName
  puts i,j
  puts nextPoint(x)
end

#restartObject



20
21
22
# File 'lib/LowDiscrepancySequence.rb', line 20

def restart
@index =1
end