Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created June 24, 2016 01:59
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/28b8ea125599434c454acad9184d5d11 to your computer and use it in GitHub Desktop.
Save vznvzn/28b8ea125599434c454acad9184d5d11 to your computer and use it in GitHub Desktop.
def f1(n)
n = (n * 3 + 1) / 2 while (n % 2 == 1)
n /= 2 while (n % 2 == 0)
return n
end
def stat(l)
return 0, 0 if (l.empty?)
t = t2 = 0
l.each \
{
|x|
t += x
t2 += x ** 2
}
c = l.size
a = t.to_f / c
z = t2.to_f / c - a ** 2
sd = Math.sqrt(z < 0 ? 0 : z)
return a, sd
end
def stat2(l)
a, sd = stat(l)
mn, mx = [l.min, l.max]
mn, mx = [0, 0] if (l.empty?)
return a, sd, mn, mx
end
def metrics(n, j)
s = n.to_s(2)
d1, d2, w, c1, c0 = d(s)
m = s.length / 2
da = d2(s[0...m])
db = d2(s[m..-1])
# m1 = w ** 2
# m2 = w ** 0.5
asdmm1 = stat2(s.split(/0+/).map { |x| x.length })
l = s.split(/1+/)
l.shift
asdmm0 = stat2(l.map { |x| x.length })
# 0 1 2 3 4
l2 = [w, d1, d2, c0, c1]
# (5 6 7 8) (9 10 11 12)
l2.concat(asdmm1 + asdmm0)
# (13 14) (15 16)
l2.concat(da + db)
raise if (l2.compact.size != l2.size)
return l2
end
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d, (0.5 - d).abs, s.length, c, s.size - c
end
def d2(s)
return [0, 0] if (s.length == 0)
d(s)[0..1]
end
def seq(z)
n = z['n']
l = [n]
while (n != 1)
n = f1(n)
l << n
end
return l
end
def mono(l, z)
return if (l.empty?)
c = 0
m = l[0]
mx = nil
j = 0
l2 = []
(1...l.size).each \
{
|i|
c += 1 if (l[i] < l[i - 1])
if (l[i] < m) then
l2 << i - j
m, j = [l[i], i]
end
mx = [mx, i - j].compact.max
}
return if (c == 0 || mx.nil? || l2.empty?)
r = c.to_f / l.size
a, sd = stat(l2)
return {'r' => r, 'mx' => mx, 'c' => l2.size,
'd' => l2.size.to_f / l.size, 'a' => a, 'sd' => sd,
'mn' => l.min.abs, 'cn' => l.select { |x| x < 0 }.size.to_f / l.size
}.merge(z)
end
l = File.open("db.txt").readlines
l3 = []
l.each_with_index \
{
|z, t|
x = Kernel.eval(z)
l = seq(x)
l1 = []
l.each_with_index \
{
|n, j|
l1 << metrics(n, j)
}
l = []
l1[0].size.times \
{
|j|
l2 = l1.map { |x| x[j] }
z = mono(l2, {})
mx = 0
mx = z['mx'] if (!z.nil?)
l << mx
}
l3 << l
$stderr.puts(t + 1) if ((t + 1) % 25 == 0)
}
cmd1 = ''
cmd2 = ''
f2 = File.open('out2.txt', 'w')
l3[0].size.times \
{
|i|
m = l3.map { |x| x[i] }.max
l3.each { |x| x[i] = x[i].to_f / m }
cmd1 += "'out.txt' using ($#{i + 1}+#{i}) with line, "
cmd2 += "'out.txt' using ($#{i + 1}) with line, "
l3.each_with_index \
{
|x, j|
f2.puts([j, x[i], i].join("\t"))
}
f2.puts
f2.flush
}
f2.close
f = File.open('out.txt', 'w')
l3.each { |x| f.puts(x.join("\t")) }
f.close
f = File.open('gp.cmd', 'w')
f.puts('plot ' + cmd1 + "\npause mouse")
f.puts('plot ' + cmd2 + "\npause mouse")
f.puts("plot 'out2.txt' using 1:2:3 with line linecolor palette\npause mouse")
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment