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 seq(n)
l = [n]
while (n != 1)
n = f2(n)
l << n
end
return l
end
def adj(x, m, n, s2, p2, p, x3)
s2.replace(p[0...x] + (x3 % 2).to_s)
m = (s2[x, 1] == p[x, 1])
n = p2.reverse.to_i(2)
p2[x, 1] = m ? '11' : '01'
return m, n, x3
end
def terras121(p)
p2 = ['01', '11'][p[0, 1].to_i]
n = 1
n3 = 0
m = p2 == '11'
x3 = (p2.reverse.to_i(2)) >> 1
(1...p.length).each \
{
|x|
if (!m)
ns = n.to_s(2)
ns[0, 1] = ''
m1 = x3 - 3**n3
n1 = f2(m1)
end
n3 = p[0...x].split('').select { |z| z == '1' }.size
s2 = ''
m, n, x3 = adj(x, m, n, s2, p2, p, m ? (f2(x3) + 3**n3) : (n1 + 3**n3))
}
n = p2.reverse.to_i(2)
return n
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 outa(f, l, a = {}, t = '')
k = outd(f, l)
f.puts("set colors classic; set title '#{t}'; ")
f.puts("set ytics nomirror; set y2tics;")
f.puts("plot \\")
k, ct = [k - ['t'], "(column('t')):"] if (k.member?('t'))
k.each \
{
|x|
next if (a.member?(x) && a[x].nil?)
opt = a.fetch(x, '')
opt += ' lw 2 ' if (!opt.include?('lw'))
opt += ' with line ' if (!opt.include?('with') && !opt.include?('pt'))
f.puts("'$dat' using #{ct}(column('#{x}')) #{opt} title '#{x}',\\")
}
f.puts
# f.puts("reset; pause -1;")
end
def outafn(l, a = {}, fno = nil, t = '')
fn = "gnuplot#{fno}.cmd"
outa(f = File.open(fn, 'w'), l, a, t)
f.close
$stderr.puts([fn, t, l.size].inspect)
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 d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def add(a, x)
a[x] = a.fetch(x, 0) + 1
end
def val(a)
return a.fetch(1, 0).to_f / a.fetch(2, 1)
end
def scan(l, a)
l.each { |x| add(a, x % 3) }
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 len01(ns)
return len1(ns), len0(ns)
end
def e(ns)
return len01(ns).flatten.size.to_f / ns.length
end
def sum(l)
return l.inject { |t, x| t + x }
end
def avg(l)
return sum(l).to_f / l.size
end
def corr(l, y1, yp = nil)
yp, ye = ["#{y1}_y", "#{y1}_e"] if (yp.nil?)
xa = avg(l.map { |x| x[y1] })
ya = avg(l.map { |x| x[yp] })
tx = ty = txy = e = 0.0
m = nil
l.each \
{
|z|
x = z[y1]
y = z[yp]
z[ye] = (x - y).abs if (!ye.nil?)
txy += (x - xa) * (y - ya)
tx += (x - xa) ** 2
ty += (y - ya) ** 2
}
d = Math.sqrt(tx) * Math.sqrt(ty)
$z = 1e-4
r = txy / (d.abs < $z ? $z : d)
return r
end
def line(l)
l1 = l.map { |x| x.to_s(2).length }
mn = l1.min
mx = l1.max
t1 = t2 = 0
l1.size.times \
{
|x|
y1 = (l1[x].to_f - mn) / (mx - mn)
y2 = x.to_f / (l1.size - 1)
t1 += (y1 - y2)
t2 += (y1 - y2).abs
}
return {
# 't1' => t1.to_f / l1.size,
# 't2' => t2 / l1.size
}
end
c1 = 500
w = 200
l1 = []
c1.times \
{
|i|
r = i.to_f / (c1 - 1)
p = dense(w, r)
n = terras121(p)
scan((l = seq(n))[0...w], a = {})
e = e(p)
eq = (r)*(r - 1.0) * 2
e1 = e + eq
l1 << line(l[0...w]).merge({
'e' => e,
'a21' => 1.0 - val(a) - r,
'eq' => -eq,
'e1' => e1,
})
}
outafn(l1)
f = File.open('gnuplot.cmd', 'a')
f.puts('pause -1;')
f.puts("plot $dat using (column('e1')):(column('a21')) pt 5 title 'a21 vs e1'")
f.close
$stderr.puts(corr(l1, 'e1', 'a21'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment