Created
December 29, 2020 03:40
-
-
Save vznvzn/fcf10a1f95cb687c7b24183537fbf66f 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 seq(n) | |
l = [n] | |
while (n != 1) | |
n = f2(n) | |
l << n | |
end | |
return l | |
end | |
def outd(f, l) | |
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') | |
return k | |
end | |
def hide(a, x) | |
return (a.member?(x) && a[x].nil?) | |
end | |
def plot(f, d, k, a, t = nil) | |
f.puts("# #{t}") if (!t.nil?) | |
f.puts("set colors classic; set title '#{t}'; ") | |
# f.puts("set key top right opaque; ") | |
f.puts("set ytics nomirror; set y2tics;") | |
f.puts("plot \\") | |
k.each \ | |
{ | |
|x| | |
next if (hide(a, x)) | |
opt = a.fetch(x, '') | |
opt = ' with line ' + opt if (!opt.include?('with') && !opt.include?('pt')) | |
opt += ' lw 2 ' if (!opt.include?('lw') && !opt.include?('pt')) | |
f.puts("#{d} using (column('#{x}')) #{opt} title '#{x}',\\") | |
} | |
f.puts | |
# f.puts("reset; pause -1;") | |
end | |
def outa(f, l, a = {}, t = nil) | |
k = outd(f, l) | |
plot(f, '$dat', k, a, t) | |
end | |
def outafn(l, a = {}, fno = nil, t = '') | |
outa(f = File.open(fn = "gnuplot#{fno}.cmd", 'w'), l, a, t) | |
f.close | |
$stderr.puts([fn, l.size, t, l[0].keys].inspect) | |
end | |
def read(fn) | |
l = (f = File.open(fn)).readlines.map { |x| Kernel.eval(x) } | |
f.close | |
$stderr.puts(['read', fn, l.size].inspect) | |
return l | |
end | |
def cg(l2) | |
cg = l2.index { |x| x < l2[0] } | |
return cg | |
end | |
def len(ns, p) | |
l = ns.split(p) | |
l = [] if (l.nil?) | |
l.shift if (l[0] == '') | |
return l.map { |x| x.length } | |
end | |
def len1(ns) | |
return len(ns, /0+/) | |
end | |
def len0(ns) | |
return len(ns, /1+/) | |
end | |
def sum(l) | |
return l.inject(0) { |t, x| t + x } | |
end | |
def avg(l) | |
return sum(l).to_f / l.size | |
end | |
def features(ns) | |
l0 = len0(ns) | |
a0 = l0.empty? ? 0 : avg(l0) | |
a1 = avg(l1 = len1(ns)) | |
a01 = avg(l0 + l1) | |
return { | |
'a0' => a0, | |
'a1' => a1, | |
'a01' => a01, | |
'a0s' => a0 / ns.length, | |
'a1s' => a1 / ns.length, | |
'a01s' => a01 / ns.length, | |
} | |
end | |
def axes2(x2) | |
return x2.inject({}) { |h, x| h.merge({x => 'axes x1y2' }) } | |
end | |
l1 = read('outline2.txt') | |
k = 'hc' | |
l1.each \ | |
{ | |
|x| | |
c = (l = seq(x['ns'].to_i(2))).size | |
x[k] = c.to_f / x['ns'].length | |
x['c'] = c | |
x['cg'] = cg(l) | |
x['l'] = l | |
} | |
x = l1.max_by { |x| x['cg'] } | |
l = x.delete('l') | |
p(x) | |
outafn(l.map { |x| features(x.to_s(2)) }, axes2(['a0s', 'a1s', 'a01s'])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment