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 d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def keys(ks2)
ksmn = ks2.select { |x| x.end_with?('-') }
ksmx = ks2 - ksmn
ksmn.each { |x| x.replace(x.chop) }
x = {'ks' => ksmn + ksmx, 'ksmx' => ksmx, 'ksmn' => ksmn}
$stderr.puts(x.inspect)
return x
end
def order(l, ks2)
l2 = (0...l.size).map { {} }
$k = keys(ks2) if ($k.nil?)
$k['ks'].each \
{
|k|
l1 = (0...l.size).map { |x| {k => l[x][k], 'i' => x} }
s = $k['ksmx'].member?(k) ? -1 : 1
l1.sort_by! { |x| s * x[k] }
(0...l1.size).each { |x| l2[l1[x]['i']][k] = x }
}
l2.each { |x| x['z'] = x.values.inject(0) { |t, x| t + x } }
return (0...l2.size).sort_by { |x| l2[x]['z'] }
end
def out(a, a2 = {}, t = nil, ct = "(column('t')):")
fn = $fn.nil? ? 'out.txt' : $fn
k = a.keys
if ($f1.nil?) then
$f1 = File.open(fn, 'w')
$f1.puts(k.join("\t"))
f2 = File.open('gnuplot.cmd', 'w')
st = t.nil? ? 'unset title; ' : "set title '#{t}'; "
f2.puts("set ytics nomirror; set y2tics; set colors classic; set key top left box opaque; #{st}; plot \\")
(a.keys - ['t']).each \
{
|x|
next if (a2.member?(x) && a2[x].nil?)
m = a2.fetch(x, '')
m += ' with line' if (!m.include?('with'))
f2.puts("'#{fn}' using #{ct}(column('#{x}')) title '#{x}' #{m}, \\")
}
f2.puts
f2.close
end
$f1.puts(a.values_at(*k).join("\t"))
$f1.flush
end
def remove(a, ks)
a = a.dup
ks.each { |x| a.delete(x) }
return a
end
def seq(n)
n1 = n
l = []
begin
l << n
n = f2(n)
end while (n != 1)
return l
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 mx(l2)
return l2.empty? ? 0 : l2.map { |x| len01x(x.to_s(2)) }.flatten.max
end
def count(ns)
nw = ns.length
l = seq(ns.to_i(2))
cm1 = (0...l.size).max_by { |x| l[x] }
cg = l.index { |x| x < l[0] }
cg = l.size if (cg.nil?)
cm = (0...cg).max_by { |x| l[x] }
c = l.size
mxl = mx(l[0..cm])
mxr = mx(l[cm..cg])
return {
'nw' => nw,
'cm' => cm,
'cm1' => cm1,
'cg' => cg,
'c' => c,
'mxl' => mxl,
'mxr' => mxr,
'mxrl' => mxr - mxl,
'l' => l,
'ns' => ns,
'n' => ns.to_i(2)
}
end
def fmt(x)
return remove(x, ['l'])
end
def axes2(x2)
return x2.inject({}) { |h, x| h.merge({x => 'axes x1y2' }) }
end
def add(l, ns, x2)
$t = 0 if ($t.nil?)
$t += 1
x = count(ns).merge({'t' => $t})
l << x
x = fmt(x)
out(remove(x, ['ns']), x2) if ($t % $d == 0)
$stderr.puts(x.inspect) if ($t % 200 == 0)
end
def adj(ns)
ns1 = ns.dup
ns1[0, 1] = '10'
ns2 = ns.dup
ns2[0, 1] = '11'
return ns1, ns2
end
def opt(ks, c, x2)
$d = c / 2000
add(l = [], '1', x2)
(c / 2).times \
{
l1 = order(l, ks)
i = l1[rand([l1.size, 50].min)]
x = l.delete_at(i)
ns1, ns2 = adj(x['ns'])
add(l, ns1, x2)
add(l, ns2, x2)
}
return l[order(l, ks)[0]]
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 stat(l)
return 0, 0, 0 if (l.empty?)
t = l.inject { |a, x| a + x }
t2 = l.inject { |a, x| a + x ** 2 }
c = l.size
a = t.to_f / c
z = t2.to_f / c - a ** 2
sd = Math.sqrt(z < 0 ? 0 : z)
raise if (sd.nan?)
return a, sd, l.max
end
def stat2(l, n, m)
return Hash[[["#{m}a#{n}", "#{m}sd#{n}", "#{m}mx#{n}"], stat(l)].transpose]
end
def stat3(x, t)
l1, l0 = len01x(x)
return stat2(l1, "1", t).merge(stat2(l0, "0", t))
end
def hist(l, f)
h = {}
l.each \
{
|x|
h[x] = h.fetch(x, 0) + 1
}
$c = 0 if ($c.nil?)
h.sort.each \
{
|x|
f.puts([x, $c].join("\t"))
}
f.puts
$c += 1
end
def dissect(x, f)
l1, l0 = len01x(x)
hist(l1, f)
hist(l0, f)
end
def review()
l = File.open('out.txt').readlines
c = l[0].split.index('n')
l2 = l[1..-1].map { |ln| ln.split[c].to_i }
f = File.open('out2.txt', 'w')
l2[-1..-1].each \
{
|x|
l = seq(x)
cm1 = (0...l.size).max_by { |x| l[x] }
cg = l.index { |x| x < l[0] }
cg = l.size if (cg.nil?)
cm = (0...cg).max_by { |x| l[x] }
l2 = l.map { |x| x.to_s(2) }
w = [cm, cg - cm].min
$stderr.puts([cm, cg, w].inspect)
ll = l2[0..w].join
lr = l2[cm..(cm + w)].join
dissect(ll, f)
dissect(lr, f)
}
f.close
exit
end
review() if (File.exist?('out.txt'))
ks = ['nw', 'cm', 'cg', 'c', 'mxl-', 'mxr-']
x2 = axes2(['mxl', 'mxr', 'mxrl'])
x2['mxrl'] += ' with impulse'
x2['n'] = nil
x = opt(ks, 1e4.to_i, x2)
$stderr.puts(fmt(x).inspect)
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.