Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 seq(n)
n1 = n
l = []
begin
l << n
n = f2(n)
end while (n != 1)
return l
end
def count(ns)
l = seq(ns.to_i(2))
cm1 = (0...l.size).max_by { |x| l[x] }
cg = l.index { |x| x < l[0] }
cm = (0...cg).max_by { |x| l[x] }
cg1 = l.rindex { |x| x > l[0] }
return {'ns' => ns,
'cm' => cm,
'cm1' => cm1,
'cg' => cg,
'cg1' => cg1,
'l' => l
}
end
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def avg(l)
l.inject { |t, x| t + x }.to_f / l.size
end
def out(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 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()
l = (1..100).map { count(dense(50, 0.5))['l'] }.sort_by { |x| x.size }
l2 = l.map \
{
|l1|
l2 = l1.map { |x| d(x.to_s(2)) }
l3 = []
n = l1[0]
l1.size.times { l3 << d(n.to_s(2)); n *= 3; }
{'c' => l1.size,
'da' => avg(l2),
'dp' => l2.select { |x| x > 0.5}.size,
'd3a' => avg(l3),
'd3p' => l3.select { |x| x > 0.5}.size
}
}
$i = 0 if ($i.nil?)
$i += 1
$stderr.puts($i)
return l2
end
f = File.open('gnuplot.cmd', 'w')
out(f, avgs((1..100).map { dataset() }), {'da' => 'axes x1y2', 'd3a' => 'axes x1y2'})
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.