Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created February 18, 2019 21:27
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/91051657f262b3e4a69ffd96c99ca7e8 to your computer and use it in GitHub Desktop.
Save vznvzn/91051657f262b3e4a69ffd96c99ca7e8 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 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 len(ns, p)
l = ns.split(p)
l = [''] if (l.nil? || l.empty?)
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 stat(l)
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 avg(l)
l.inject { |t, x| t + x }.to_f / l.size
end
def avgs(l)
ks = l[0][0].keys
l1 = []
l[0].size.times \
{
|i|
l1[i] = Hash[[ks, ks.map { |k| avg(l.map { |x| x[i][k] }) }].transpose]
}
return l1
end
def dataset()
l = []
t = 50
t.times \
{
|c|
d = c.to_f / (t - 1)
ps = dense(200, d)
n = terras121(ps)
le = []
ld = []
200.times \
{
raise if (n == 1)
ns = n.to_s(2)
le << e(ns)
ld << d(ns)
1.times { n = f2(n) }
}
a = {'d1' => d,
'e' => le,
'd' => ld
}
l << a
}
return l
end
def gp(l, t)
f = File.open($gp, 'a')
f.puts('$dat << eof')
l.each { |x| f.puts(x.join("\t")) }
f.puts('eof')
f.puts("set title '#{t}'")
f.puts("plot $dat using 1:2:3 with line linecolor palette")
f.puts("pause -1")
f.close
end
def review(l, k)
l2 = (0...l[0].size).map \
{
|j|
l1 = l.map { |x| x[j][k] }
l1.transpose.map { |x| avg(x) }
}
l3 = []
l2.each_with_index \
{
|l, c|
l.each_with_index { |x, j| l3 << [j, x, c] }
l3 << []
}
gp(l3, k)
end
y = 100
l = (1..y).map { dataset() }
File.open($gp = 'gnuplot.cmd', 'w').close
review(l, 'd')
review(l, 'e')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment