Created
October 23, 2025 19:15
-
-
Save gbragafibra/2c75367423472a741aae74ab8c634762 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| import mpmath | |
| import random | |
| def rule30(S): | |
| L = np.roll(S, 1) | |
| C = S | |
| R = np.roll(S, -1) | |
| return np.bitwise_xor(L, np.bitwise_or(C, R)) | |
| def rule90(S): | |
| L = np.roll(S, 1) | |
| R = np.roll(S, -1) | |
| return np.bitwise_xor(L, R) | |
| def rule244(S): | |
| L = np.roll(S, 1) | |
| C = S | |
| R = np.roll(S, -1) | |
| return np.bitwise_and(C, np.bitwise_or(L, R)) | |
| def rule104(S): | |
| L = np.roll(S, 1) | |
| C = S | |
| R = np.roll(S, -1) | |
| return np.bitwise_xor(C, np.bitwise_and(L, R)) | |
| def rule246(S): | |
| L = np.roll(S, 1) | |
| C = S | |
| R = np.roll(S, -1) | |
| return np.bitwise_or(R, np.bitwise_xor(L, R)) | |
| def rule170(S): | |
| L = np.roll(S, 1) | |
| C = S | |
| R = np.roll(S, -1) | |
| return np.bitwise_and(R, np.bitwise_or(L, np.bitwise_xor(C, R))) | |
| def rule144(S): | |
| L = np.roll(S, 1) | |
| C = S | |
| R = np.roll(S, -1) | |
| return np.bitwise_and(L, np.bitwise_xor(C, np.bitwise_xor(L, R))) | |
| def rule202(S): | |
| L = np.roll(S, 1) | |
| C = S | |
| R = np.roll(S, -1) | |
| return np.bitwise_xor(R, np.bitwise_and(L, np.bitwise_xor(C, R))) | |
| def rule156(S): | |
| L = np.roll(S, 1) | |
| C = S | |
| R = np.roll(S, -1) | |
| return np.bitwise_xor(C, np.bitwise_and(L, np.bitwise_xor(L, R))) | |
| def rule76(S): | |
| L = np.roll(S, 1) | |
| C = S | |
| R = np.roll(S, -1) | |
| return np.bitwise_and(C, np.bitwise_xor(C, np.bitwise_and(L, R))) | |
| #gen a random 8-bit rule number | |
| rule_number = random.randint(0, 255) | |
| print("Rule number:", rule_number) | |
| #get 8-bit binary | |
| rule_bits = np.array([(rule_number >> i) & 1 for i in range(8)], dtype=np.uint8) | |
| def random_rule(S): | |
| L = np.roll(S, 1) | |
| C = S | |
| R = np.roll(S, -1) | |
| #code for each neighbor (0-7) | |
| neighborhood = (L << 2) | (C << 1) | R | |
| return rule_bits[(7 - neighborhood).astype(int)] | |
| def collatz_automata(n, *args): | |
| S = np.zeros(N, dtype=np.uint8) | |
| S[N//2] = 1 | |
| gates = [rule30, random_rule] | |
| frames = S.copy().reshape(1, -1) | |
| while n != 1: | |
| par = int(n % 2) #parity | |
| if par == 0: | |
| n /= 2 | |
| else: | |
| n = (3 * n + 1)/2 | |
| S = gates[par](S) | |
| frames = np.concatenate((frames, S.reshape(1, -1)), axis = 0) | |
| return frames | |
| if __name__ == "__main__": | |
| mpmath.mp.dps = 201 | |
| n = mpmath.mpf(10**30) | |
| N = 400 | |
| frames = collatz_automata(n) | |
| plt.imshow(frames, cmap = "binary") | |
| plt.axis("off") | |
| plt.tight_layout() | |
| plt.show() | |
| #plt.savefig("col_aut_1e30_r30,76.png", dpi=300, bbox_inches="tight") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment