Skip to content

Instantly share code, notes, and snippets.

@x-or
Created July 3, 2014 08:56
Show Gist options
  • Save x-or/43c44776e844e280d346 to your computer and use it in GitHub Desktop.
Save x-or/43c44776e844e280d346 to your computer and use it in GitHub Desktop.
import numpy as np
from PIL import Image
def digital_reverse(n, length, base):
r = 0
for _ in range(length):
r = base*r + n % base
n /= base
return r
assert digital_reverse(123, 3, 10) == 321
assert digital_reverse(54321, 5, 10) == 12345
assert digital_reverse(0x123, 3, 16) == 0x321
assert digital_reverse(0x54321, 5, 16) == 0x12345
def save_matrix(m, fn, modulo):
image = Image.new("L", (m.shape[1], m.shape[0]), 0)
pix = image.load()
for y in xrange(m.shape[0]):
for x in xrange(m.shape[1]):
pix[x, y] = 255*m[y, x]/(modulo-1)
image.save(fn, "png")
if __name__ == '__main__':
# Image size
base = 2
power = 7
size = base**power
print "size", size
image = Image.new("RGB", (size, size), 0)
pix = image.load()
# known state after applying rule 15
reversible_state = np.zeros((size, size), np.int8)
for i in xrange(size):
ri = digital_reverse(i, power, base)
reversible_state[i, ri] = 1
#reversible_state = np.ones((size, size), np.int8)
# partially known state
state = np.zeros((size, size), np.int16)
# fill initial (known) values
state[:, 0] = np.arange(size)
state[0, :] = np.arange(size)
# find rest values of the state
for i0 in xrange(size-1):
i1 = i0 + 1
for j0 in xrange(size-1):
j1 = j0 + 1
# reverse rule 15-0
state[i1, j1] = -state[i0, j0] - state[i0, j1] - state[i1, j0] + reversible_state[i0, j0]
mn = np.min(state)
mx = np.max(state)
print "min cell", mn
print "max cell", mx
save_matrix(state-mn, "pdl-base%d-power%d-reverse15-0.png"%(base, power), mx-mn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment