Skip to content

Instantly share code, notes, and snippets.

@vznvzn vznvzn/matrix12.rb
Created Dec 20, 2017

Embed
What would you like to do?
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).to_i(2)
end
def stat(l)
l = [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, l.max.to_f, l.min.to_f
end
def stat2(l, t, n)
return Hash[[["a#{n}", "sd#{n}", "mx#{n}"], stat(l)[0..2].map { |x| x / t }].transpose]
end
def d(s)
c = s.split('').select { |x| x == '1' }.size
d = c.to_f / s.length
return d
end
def data(n)
ns = n.to_s(2)
nl = ns.length
m = nl / 2
nsh = ns[0..m]
nsl = ns[m..-1]
asdm1 = stat2(ns.split(/0+/).map { |x| x.length }, nl, 1)
l1 = ns.split(/1+/)
l1.shift
asdm0 = stat2(l1.map { |x| x.length }, nl, 0)
return {'n' => n, 'ns' => ns, 'nl' => nl, 'd' => d(ns), 'dh' => d(nsh), 'dl' => d(nsl)}.merge(asdm1).merge(asdm0)
end
def count(n)
c = 0
n1 = n
l = [n]
c2 = nil
begin
n = f2(n)
l << n
c += 1
c2 = c if (c2.nil? && n <= n1)
end while n != 1
j = (0...l.size).max_by { |x| l[x] }
return {'c' => c, 'c2' => c2, 'n' => n1, 'j' => j, 'l' => l}
end
def dist(c)
return (0...c).map { |i| count(dense(100, i.to_f / (c - 1))) }
end
def sample(n)
s = {}
n.times \
{
l = dist(500)
l.each { |x| s[x['n']] = x }
}
l2 = s.values
v = 'c2'
l1 = l2.map { |x| x[v] }
mn = l1.min
mx = l1.max * 0.50
$stderr.puts([l2.size, mn, mx].inspect)
l2.each { |x| x['z'] = (x[v].to_f - mn) / (mx - mn) }
l = []
(0..100).each \
{
|i|
d = 1.0 - i.to_f / 100
d = i.to_f / 100
j = (0...l2.size).min_by { |x| (l2[x]['z'] - d).abs }
l << l2.delete_at(j)
}
return l.sort_by { |x| -x[v] }
end
def dot(x, z)
t = z['c']
(z.keys - ['c']).each { |v| t += z[v] * x[v] }
return t
end
def detect(x0, a, l1, c)
x1 = x0.dup
i = 0
x1['nl'] = 1
loop \
{
x2 = {}
(a['v'] + ['wr']).each \
{
|vy|
x2[vy] = dot(x1, a[vy].select { |k, x| (a['v'] + ['c']).member?(k) })
return c if (x2[vy].abs > 1e10)
}
x2['nl'] = x1['nl'] * x2['wr']
x1 = x2
l1 << x1
i += 1
break if (x1['nl'] < 1 || i == c)
}
x0.merge!({'i' => i})
end
def out(fn, l)
f = File.open(fn, 'w')
k = l[0].keys
f.puts(k.join("\t"))
l.each { |x| f.puts(x.values.join("\t")) }
f.close
l = []
k.each { |x| l << "'#{fn}' using (column('#{x}')) with line title '#{x}',\\" }
return l
end
def sum(l)
t = 0.0
l.each { |x| t += x }
return t
end
def load()
raise if (ARGV[0].nil?)
d = "out/#{ARGV[0]}"
l = Dir.glob("#{d}/*").select { |x| x =~ /out\d+.txt$/}
n = l.size
i = n - 1
a = Kernel.eval(File.open("#{d}/out#{i}.txt").readlines[0])
l = a['lb'].map { |x| x.size }
raise if (sum(l) != 0)
return a
end
def add(l, z, x)
x.each \
{
|k, v|
z[k] = z.fetch(k, 0) + v
}
l << z.dup
end
a = load()
l = sample(10)
l1 = []
l2 = []
z1 = {}
z2 = {}
p(l[0]['c2'])
l[0]['l'].each_with_index \
{
|x, i|
x1 = data(x).select { |k, | !['n', 'ns', 'nl', 'mx0', 'd'].member?(k) }
x2 = {}
(a['v'] + ['wr']).each \
{
|vy|
x2[vy] = dot(x1, a[vy].select { |k, x| (a['v'] + ['c']).member?(k) })
# return c if (x2[vy].abs > 1e10)
}
add(l1, z1, x1) if (i > 0)
add(l2, z2, x2.select { |k, | !['wr'].member?(k) })
}
l3 = []
l1.size.times \
{
|i|
l3 << Hash[l1[i].keys.map { |k| [k, l1[i][k] - l2[i][k]] }]
}
lx = out('out1.txt', l1)
ly = out('out2.txt', l2)
lz = out('out3.txt', l3)
f = File.open('gnuplot.cmd', 'w')
f.puts("set title '12'; plot \\", lx + ly, '', 'pause -1')
f.puts('plot \\', lz)
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.