Starting interactive Python…
import%20base64%0Aimport%20os%0Aimport%20struct%0Aimport%20zlib%0A%0Aimport%20marimo%20as%20mo%0Aimport%20numpy%20as%20np
PRESETS%20%3D%20%7B%0A%20%20%20%20%22Conradi%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22matrix%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B-2%2C%200%2C%200%2C%200%2C%200%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%200%2C%201%2C%201%2C%200%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B2%2C%200%2C%200%2C%201%2C%200%2C%202%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%200%2C%200%2C%20-2%2C%20-2%2C%202%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B-2%2C%201%2C%200%2C%200%2C%20-2%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B-2%2C%201%2C%200%2C%200%2C%200%2C%20-2%5D%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22mask%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%200%2C%201%2C%200%2C%200%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%200%2C%200%2C%200%2C%200%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%200%2C%200%2C%200%2C%200%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%200%2C%200%2C%200%2C%200%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%200%2C%200%2C%200%2C%200%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%200%2C%200%2C%200%2C%200%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22Sparse%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22matrix%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%202%2C%200%2C%200%2C%20-2%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B-2%2C%200%2C%201%2C%200%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%20-1%2C%200%2C%202%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%200%2C%20-2%2C%200%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B2%2C%200%2C%200%2C%20-1%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22mask%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%200%2C%201%2C%200%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%200%2C%200%2C%200%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%200%2C%200%2C%200%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%200%2C%200%2C%200%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%200%2C%200%2C%200%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22Border%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22matrix%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%201%2C%200%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B2%2C%200%2C%202%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%201%2C%200%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B2%2C%200%2C%202%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22mask%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%201%2C%200%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%200%2C%200%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%200%2C%200%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B0%2C%200%2C%201%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%7D%2C%0A%7D%0APALETTES%20%3D%20%7B%0A%20%20%20%20%22Ink%22%3A%20((245%2C%20241%2C%20235)%2C%20(45%2C%2037%2C%2032))%2C%0A%20%20%20%20%22Rust%22%3A%20((245%2C%20241%2C%20235)%2C%20(191%2C%2097%2C%2089))%2C%0A%20%20%20%20%22Pine%22%3A%20((245%2C%20241%2C%20235)%2C%20(86%2C%20112%2C%20107))%2C%0A%7D
def%20decode_matrix(encoded)%3A%0A%20%20%20%20encoded%20%3D%20np.asarray(encoded%2C%20dtype%3Dint)%0A%20%20%20%20matrix%20%3D%20np.zeros(encoded.shape%2C%20dtype%3Dnp.complex128)%0A%20%20%20%20matrix%5Bencoded%20%3D%3D%20-2%5D%20%3D%20-1j%0A%20%20%20%20matrix%5Bencoded%20%3D%3D%20-1%5D%20%3D%20-1%0A%20%20%20%20matrix%5Bencoded%20%3D%3D%201%5D%20%3D%201%0A%20%20%20%20matrix%5Bencoded%20%3D%3D%202%5D%20%3D%201j%0A%20%20%20%20return%20matrix%0A%0Adef%20eigenfish_torus(base%2C%20variable_mask%2C%20samples%2C%20radius)%3A%0A%20%20%20%20rows%2C%20cols%20%3D%20np.nonzero(variable_mask)%0A%20%20%20%20if%20len(rows)%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20return%20np.array(%5B%5D%2C%20dtype%3Dnp.complex128)%0A%20%20%20%20rng%20%3D%20np.random.default_rng(7)%0A%20%20%20%20phases%20%3D%20radius%20*%20np.exp(%0A%20%20%20%20%20%20%20%201j%20*%20rng.uniform(0%2C%202%20*%20np.pi%2C%20size%3D(samples%2C%20len(rows)))%0A%20%20%20%20)%0A%20%20%20%20matrices%20%3D%20np.broadcast_to(base%2C%20(samples%2C%20*base.shape)).copy()%0A%20%20%20%20matrices%5B%3A%2C%20rows%2C%20cols%5D%20%3D%20phases%0A%20%20%20%20return%20np.linalg.eigvals(matrices).reshape(-1)%0A%0Adef%20density_png(eigenvalues%2C%20background%2C%20foreground%2C%20size%3D820)%3A%0A%20%20%20%20coordinates%20%3D%20np.concatenate(%5Beigenvalues.real%2C%20eigenvalues.imag%5D)%0A%20%20%20%20limit%20%3D%20max(float(np.quantile(np.abs(coordinates)%2C%200.997))%2C%201e-6)%20*%201.06%0A%20%20%20%20x%20%3D%20((eigenvalues.real%20%2B%20limit)%20%2F%20(2%20*%20limit)%20*%20(size%20-%201)).astype(int)%0A%20%20%20%20y%20%3D%20((eigenvalues.imag%20%2B%20limit)%20%2F%20(2%20*%20limit)%20*%20(size%20-%201)).astype(int)%0A%20%20%20%20inside%20%3D%20(x%20%3E%3D%200)%20%26%20(x%20%3C%20size)%20%26%20(y%20%3E%3D%200)%20%26%20(y%20%3C%20size)%0A%0A%20%20%20%20density%20%3D%20np.zeros((size%2C%20size)%2C%20dtype%3Dnp.float32)%0A%20%20%20%20np.add.at(density%2C%20(size%20-%201%20-%20y%5Binside%5D%2C%20x%5Binside%5D)%2C%201)%0A%20%20%20%20glow%20%3D%20density.copy()%0A%20%20%20%20for%20shift_y%2C%20shift_x%2C%20weight%20in%20(%0A%20%20%20%20%20%20%20%20(-1%2C%200%2C%200.50)%2C%0A%20%20%20%20%20%20%20%20(1%2C%200%2C%200.50)%2C%0A%20%20%20%20%20%20%20%20(0%2C%20-1%2C%200.50)%2C%0A%20%20%20%20%20%20%20%20(0%2C%201%2C%200.50)%2C%0A%20%20%20%20%20%20%20%20(-1%2C%20-1%2C%200.22)%2C%0A%20%20%20%20%20%20%20%20(-1%2C%201%2C%200.22)%2C%0A%20%20%20%20%20%20%20%20(1%2C%20-1%2C%200.22)%2C%0A%20%20%20%20%20%20%20%20(1%2C%201%2C%200.22)%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20glow%20%2B%3D%20weight%20*%20np.roll(density%2C%20(shift_y%2C%20shift_x)%2C%20axis%3D(0%2C%201))%0A%0A%20%20%20%20tone%20%3D%20np.log1p(glow)%0A%20%20%20%20nonzero%20%3D%20tone%5Btone%20%3E%200%5D%0A%20%20%20%20exposure%20%3D%20float(np.quantile(nonzero%2C%200.997))%20if%20len(nonzero)%20else%201.0%0A%20%20%20%20tone%20%3D%20np.clip(tone%20%2F%20max(exposure%2C%201e-6)%2C%200%2C%201)%20**%200.72%0A%20%20%20%20bg%20%3D%20np.asarray(background%2C%20dtype%3Dnp.float32)%0A%20%20%20%20fg%20%3D%20np.asarray(foreground%2C%20dtype%3Dnp.float32)%0A%20%20%20%20pixels%20%3D%20np.rint(bg%20%2B%20tone%5B...%2C%20None%5D%20*%20(fg%20-%20bg)).astype(np.uint8)%0A%0A%20%20%20%20def%20chunk(kind%2C%20data)%3A%0A%20%20%20%20%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20struct.pack(%22%3EI%22%2C%20len(data))%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20kind%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20data%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20struct.pack(%22%3EI%22%2C%20zlib.crc32(kind%20%2B%20data)%20%26%200xFFFFFFFF)%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20scanlines%20%3D%20b%22%22.join(b%22%5Cx00%22%20%2B%20row.tobytes()%20for%20row%20in%20pixels)%0A%20%20%20%20png%20%3D%20(%0A%20%20%20%20%20%20%20%20b%22%5Cx89PNG%5Cr%5Cn%5Cx1a%5Cn%22%0A%20%20%20%20%20%20%20%20%2B%20chunk(b%22IHDR%22%2C%20struct.pack(%22%3EIIBBBBB%22%2C%20size%2C%20size%2C%208%2C%202%2C%200%2C%200%2C%200))%0A%20%20%20%20%20%20%20%20%2B%20chunk(b%22IDAT%22%2C%20zlib.compress(scanlines%2C%209))%0A%20%20%20%20%20%20%20%20%2B%20chunk(b%22IEND%22%2C%20b%22%22)%0A%20%20%20%20)%0A%20%20%20%20return%20%22data%3Aimage%2Fpng%3Bbase64%2C%22%20%2B%20base64.b64encode(png).decode(%22ascii%22)
A small matrix becomes a creature when selected entries move around a
complex torus. Inspired by Simon Conradi.
preset%20%3D%20mo.ui.dropdown(%0A%20%20%20%20options%3Dlist(PRESETS)%2C%20value%3D%22Conradi%22%2C%20label%3D%22Structure%22%0A)%0Asamples%20%3D%20mo.ui.slider(%0A%20%20%20%20start%3D2000%2C%0A%20%20%20%20stop%3D30000%2C%0A%20%20%20%20step%3D2000%2C%0A%20%20%20%20value%3D12000%2C%0A%20%20%20%20debounce%3DTrue%2C%0A%20%20%20%20show_value%3DTrue%2C%0A%20%20%20%20label%3D%22Samples%22%2C%0A)%0Aradius%20%3D%20mo.ui.slider(%0A%20%20%20%20start%3D0.25%2C%0A%20%20%20%20stop%3D2.0%2C%0A%20%20%20%20step%3D0.05%2C%0A%20%20%20%20value%3D1.0%2C%0A%20%20%20%20debounce%3DTrue%2C%0A%20%20%20%20show_value%3DTrue%2C%0A%20%20%20%20label%3D%22Torus%20radius%22%2C%0A)%0Apalette%20%3D%20mo.ui.dropdown(options%3Dlist(PALETTES)%2C%20value%3D%22Ink%22%2C%20label%3D%22Ink%22)%0A%0Amo.vstack(%0A%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%20%20%20%20A%20small%20matrix%20becomes%20a%20creature%20when%20selected%20entries%20move%20around%20a%0A%20%20%20%20%20%20%20%20complex%20torus.%20Inspired%20by%20%5BSimon%20Conradi%5D(..%2Fsimon-conradi).%0A%20%20%20%20%20%20%20%20%22%22%22)%2C%0A%20%20%20%20%20%20%20%20mo.hstack(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5Bpreset%2C%20samples%2C%20radius%2C%20palette%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20widths%3D%22equal%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20gap%3D1%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%5D%2C%0A%20%20%20%20gap%3D1.2%2C%0A)
Shape the matrix
Drag or press Enter to edit. Matrix codes: −2 = −i, −1, 0, 1, 2 = i. Set the same position to 1 in the variable mask to sample it from the torus.
selected%20%3D%20PRESETS%5Bpreset.value%5D%0Adimension%20%3D%20len(selected%5B%22matrix%22%5D)%0Alabels%20%3D%20%5Bstr(index%20%2B%201)%20for%20index%20in%20range(dimension)%5D%0Amatrix_editor%20%3D%20mo.ui.matrix(%0A%20%20%20%20selected%5B%22matrix%22%5D%2C%0A%20%20%20%20min_value%3D-2%2C%0A%20%20%20%20max_value%3D2%2C%0A%20%20%20%20step%3D1%2C%0A%20%20%20%20precision%3D0%2C%0A%20%20%20%20row_labels%3Dlabels%2C%0A%20%20%20%20column_labels%3Dlabels%2C%0A%20%20%20%20debounce%3DTrue%2C%0A%20%20%20%20label%3D%22Matrix%22%2C%0A)%0Amask_editor%20%3D%20mo.ui.matrix(%0A%20%20%20%20selected%5B%22mask%22%5D%2C%0A%20%20%20%20min_value%3D0%2C%0A%20%20%20%20max_value%3D1%2C%0A%20%20%20%20step%3D1%2C%0A%20%20%20%20precision%3D0%2C%0A%20%20%20%20row_labels%3Dlabels%2C%0A%20%20%20%20column_labels%3Dlabels%2C%0A%20%20%20%20debounce%3DTrue%2C%0A%20%20%20%20label%3D%22Variable%20entries%22%2C%0A)%0A%0Amo.vstack(%0A%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20mo.md(%22%23%23%20Shape%20the%20matrix%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22Drag%20or%20press%20Enter%20to%20edit.%20Matrix%20codes%3A%20%60%E2%88%922%20%3D%20%E2%88%92i%60%2C%20%60%E2%88%921%60%2C%20%600%60%2C%20%601%60%2C%20%602%20%3D%20i%60.%20Set%20the%20same%20position%20to%20%601%60%20in%20the%20variable%20mask%20to%20sample%20it%20from%20the%20torus.%22)%2C%0A%20%20%20%20%20%20%20%20mo.hstack(%5Bmatrix_editor%2C%20mask_editor%5D%2C%20widths%3D%22equal%22%2C%20gap%3D1.5)%2C%0A%20%20%20%20%5D%2C%0A%20%20%20%20gap%3D0.75%2C%0A)
base_matrix%20%3D%20decode_matrix(matrix_editor.value)%0Avariable_mask%20%3D%20np.asarray(mask_editor.value%2C%20dtype%3Dfloat)%20%3E%3D%200.5%0Aeffective_samples%20%3D%20(%0A%20%20%20%20min(samples.value%2C%202000)%0A%20%20%20%20if%20os.environ.get(%22EWAN_MARIMO_STATIC_PREVIEW%22)%20%3D%3D%20%221%22%0A%20%20%20%20else%20samples.value%0A)%0Aeigenvalues%20%3D%20eigenfish_torus(%0A%20%20%20%20base_matrix%2C%0A%20%20%20%20variable_mask%2C%0A%20%20%20%20effective_samples%2C%0A%20%20%20%20radius.value%2C%0A)
12,000 eigenvalues · 2 torus-sampled entries
variable_count%20%3D%20int(variable_mask.sum())%0Aif%20len(eigenvalues)%20%3D%3D%200%3A%0A%20%20%20%20artwork%20%3D%20mo.callout(%0A%20%20%20%20%20%20%20%20mo.md(%22Select%20at%20least%20one%20position%20in%20the%20variable%20mask.%22)%2C%0A%20%20%20%20%20%20%20%20kind%3D%22warn%22%2C%0A%20%20%20%20)%0Aelse%3A%0A%20%20%20%20background%2C%20foreground%20%3D%20PALETTES%5Bpalette.value%5D%0A%20%20%20%20source%20%3D%20density_png(eigenvalues%2C%20background%2C%20foreground)%0A%20%20%20%20artwork%20%3D%20mo.Html(%0A%20%20%20%20%20%20%20%20f%22%22%22%0A%20%20%20%20%20%20%20%20%3Cfigure%20class%3D%22eigenfish-figure%22%3E%0A%20%20%20%20%20%20%20%20%20%20%3Cimg%20src%3D%22%7Bsource%7D%22%20alt%3D%22Eigenvalue%20density%20generated%20from%20the%20edited%20complex%20matrix%22%20%2F%3E%0A%20%20%20%20%20%20%20%20%20%20%3Cfigcaption%3E%7Blen(eigenvalues)%3A%2C%7D%20eigenvalues%20%C2%B7%20%7Bvariable_count%7D%20torus-sampled%20entries%3C%2Ffigcaption%3E%0A%20%20%20%20%20%20%20%20%3C%2Ffigure%3E%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0Aartwork