Cells (Armin Hofmann) with sympy II

Task: Previously we’ve written a program that outputs all possibilities of a 3 x 3 grid, containing 3 white cells (zeros) and 6 black cells (ones). No we want to reduce that list of possibilities based on conditions.

Imports

from sympy.utilities.iterables import multiset_permutations
import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np

Create a list of all permutations

ones = np.ones(6, dtype=np.int8)  # Create an array of 1s
zeros = np.zeros(3, dtype=np.int8)  # Create an array of 0s
data = np.concatenate((ones, zeros))  # Combine the arrays
print(data)  # Check the data
[1 1 1 1 1 1 0 0 0]
# Create all permutations and store them in a list
permutations = list(multiset_permutations(data))
# Print the length
print(len(permutations))
# Print the first items
for i in range(5):
    print(permutations[i])
84
[0, 0, 0, 1, 1, 1, 1, 1, 1]
[0, 0, 1, 0, 1, 1, 1, 1, 1]
[0, 0, 1, 1, 0, 1, 1, 1, 1]
[0, 0, 1, 1, 1, 0, 1, 1, 1]
[0, 0, 1, 1, 1, 1, 0, 1, 1]

Reduce the list based on a condition

Next we’ll define a condition, that will reduce the list to elements matching that condition. We’ll go through our list permutations and check for each element if the center cell is black. If yes, we will add it to a new list. Then we’ll plot this new list.

black_center = []  # Empty list for elements matching the condition

# Loop through all elements
for r in permutations:
    # If the center cell is black, add it to the new list
    if r[4] == 1: # 4 because counting starts at 0
        black_center.append(r)
        
print(len(black_center))
56
def display_grids(grids, grid_shape=(3,3), columns=7, set_title=True):
    '''The function takes a list of grids in a binary 
    representation (string) and outputs a plot.'''
    import math
    columns = columns
    rows = math.ceil(len(grids)/columns)
    fig=plt.figure(figsize=(columns*3, rows*3))
    cmap = colors.ListedColormap(['white','black'])

    for i in range(len(grids)):
        binary_representation = grids[i]
        data = [int(n) for n in binary_representation]
        data = np.array(data).reshape(grid_shape)
        if set_title:
            title = f'{i} ( {binary_representation} )'
            fig.add_subplot(rows, columns, i+1).set_title(title)
        else:
            fig.add_subplot(rows, columns, i+1)
        # exclude ticks
        plt.xticks(ticks=[])
        plt.yticks(ticks=[])
        plt.imshow(data, cmap=cmap)
    plt.show()
display_grids(black_center, set_title=False)
_images/cells_sympy_2_11_0.png

List comprehension

In the following we’ll use list comprehension with an if statement to filter out all sequences with a white cell in the center.

white_center = [r for r in permutations if r[4] == 0]
display_grids(white_center, set_title=False)
_images/cells_sympy_2_13_0.png

Check with math

# Possibilities with a black centered cell

n = math.factorial(8)  # 9 - 1 fixed cell
k_1 = math.factorial(5)  # 6 black cells - 1 fixed black cell (center)
k_0 = math.factorial(3)  # 3 white cells

p = n / (k_1*k_0)
print(p)
56.0
# Possibilities with a white centered cell

n = math.factorial(8)  # 9 - 1 fixed cell
k_1 = math.factorial(6)  # 6 black cells
k_0 = math.factorial(2)  # 3 white cells - 1 fixed white cell (center)

p = n / (k_1*k_0)
print(p)
28.0

Task: Plot shapes based on your own condition, maybe in a larger grid