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] }
# cg = l.size - 1 if (cg.nil?)
cm = (0...cg).max_by { |x| l[x] }
cg1 = l.rindex { |x| x > l[0] }
# cg1 = l.size - 1 if (cg1.nil?)
n2 = l[1000]
ns2 = n2.nil? ? nil : n2.to_s(2)
return {'ns' => ns,
'cm' => cm,
'cm1' => cm1,
'cg' => cg,
'cg1' => cg1,
'ns2' => ns2,
'n2w' => ns2.nil? ? nil : ns2.length
}
end
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
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 len01(ns)
return len(ns, /0+/), len(ns, /1+/)
end
def e(ns)
return len01(ns).flatten.size.to_f / ns.length
end
def fmt1(l, f)
w = l.max_by { |x| x.length }.length
f.puts("set colors classic; set ytics nomirror; set y2tics; set key top left opaque;\\")
f.puts("plot \\")
f.puts("[0:#{l.size - 1}][0:#{w}] '-' matrix using 2:1:3 with image title '', \\")
f.puts("'-' using (column('d')) with line axes x1y2 lw 4, \\")
f.puts("'-' using 1 with line title 'e' axes x1y2 lw 4")
l1 = []
l.each_with_index \
{
|z, i|
l1 << [d(z), e(z)]
c = w - z.length
z[z.length...w] = '0' * c
f.puts(z.split('').join(' '))
}
f.puts("eof")
f.puts("eof")
f.puts("d")
l1.each { |x| f.puts(x[0]) }
f.puts("eof")
l1.each { |x| f.puts(x[1]) }
f.puts("eof")
end
def grid(l, f, t)
f.puts("set palette negative grayscale; unset colorbox; set title '#{t}'; set y2range [0.45:0.55];")
fmt1(l.map { |x| x.reverse }, f)
f.puts("reset; pause -1;")
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)
return Hash[[["a#{n}", "sd#{n}", "mx#{n}"], stat(l)].transpose]
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 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 hist(f, l, w)
f.puts('$dat << eof')
l.each_with_index \
{
|x, c|
a = {}
x[w].each \
{
|x|
a[x] = a.fetch(x, 0) + 1
}
a.sort.each \
{
|x|
f.puts([x, c].join("\t"))
}
f.puts
}
f.puts('eof')
f.puts("set title '#{w}'; plot '$dat' using 1:2:3 with line linecolor palette lw 2; pause -1;")
end
def dist()
l = []
100.times \
{
|x1|
x = count(dense(1000, 0.5))
l << x
}
l.sort_by! { |x| x['n2w' ] }
return l
end
def valuesat(a, l)
Hash[[l, a.values_at(*l)].transpose]
end
def convert(l)
x2 = nil
l2 = []
l.each_with_index \
{
|x, j|
next if (x == x2)
l2 << [j, x]
x2 = x
}
return l2
end
def scale(l, m)
l1 = l.map { |x| x[m] }
mn = l1.min
mx = l1.max
l.each { |x| x[m] = (x[m].to_f - mn) / (mx - mn) }
end
def histcurve(f, l, w)
f.puts('$dat << eof')
l.each_with_index \
{
|x, j|
l2 = convert(x[w].sort)
scale(l2, 0)
scale(l2, 1)
l2.each \
{
|x1|
f.puts([x1, j].join("\t"))
}
f.puts
}
f.puts('eof')
f.puts("set title '#{w}'; plot '$dat' using 1:2:3 with line linecolor palette lw 2; pause -1;")
end
def runs(l)
l.map \
{
|x|
l1, l0 = len01(x)
l01 = l1.dup
l0.size.times { |i| l01[i] += l0[i] }
{'l0' => l0, 'l1' => l1, 'l01' => l01}
}
end
l = dist()
f = File.open(fn = 'gnuplot.cmd', 'w')
out(f, l.map { |x| { 'n2w' => x['n2w'] } })
out(f, l.map { |x| valuesat(x, ['cm', 'cm1', 'cg', 'cg1', 'n2w']) }.select { |x| x.values.compact.size == 5 })
l2 = l.map { |x| x['ns'] }
grid(l2, f, 'grid by n2w')
l1 = runs(l2)
out(f, l1.map { |x| stat2(x['l1'], 1).merge(stat2(x['l0'], 0)).merge(stat2(x['l01'], '01')) })
['l1', 'l0', 'l01'].each { |x| hist(f, l1, x) }
['l1', 'l0', 'l01'].each { |x| histcurve(f, l1, x) }
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.