Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created December 30, 2020 03:49
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/47d1a735245b2abb88f97eebd9f1295c to your computer and use it in GitHub Desktop.
Save vznvzn/47d1a735245b2abb88f97eebd9f1295c to your computer and use it in GitHub Desktop.
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
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 seq(n)
l = [n]
while (n != 1)
n = f2(n)
l << n
end
return l
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 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 d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def sum(l)
return l.inject { |t, x| t + x }
end
def avg(l)
return l.empty? ? 0 : sum(l).to_f / l.size
end
def out(a)
if ($x.nil?) then
$x = true
puts(a.keys.join("\t"))
else
puts(a.values.join("\t"))
end
$stdout.flush
end
def a1(ns)
avg(len1(ns))
end
def a1m(ns)
len1(ns).max.to_f / ns.length
end
def dlo(ns)
nw2 = ns.length / 2
d(ns[nw2..-1])
end
def elo(ns)
nw2 = ns.length / 2
e(ns[nw2..-1])
end
def ehi(ns)
nw2 = ns.length / 2
e(ns[0..nw2])
end
def corr(l, y1, yp = "#{y1}_y")
ye = "#{y1}_e"
xav = avg(l.map { |x| x[y1] })
yav = avg(l.map { |x| x[yp] })
tx = ty = txy = e = 0.0
m = nil
l.each \
{
|z|
x = z[y1]
y = z[yp]
txy += (x - xav) * (y - yav)
tx += (x - xav) ** 2
ty += (y - yav) ** 2
z[ye] = (x - y)
e1 = z[ye]
e += e1.abs
}
d = Math.sqrt(tx) * Math.sqrt(ty)
z = 1e-4
r = txy / (d.abs < z ? z : d)
e /= l.size
return r
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
if (false) then
p = dense(500, 0.64)
n1 = terras121(p)
else
n1 = dense(500, 0.5).to_i(2)
end
l = seq(n1)
l2 = l.map { |x| d(x.to_s(2)) }
l1 = []
(0...(l2.size - 1)).each_with_index \
{
|i, j|
l1 << {'d1' => l2[i], 'd2' => l2[i + 1], 'j' => j}
}
r = corr(l1, 'd1', 'd2')
$stderr.puts(r)
f = File.open('gnuplot.cmd', 'w')
outd(f, l1)
f.puts("plot $dat using (column('d1')):(column('d2')):(column('j')) with point pt 5 ps 0.5 palette")
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment