Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created October 10, 2018 01:29
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/45d644f97796153310fee2f3fed2ebe9 to your computer and use it in GitHub Desktop.
Save vznvzn/45d644f97796153310fee2f3fed2ebe9 to your computer and use it in GitHub Desktop.
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
end
def len01(ns)
nl = ns.length
l1 = ns.split(/0+/).map { |x| x.length }
l2 = ns.split(/1+/)[1..-1]
l2 = [''] if (l2.nil?)
l0 = l2.map { |x| x.length }
return l0, l1
end
def count(x, l = nil)
n = n1 = x['n']
l = [] if (l.nil?)
l << n
while (n != 1)
n = $f.call(n)
l << n
end
end
def sample(l, c)
r = l.size / c
l2 = *(0...l.size).select { |x| x % r == 0 }
l2[c..-1] = []
$stderr.puts({'l' => l.size, 'l2' => l2.size}.inspect)
return l.values_at(*l2)
end
def KT()
return [461030156909527021089206418427,
482864760756452064016217195591,
1043622266875375479678659421311,
441601129862460961268061618843,
588458712014604930003389655503].map { |x| {'n' => x} }
end
$f = lambda { |x| f2(x) }
fns = ['tmp/bitwise8/out/10/mixdb.txt',
'tmp/bitwise9/mixdb_a.txt',
'tmp/bitwise9/mixdb_mx.txt',
'KT5']
fn = fns[ARGV[0].to_i]
l = (fn == 'KT5') ? KT() : sample(File.open(fn).readlines.map { |x| Kernel.eval(x) }, 100)
$stderr.puts({'fn' => fn, 'c' => l.size}.inspect)
acg = {}
acm = {}
l.each \
{
|x|
l = []
count(x, l)
(0...(l.size - 1)).each \
{
|i|
l1 = l[i..-1]
cg = l1.index { |x| x < l1[0] }
cm1 = (0...l1.size).max_by { |x| l1[x] }
len01(l1[0].to_s(2))[1].each \
{
|x|
p1 = [x, cg]
p2 = [x, cm1]
acg[p1] = acg.fetch(p1, 0) + 1
acm[p2] = acm.fetch(p2, 0) + 1
}
}
}
f = File.open('gnuplot.cmd', 'w')
f.puts("set title 'cm1/cg vs mx -- #{fn}'; plot [-20:20][0:700] '-' using (column('mx')):(column('x')):(log(column('c'))) with points lt palette pt 7 ps 0.5")
f.puts("mx x c")
[acg, acm].each_with_index \
{
|a, i|
a.sort_by { |k, v| v }.each \
{
|k, v|
k[0] *= -1 if (i == 1)
f.puts([k, v].join("\t"))
}
}
f.puts("eof")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment