diff --git a/src/neighborhood.py b/src/neighborhood.py index 68efc5e..bb1b7b5 100644 --- a/src/neighborhood.py +++ b/src/neighborhood.py @@ -55,7 +55,7 @@ class Neighborhood: if plane.N > 0: f_offsets = list(map(plane.flatten, offsets)) for i in range(len(plane.bits)): - neighborhood = Neighborhood(i) + neighborhood = Neighborhood(plane.unflatten(i)) for j in range(len(f_offsets)): neighborhood.neighbors.append(plane.bits[j]) 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 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 if 0 < plane.N <= 1: 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: for level in range(plane.shape[0]): @@ -122,6 +122,6 @@ class Neighborhood: totals = map(sum, zip(totals, padded_chunk)) # Neighboring totals now align with original grid - neighborhoods += list(totals) + n_counts += list(totals) - return neighborhoods + return n_counts diff --git a/src/plane.py b/src/plane.py index 4170624..475d02c 100644 --- a/src/plane.py +++ b/src/plane.py @@ -172,6 +172,17 @@ class Plane: 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): """ Convert bitarray into a corresponding numpy matrix. diff --git a/src/ruleset.py b/src/ruleset.py index f4da395..de1b76d 100644 --- a/src/ruleset.py +++ b/src/ruleset.py @@ -14,7 +14,7 @@ from bitarray import bitarray 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, 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 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 # of a neighborhood in a batch manner. We try to apply a configuration to every bit of a diff --git a/tests/plane_test.py b/tests/plane_test.py index a1f4a49..ceda377 100644 --- a/tests/plane_test.py +++ b/tests/plane_test.py @@ -113,3 +113,14 @@ class TestIndexing: assert self.plane3d.flatten((-1, 0, 0)) == 990000 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) +