revisions to polycount.cell2

This commit is contained in:
queue-miscreant 2025-02-23 18:09:37 -06:00
parent 5cb572e3e1
commit bce371e2a8
12 changed files with 663 additions and 248 deletions

1
.gitignore vendored
View File

@ -3,4 +3,5 @@ _site/
/.quarto/
index_files/
.jupyter_cache/
*.quarto_ipynb
__pycache__/

View File

@ -3,7 +3,7 @@ title: "Polynomial Counting 4: Two 2's"
format:
html:
html-math-method: katex
date: "2021-02-06"
date: "2021-02-09"
date-modified: "2025-02-12"
jupyter: python3
categories:

View File

@ -3,8 +3,8 @@ title: "Polynomial Counting 5: Pentamerous multiplication"
format:
html:
html-math-method: katex
date: "2021-02-09"
date-modified: "2025-2-12"
date: "2021-02-12"
date-modified: "2025-02-12"
jupyter: python3
categories:
- algebra

View File

@ -22,7 +22,7 @@ def animate(
def animate_carry_count(
carry: Carry,
dims: int = 100,
operation: Literal["add", "multiply"] = "add",
operation: Literal["add", "multiply", "log"] = "add",
op_val=1,
center: tuple[int, int] | None = None,
# Animation parameters
@ -49,18 +49,18 @@ def animate_carry_count(
else:
center = (0, 0)
if operation == "add":
next_func = partial(add, carry=carry, val=op_val, center=center)
if operation == "add" or operation == "log":
next_func = partial(add, carry=carry, center=center)
elif operation == "multiply":
if op_val == 1:
raise ValueError(f"too small value {op_val} for repeated multiplication")
next_func = partial(times, carry=carry, val=op_val)
next_func = partial(times, carry=carry)
else:
raise ValueError(f"Cannot use {repr(operation)} for animation")
expansion = np.zeros((dims, dims))
expansion[center] = 1
title = [1]
state = [1, op_val, op_val]
fig = plt.gcf()
@ -69,21 +69,26 @@ def animate_carry_count(
extent=[-center[0], dims - center[0], dims - center[1], -center[1]], # type: ignore
)
def init():
plt.title(f"{title[0]}")
plt.title(f"{state[0]}")
image.set_clim(0, carry.overflow - 1)
fig.tight_layout()
@animate(fig, frames, interval=interval, init_func=init)
def ret(_):
plt.title(f"{title[0]}")
title, op_val, old_op_val = state
plt.title(f"{title}")
image.set_data(expansion)
fig.tight_layout()
next_func(expansion)
next_func(expansion, val=op_val)
if operation == "add":
title[0] += op_val
else:
title[0] *= op_val
state[0] += op_val
elif operation == "multiply":
state[0] *= op_val
elif operation == "log":
state[0] += op_val
if state[0] >= op_val * old_op_val * old_op_val:
state[1] *= old_op_val
return ret

View File

@ -1,4 +1,6 @@
from sympy.abc import x, y
import sympy
from sympy.polys.polytools import degree
import numpy as np
from .carry import Carry
@ -38,6 +40,43 @@ def poly_from_array(
return ret
def poly_to_array(
poly: sympy.Expr,
alloc: np.ndarray | None = None,
x_var=x,
y_var=y
) -> np.ndarray:
'''Convert a polynomial of 2 variables to a 2D array'''
if isinstance(poly, (int, sympy.core.numbers.Zero)):
if alloc is None:
alloc = np.zeros((1, 1), dtype=np.int32)
alloc[0,0] = int(poly)
return alloc
alloc.fill(0)
alloc[0,0] = int(poly)
return alloc
coeffs = list(sorted(
map(
lambda z: (z, (degree(z[0], x_var), degree(z[0], y_var))), # type: ignore
poly.as_coefficients_dict().items()
),
key=lambda z: sum(z[1])
))
max_x, max_y = coeffs[-1][1]
if alloc is None:
alloc = np.zeros((max_x + max_y + 1, max_x + max_y + 1), dtype=np.int32)
elif alloc.shape[0] < (max_x + 1) or alloc.shape[1] < (max_y + 1):
raise ValueError("return array has insufficient size")
else:
alloc.fill(0)
for (_, coeff), (x_deg, y_deg) in coeffs:
alloc[y_deg, x_deg] = coeff
return alloc
def _first_nonzero(arr: np.ndarray, axis: int):
"""
Find the index of the first nonzero entry of `arr` along `axis`.

View File

@ -5,7 +5,7 @@ format:
html:
html-math-method: katex
date: "2021-02-23"
date-modified: "2025-2-20"
date-modified: "2025-02-20"
jupyter: python3
categories:
- algebra

1
polycount/cell2/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.mp4

1
polycount/cell2/carry2d Symbolic link
View File

@ -0,0 +1 @@
../cell1/carry2d

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB