Created
November 23, 2019 02:59
-
-
Save vznvzn/de37156598d3e5667672a2f6da62b087 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
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 seq(n) | |
n1 = n | |
l = [] | |
begin | |
l << n | |
n = f2(n) | |
end while (n != 1) | |
return l | |
end | |
def fmt1(l, f) | |
w = l.max_by { |x| x.length }.length | |
f.puts("set colors classic; set ytics nomirror; set y2tics; set key top left opaque;\\") | |
f.puts("plot \\") | |
f.puts("[-0.5:#{w - 0.5}][0:#{l.size - 1}] '-' matrix using 1:2:3 with image title ''") | |
l.each_with_index \ | |
{ | |
|z, i| | |
c = w - z.length | |
z[z.length...w] = '0' * c | |
f.puts(z.split('').join(' ')) | |
} | |
f.puts("eof") | |
f.puts("eof") | |
end | |
def grid(l) | |
f = File.open(fn = "gnuplot1.cmd", 'w') | |
f.puts("set palette negative grayscale; unset colorbox;") | |
fmt1(l, f) | |
f.close | |
end | |
def outa(f, l, a = {}, t = '') | |
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("set colors classic; set key bottom right; set title '#{t}'") | |
# f.puts("set ytics nomirror; set y2tics;") | |
f.puts("plot \\") | |
k.each \ | |
{ | |
|x| | |
next if (a.member?(x) && a[x].nil?) | |
f.puts("'$dat' using (column('#{x}')) with line #{a.fetch(x, '')} lw 2,\\") | |
} | |
f.puts | |
# f.puts("reset; pause -1;") | |
end | |
l = [] | |
w = 10 | |
(1..20).each \ | |
{ | |
|i| | |
w = i * 10 | |
20.times \ | |
{ | |
ns = dense(w, 0.5) | |
l << ns.to_i(2) | |
} | |
} | |
grid(l1 = l.map { |x| x.to_s(6) }) | |
outa(f = File.open('gnuplot.cmd', 'w'), l1.map { |x| {'msb6' => x[0, 1].to_i } }) | |
f.close | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment