Created
September 16, 2019 03:12
-
-
Save vznvzn/dd1eb311b944d169b9fe8211baeeeb9e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def f2(n) | |
return n.odd? ? (n * 3 + 1) / 2 : n / 2 | |
end | |
def dense(w, d) | |
return '' if (w == 0) | |
w2 = w - 1 | |
a = (0...w2).to_a | |
s = '0' * w2 | |
(1..(d * w - 1)).map { a.delete_at(rand(a.size)) }.each { |x| s[x, 1] = '1' } | |
return ('1' + s) | |
end | |
def d(s) | |
c = s.split('').select { |x| x == '1' }.size | |
d = c.to_f / s.length | |
return d | |
end | |
def lenx(ns, p) | |
l = ns.split(p) | |
l = [] if (l.nil?) | |
l.shift if (l[0] == '') | |
return l.map { |x| x.length } | |
end | |
def len01x(ns) | |
return lenx(ns, /0+/), lenx(ns, /1+/) | |
end | |
def mx01x(ns) | |
l1, l0 = len01x(ns) | |
return (l1 + l0).max | |
end | |
def seq(n) | |
n1 = n | |
l = [] | |
begin | |
l << n | |
n = f2(n) | |
end while (n != 1) | |
return l | |
end | |
def measure(l) | |
cm1 = (0...l.size).max_by { |x| l[x] } | |
cg = l.index { |x| x < l[0] } | |
cg = l.size - 1 if (cg.nil?) | |
cm = (0..cg).max_by { |x| l[x] } | |
c = l.size | |
{'cg' => cg, 'c' => c, 'cm1' => cm1, 'cm' => cm} | |
end | |
def outa(f, l, a = {}, t = '') | |
f.puts('$dat << eof') | |
k = l[0].keys | |
f.puts(k.join("\t")) | |
l.each { |x| f.puts(x.values.join("\t")) } | |
f.puts('eof') | |
f.puts("set colors classic; set ytics nomirror; set y2tics; set title '#{t}'; plot \\") | |
k.each { |x| f.puts("'$dat' using (column('#{x}')) with line #{a.fetch(x, '')},\\") } | |
f.puts | |
# f.puts("reset; pause -1;") | |
end | |
def run(w) | |
w2 = w / 2 | |
w1 = rand(w2) | |
i = rand(w - w1) | |
ns = ' ' * w | |
ns[i, w1] = '1' * w1 | |
a = Hash[[(0...w).to_a, [nil] * w].transpose] | |
(i...(i + w1)).each { |x| a.delete(x) } | |
l = a.keys | |
w1.times { x = l.delete_at(rand(l.size)); a.delete(x); ns[x, 1] = '0' } | |
w3 = w - 2 * w1 | |
ns2 = dense(w3, 0.5) | |
a.keys.each_with_index \ | |
{ | |
|x, i| | |
ns[x, 1] = ns2[i, 1] | |
} | |
return ns | |
end | |
def lsb(ns, b) | |
ns = ns.dup | |
i = 0 | |
while (ns.length > 0) | |
break if (ns[0, 1] == b) | |
ns[0, 1] = '' | |
end | |
return i | |
end | |
def sum(l) | |
return l.inject { |t, x| t + x } | |
end | |
def avg(l) | |
return l.empty? ? 0 : sum(l).to_f / l.size | |
end | |
def avgs(l) | |
ks = l[0][0].keys | |
l1 = [] | |
l[0].size.times \ | |
{ | |
|i| | |
l1[i] = Hash[[ks, ks.map { |k| avg(l.map { |x| x[i][k] }) }].transpose] | |
} | |
return l1 | |
end | |
def dataset() | |
w = 500 | |
l2 = [] | |
200.times \ | |
{ | |
ns = run(w) | |
mx01 = mx01x(ns) | |
l = seq(ns.to_i(2)) | |
raise if (l[w].nil?) | |
a = measure(l) | |
a.merge!({'nw2' => l[w].to_s(2).length, | |
'mx01' => mx01, | |
'lsb1' => lsb(ns, '1'), | |
'lsb0' => lsb(ns, '0')}) | |
a['nw2'] /= 10 | |
a['cg'] *= 10 | |
a['cm'] *= 10 | |
l2 << a | |
} | |
l2.sort_by! { |x| x['mx01'] } | |
return l2 | |
end | |
outa(f = File.open('gnuplot.cmd', 'w'), avgs((1..100).map { dataset() }), | |
{'c' => 'axes x1y2', | |
'lsb1' => 'lw 2', | |
'lsb0' => 'lw 2'}) | |
f.close | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment