Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created November 26, 2019 23:08
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save vznvzn/009db9fd4b8ee4055f4287cb42fd004a to your computer and use it in GitHub Desktop.
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:#{w}][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.map { |x| x.reverse }, f)
f.close
end
def stat(k, l)
t = l.inject { |a, x| a + x }
t2 = l.inject(0) { |a, x| a + (x ** 2) }
c = l.size
a = t.to_f / c
z = t2.to_f / c - a ** 2
sd = Math.sqrt(z < 0 ? 0 : z)
raise if (sd.nan?)
return {"#{k}_a" => a, "#{k}_s" => sd}
end
def msd(l, w)
l2 = []
p(w)
l.each \
{
|l1|
x1 = l1[0].to_s(2).length
x2 = l1[w].to_s(2).length
x3 = l1[-1].to_s(2).length
m1 = (x2.to_f - x1) / w
m2 = (x3.to_f - x2) / (l1.size - w)
l2 << {'m1' => m1, 'm2' => m2}
}
a = {}
l2[0].keys.each { |k| a.merge!(stat(k, l2.map { |x| x[k] })) }
return a
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 top 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
def out1(l)
f = File.open('gnuplot1.cmd', 'w')
f.puts('$dat << eof')
l.each_with_index \
{
|l1, c|
l1.each_with_index { |x, j| f.puts([j, Math.log(x), c].join("\t")) }
f.puts
}
f.puts('eof')
f.puts('plot $dat using 1:2:3 with line linecolor palette')
f.close
end
def sample(l, c)
return l if (l.size < c)
r = l.size / c
l2 = *(0...l.size).select { |x| x % r == 0 }
l2[c..-1] = []
return l.values_at(*l2)
end
l = []
500.times \
{
ns = dense(100, 0.5)
l << seq(ns.to_i(2))
}
out1(sample(l.sort_by { |x| x.size }, 100))
outa(f = File.open('gnuplot.cmd', 'w'), (1..20).map { |x| msd(l, x * 5) })
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment