Unflattening
parent
1b2bb74198
commit
0145165649
|
@ -55,7 +55,7 @@ class Neighborhood:
|
||||||
if plane.N > 0:
|
if plane.N > 0:
|
||||||
f_offsets = list(map(plane.flatten, offsets))
|
f_offsets = list(map(plane.flatten, offsets))
|
||||||
for i in range(len(plane.bits)):
|
for i in range(len(plane.bits)):
|
||||||
neighborhood = Neighborhood(i)
|
neighborhood = Neighborhood(plane.unflatten(i))
|
||||||
for j in range(len(f_offsets)):
|
for j in range(len(f_offsets)):
|
||||||
neighborhood.neighbors.append(plane.bits[j])
|
neighborhood.neighbors.append(plane.bits[j])
|
||||||
plane.bits[j] += 1
|
plane.bits[j] += 1
|
||||||
|
@ -91,12 +91,12 @@ class Neighborhood:
|
||||||
We can then find the total of the ith neighborhood by checking the sum of the ith index of the summation of every
|
We can then find the total of the ith neighborhood by checking the sum of the ith index of the summation of every
|
||||||
9 chunks of numbers (this is done at the Nth-1 dimension).
|
9 chunks of numbers (this is done at the Nth-1 dimension).
|
||||||
"""
|
"""
|
||||||
neighborhoods = []
|
n_counts = []
|
||||||
|
|
||||||
# In the first dimension, we have to simply loop through and count for each bit
|
# In the first dimension, we have to simply loop through and count for each bit
|
||||||
if 0 < plane.N <= 1:
|
if 0 < plane.N <= 1:
|
||||||
for i in range(len(plane.bits)):
|
for i in range(len(plane.bits)):
|
||||||
neighborhoods.append(sum([plane.bits[i+j] for j in offsets]))
|
n_counts.append(sum([plane.bits[i+j] for j in offsets]))
|
||||||
else:
|
else:
|
||||||
for level in range(plane.shape[0]):
|
for level in range(plane.shape[0]):
|
||||||
|
|
||||||
|
@ -122,6 +122,6 @@ class Neighborhood:
|
||||||
totals = map(sum, zip(totals, padded_chunk))
|
totals = map(sum, zip(totals, padded_chunk))
|
||||||
|
|
||||||
# Neighboring totals now align with original grid
|
# Neighboring totals now align with original grid
|
||||||
neighborhoods += list(totals)
|
n_counts += list(totals)
|
||||||
|
|
||||||
return neighborhoods
|
return n_counts
|
||||||
|
|
11
src/plane.py
11
src/plane.py
|
@ -172,6 +172,17 @@ class Plane:
|
||||||
|
|
||||||
return index % len(self.bits)
|
return index % len(self.bits)
|
||||||
|
|
||||||
|
def unflatten(self, f_index):
|
||||||
|
"""
|
||||||
|
Given a flattened index, return back to tuple coordinates.
|
||||||
|
"""
|
||||||
|
coordinates = []
|
||||||
|
for i in self.offsets:
|
||||||
|
coordinates.append(f_index // i)
|
||||||
|
f_index -= (i * coordinates[-1])
|
||||||
|
|
||||||
|
return tuple(coordinates)
|
||||||
|
|
||||||
def matrix(self):
|
def matrix(self):
|
||||||
"""
|
"""
|
||||||
Convert bitarray into a corresponding numpy matrix.
|
Convert bitarray into a corresponding numpy matrix.
|
||||||
|
|
|
@ -14,7 +14,7 @@ from bitarray import bitarray
|
||||||
|
|
||||||
class Ruleset:
|
class Ruleset:
|
||||||
"""
|
"""
|
||||||
The primary class of this module, which saves configurations of cells that yield the next state.
|
A bundle of configurations.
|
||||||
|
|
||||||
The ruleset will take in configurations defined by the user that specify how a cell's state should change,
|
The ruleset will take in configurations defined by the user that specify how a cell's state should change,
|
||||||
depending on the given neighborhood and current state. For example, if I have a configuration that states
|
depending on the given neighborhood and current state. For example, if I have a configuration that states
|
||||||
|
@ -64,7 +64,16 @@ class Ruleset:
|
||||||
arg should be a function returning a BOOL, which takes in a current cell's value, and the
|
arg should be a function returning a BOOL, which takes in a current cell's value, and the
|
||||||
value of its neighbors.
|
value of its neighbors.
|
||||||
"""
|
"""
|
||||||
next_grid = []
|
next_states = []
|
||||||
|
|
||||||
|
for index, state in enumerate(plane.bits):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# We apply our method a row at a time, to take advantage of being able to sum the totals
|
# We apply our method a row at a time, to take advantage of being able to sum the totals
|
||||||
# of a neighborhood in a batch manner. We try to apply a configuration to every bit of a
|
# of a neighborhood in a batch manner. We try to apply a configuration to every bit of a
|
||||||
|
|
|
@ -113,3 +113,14 @@ class TestIndexing:
|
||||||
assert self.plane3d.flatten((-1, 0, 0)) == 990000
|
assert self.plane3d.flatten((-1, 0, 0)) == 990000
|
||||||
assert self.plane3d.flatten((1, 1, 1)) == 10101
|
assert self.plane3d.flatten((1, 1, 1)) == 10101
|
||||||
|
|
||||||
|
def test_unflatten(self):
|
||||||
|
"""
|
||||||
|
Unflatten indices.
|
||||||
|
"""
|
||||||
|
assert self.plane2d.unflatten(0) == (0, 0)
|
||||||
|
assert self.plane2d.unflatten(9900) == (99, 0)
|
||||||
|
assert self.plane2d.unflatten(101) == (1, 1)
|
||||||
|
assert self.plane3d.unflatten(0) == (0, 0, 0)
|
||||||
|
assert self.plane3d.unflatten(990000) == (99, 0, 0)
|
||||||
|
assert self.plane3d.unflatten(10101) == (1, 1, 1)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue