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 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 dense2(w, d)
b1 = (d >= 0.5) ? 1 : 0
c1 = b1
c1 = 1
s = c1.to_s
(1...w).each \
{
|w1|
b = [0, b1, 1][(d <=> c1.to_f / w1) + 1]
s << b.to_s
c1 += b
}
return s
end
def dense3(w, d)
c = [(d * w - 1).to_i, 0].max
c2 = w - 1 - c
return '1' + (('0' * c2) + ('1' * c)).reverse
end
def dense4(w, d)
c = [(d * w - 1).to_i, 0].max
c2 = w - 1 - c
c2d = c2.to_f / 2
c2a = c2d.floor
c2b = c2d.ceil
return '1' + (('0' * c2a) + ('1' * c) + ('0' * c2b))
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 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 dataset()
t = 20
lo = 0.5
hi = 1.0
t.times \
{
|c|
$stderr.puts(c)
d = lo + c.to_f / (t - 1) * (hi - lo)
l1b = []
100.times \
{
ps = dense(1000, d)
n = terras121(ps)
ns = n.to_s(2)
l1, l0 = len01(ns)
l1b += l1
}
l = convert(l1b.sort)
# l = convert(l0.sort)
scale(l, 0)
scale(l, 1)
l.each \
{
|x, y|
puts([x, y, c].join("\t"))
}
puts
}
end
dataset()
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.