Created
July 17, 2020 04:42
-
-
Save vznvzn/28df7a6fec1070841ac554f59929859d 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
#!/usr/bin/ruby | |
def f2(n) | |
return n.odd? ? (n * 3 + 1) / 2 : n / 2 | |
end | |
def seqg(n) | |
n1 = n | |
l = [n1] | |
while (n != 1 && n >= n1) | |
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 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 empty(l) | |
return l.nil? || l.empty? | |
end | |
def data(l) | |
nw = l[0].to_s(2).length | |
cm = (0...l.size).max_by { |x| l[x] } | |
cmnw = cm - nw | |
l1 = l[nw..cm] | |
l2 = l[cm..-1] | |
# return nil if (empty(l1) || empty(l2)) | |
return {'nw' => nw, | |
'cm' => cm, | |
'cg' => l.size, | |
'cmnw' => cmnw, | |
'cgcm' => l.size - cm, | |
'cgnw' => l.size - nw, | |
'l' => l | |
} | |
end | |
def glides(t, w = nil) | |
l1 = [] | |
e = 0.64 | |
c1 = c2 = 0 | |
s = [] | |
t.times \ | |
{ | |
|i| | |
if ((i + 1) % 10 == 0 && s[i].nil?) then | |
$stderr.puts([i + 1, c1, c2].inspect) | |
c1 = c2 = 0 | |
end | |
nw, d = [i, e + rand() * (1.0 - e)] | |
nw, d = [w, e + i.to_f / t * (1.0 - e)] if (!w.nil?) | |
p = dense(nw + 1, d) | |
n = terras121(p) | |
l = seqg(n) | |
s[i] = s.fetch(i, 0) + 1 | |
if (l.size < n.to_s(2).length) then | |
c1 += 1 | |
next if (s[i] == 20) | |
redo | |
end | |
x = data(l) | |
if (x.nil?) then | |
c2 += 1 | |
next if (s[i] == 20) | |
redo | |
end | |
l1 << x.merge({'t' => i}) | |
} | |
return l1 | |
end | |
def ext(x) | |
l = x['l'] | |
n = l[-1] | |
while (n != 1) | |
n = f2(n) | |
l << n | |
end | |
x['c'] = l.size | |
end | |
def outa1(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') | |
f.puts | |
end | |
def outafn1(l, fn = nil) | |
outa1(f = File.open(fn.nil? ? 'gnuplot.cmd' : fn, 'w'), l) | |
return f | |
end | |
l = glides(50, 50) | |
l.map { |x| ext(x) } | |
l1 = ['nw', 'cm', 'cg', 'cgcm', 'c'] | |
l2 = [] | |
l.each_with_index \ | |
{ | |
|x, j| | |
a = Hash[*l1.map { |x2| [x2, -x[x2]] }.flatten] | |
x['l'].each_with_index \ | |
{ | |
|x1, i| | |
l2 << a.merge({'x' => Math.log(x1), 'j' => j}) | |
a.keys.each { |k| a[k] += 1 } | |
} | |
l2 << {} | |
} | |
f = outafn1(l2) | |
l1.each \ | |
{ | |
|x| | |
ln = "plot $dat using (column('#{x}')):(column('x')):(column('j')) with line linecolor palette lw 2; pause -1;" | |
f.puts(ln) | |
} | |
f.close | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment