Skip to content

Instantly share code, notes, and snippets.

@vznvzn

vznvzn/review174.rb

Created Jan 1, 2021
Embed
What would you like to do?
def f2(n)
return n.odd? ? (n * 3 + 1) / 2 : n / 2
end
def seq(n)
l = [n]
while (n != 1)
n = f2(n)
l << n
end
return l
end
def outin(fn)
l = (f = File.open(fn)).readlines
f.close
raise if (l.shift.chop != '$dat << eof')
k = l.shift.split
i = l.index("eof\n")
raise if (i.nil?)
l[i..-1] = []
l2 = l.map { |x| Hash[[k, x.split.map { |x| #x.sub!('NaN', '0');
Kernel.eval(x) }].transpose] }
l2.each { |x| x['ns'] = x['ns'].to_s }
$stderr.puts([fn, l2.size].inspect)
return l2
end
def sample2(l, n)
d = (l.size - 1).to_f / n
l2 = []
(n + 1).times \
{
|i|
j = (i * d).to_i
l2 << l[j]
}
return l2
end
def load(l1 = [''])
l = []
l1.each \
{
|i|
l.concat(outin("gnuplot#{i}-1.cmd"))
}
return l
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')
$stderr.puts([f, l.size].inspect)
return k
end
def log2(x)
return Math.log(x) / Math.log(2.0)
end
def seqs(f, l1, k2, k = nil)
l2 = []
kc = "i-#{k.nil? ? 0 : k}"
l1.each_with_index \
{
|x, j|
n = x['ns'].to_i(2)
l = seq(n)
l.each_with_index { |x1, i| l2 << {kc => i - (k.nil? ? 0 : x[k]), 'w' => log2(x1), k2 => j} }
l2 << {}
}
outd(f, l2)
f.puts("plot $dat using (column('#{kc}')):(column('w')):(column('#{k2}')) with line linecolor palette title '#{kc}:w:#{k2}';")
f.puts("pause -1;")
end
def recalc(l1)
l1.each \
{
|x|
n = x['ns'].to_i(2)
l = seq(n)
x['cm1'] = (0...l.size).max_by { |x| l[x] }
x['cg1'] = l.rindex { |x| x >= l[0] }
}
end
l = load()
l1 = true ? sample2(l, 40) : l[-50..-1]
f = File.open('gnuplot5.cmd', 'w')
k = 'hci'
seqs(f, l1, k)
seqs(f, l1, k, 'cm')
seqs(f, l1, k, 'cg')
seqs(f, l1, k, 'c')
recalc(l1)
seqs(f, l1, k, 'cm1')
seqs(f, l1, k, 'cg1')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment