Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created October 21, 2020 18:20
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/cccb00cb61602a7b518e40cda7538ea0 to your computer and use it in GitHub Desktop.
Save vznvzn/cccb00cb61602a7b518e40cda7538ea0 to your computer and use it in GitHub Desktop.
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 len1(ns)
return len(ns, /0+/)
end
def len0(ns)
return len(ns, /1+/)
end
def len01(ns)
return len1(ns), len0(ns)
end
def log2(x)
return Math.log(x) / Math.log(2.0)
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 hide(a, x)
return (a.member?(x) && a[x].nil?)
end
def outa(f, l, a = {}, t = '')
k = outd(f, l)
k1 = "key top left opaque"
f.puts("set colors classic; set #{k1}; set title '#{t}'; ")
f.puts("set ytics nomirror; set y2tics;")
f.puts("plot \\")
ct = ''
k, ct = [k - ['t'], "(column('t')):"] if (k.member?('t') && !hide(a, 't'))
k.each \
{
|x|
next if (hide(a, x))
opt = a.fetch(x, '')
opt = ' with line ' + opt if (!opt.include?('with'))
opt += ' lw 2 ' if (opt.include?('line') && !opt.include?('lw'))
f.puts("'$dat' using #{ct}(column('#{x}')) #{opt} title '#{x}',\\")
}
f.puts
# f.puts("reset; pause -1;")
end
def outafn(l, a = {}, fn = nil, t = '')
fn = 'gnuplot.cmd' if (fn.nil?)
outa(f = File.open(fn, 'w'), l, a, t)
f.close
$stderr.puts([fn, l.size, t].inspect)
end
l = []
(10..1000).each \
{
|w|
ns = dense(w, 0.5)
n = ns.to_i(2)
mx01 = len01(ns).flatten.max
mxs1 = mx01 / log2(w)
mxs2 = mx01 / log2(log2(n))
mxs12 = mxs1 - mxs2
l << {'t' => w, 'mxs1' => mxs1, 'mxs2' => mxs2, 'mxs12' => mxs2 - mxs1}
}
outafn(l, {'mxs2' => 'dt 3', 'mxs12' => 'axes x1y2'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment