Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created August 7, 2020 16:58
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/0f8d8b59b00716b55abf777de53b56e0 to your computer and use it in GitHub Desktop.
Save vznvzn/0f8d8b59b00716b55abf777de53b56e0 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 hist(l)
a = {}
l.each { |x| a[x] = a.fetch(x, 0) + 1 }
return a
end
def hdiff(a, b)
d = 0
(a.keys | b.keys).each { |x| d += (a.fetch(x, 0) - b.fetch(x, 0)).abs }
return d
end
def hdiff1(ns, a = '')
a0, a1 = $h[ns.length]
l1, l0 = len01(ns)
b0, b1 = [hist(l0), hist(l1)]
return {"hd#{a}" => hdiff(a0, b0) + hdiff(a1, b1), "mx01#{a}" => (l0 + l1).max}
end
def hdiff2(ns)
$h = {} if ($h.nil?)
if (!$h.member?(nw = ns.length)) then
ns2 = dense(nw, 0.5)
l1, l0 = len01(ns2)
$h[nw] = [hist(l0), hist(l1)]
end
return hdiff1(ns)
end
def hadd(a, b)
(a.keys | b.keys).each { |x| a[x] = a.fetch(x, 0) + b.fetch(x, 0) }
end
def hdiv(a, d)
a.keys.each { |x| a[x] = a[x].to_f / d }
end
def hdiffavg(w)
t0 = {}
t1 = {}
(c = 3).times \
{
$h.delete(w)
hdiff2(dense(w, 0.5))
a0, a1 = $h[w]
hadd(t0, a0)
hadd(t1, a1)
}
hdiv(t0, c)
hdiv(t1, c)
$h[w] = [t0, t1]
return hdiff1(dense(w, 0.5), 'a')
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 outa(f, l, a = {}, t = '')
k = outd(f, l)
f.puts("set colors classic; set key top left opaque; set title '#{t}'; ")
f.puts("set ytics nomirror; set y2tics;")
f.puts("plot \\")
k, ct = [k - ['t'], "(column('t')):"] if (k.member?('t'))
k.each \
{
|x|
next if (a.member?(x) && a[x].nil?)
opt = a.fetch(x, '')
opt += ' lw 2 ' if (!opt.include?('lw'))
opt += ' with line ' if (!opt.include?('with') && !opt.include?('pt'))
f.puts("'$dat' using #{ct}(column('#{x}')) #{opt} title '#{x}',\\")
}
f.puts
# f.puts("reset; pause -1;")
end
def outafn(l, a = {}, fn = nil)
outa(f = File.open(fn.nil? ? 'gnuplot.cmd' : fn, 'w'), l, a)
f.close
end
l = []
(1..1000).each \
{
|w|
a = hdiff2(dense(w, 0.5))
a1 = hdiffavg(w)
l << a.merge(a1)
$stderr.puts(w) if (w % 100 == 0)
}
outafn(l, {'mx01' => 'axes x1y2 dt 2', 'hda' => 'dt 3', 'mx01a' => nil})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment