Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created July 19, 2020 05:05
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/6bc13bed903bad087cd405cb29ebb13a to your computer and use it in GitHub Desktop.
Save vznvzn/6bc13bed903bad087cd405cb29ebb13a to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
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 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 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 convert1(l, x)
l << convert(len(x, /0+/).sort)
end
def outd(f, l, n = '')
f.puts("$dat#{n} << 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 triplets(l1, f, n)
l2 = []
l1.each_with_index \
{
|l, t|
l.each { |x, y| l2 << {'x' => x, 'y' => y, 't' => t}}
l2 << {}
}
outd(f, l2, n)
end
ns = dense(200, 0.5)
l1 = []
l2 = []
500.times \
{
|t|
convert1(l1, ns)
convert1(l2, dense(ns.length, 0.5))
n = ns.to_i(2)
n = f2(n)
ns = n.to_s(2)
}
$stderr.puts(ns.length)
f = File.open('gnuplot.cmd', 'w')
triplets(l1, f, 1)
triplets(l2, f, 2)
f.puts("plot $dat1 using (column('x')):(column('y')):(column('t')) with line linecolor palette lw 2," +
"$dat2 using (-column('x')):(column('y')):(column('t')) with line linecolor palette lw 2")
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment