Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created March 10, 2019 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vznvzn/54c6db97b05a1e056e6fc40a05f45a85 to your computer and use it in GitHub Desktop.
Save vznvzn/54c6db97b05a1e056e6fc40a05f45a85 to your computer and use it in GitHub Desktop.
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
end
def dense(w, d)
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 len(ns, p)
l = ns.split(p)
l = [''] if (l.nil? || l.empty?)
return l.map { |x| x.length }
end
def len01(ns)
return len(ns, /0+/), len(ns, /1+/)
end
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def e(ns)
return len01(ns).flatten.size.to_f / ns.length
end
def avg(l)
l.inject { |t, x| t + x }.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()
ns = dense(100, 0.5)
n = ns.to_i(2)
l = []
begin
n = f2(n)
l << n.to_s(2)
end while (l[-1].length >= 50)
l2 = []
10.times \
{
|j|
z = l.map { |x| x[j, 1] }.join
l2 << {'d' => d(z), 'e' => e(z)}
}
return l2
end
def gp(l)
f = File.open('gnuplot.cmd', 'w')
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')
e = l[-1]['e'].round(2)
f.puts("plot 0.5 dt 3 lw 3, #{e} dt 3 lw 3,\\")
k.each \
{
|x|
f.puts("$dat using (column('#{x}')) with line lw 2,\\")
}
f.close
end
gp(avgs((1..50).map { dataset() }))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment