tupper公式(塔珀自指公式)

简述

Tupper 的自引用公式是由 Jeff Tupper 定义的公式,当在平面中非常特定的位置以二维图形绘制时,可以直观地再现公式本身。

实际上,它再现了所有可能的 106 像素宽和 17 像素高的图像。

该公式是由以下定义的不等式:

\[ \frac{1}{2}<\left\lfloor\mathrm{mod}\left(\left\lfloor\frac{y}{17}\right\rfloor 2^{-17\lfloor x\rfloor-\mathrm{mod}(\lfloor y\rfloor,17))},2\right)\right\rfloor \] 对于不同的 \(k\) 值,可以求得不同的图像,而当 \(k\)

1
4858450636189713423582095962494202044581400587983244549483093085061934704708809928450644769865524364849997247024915119110411605739177407856919754326571855442057210445735883681829823754139634338225199452191651284348332905131193199953502413758765239264874613394906870130562295813219481113685339535565290850023875092856892694555974281546386510730049106723058933586052544096664351265349363643957125565695936815184334857605266940161251266951421550539554519153785457525756590740540157929001765967965480064427829131488548259914721248506352686630476300

将得到与自身表达式完全一样的图像,故被称作自引用(自指)公式。

自指公式图像

可以使用在线网站绘制图像:https://tuppers-formula.ovh/

Python实现

与原公式不相同,做了一些修正

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from typing import Literal

def tupper_self_referential_formula(k: int, width: int = 17, height: int = 106):
"""
Tupper self referential formula函数

Parameters:
k - 用于迭代图像的k值
width - 图像的宽
height - 图像的高

Returns:
类型是numpy.ndarray矩阵,里面只有0和1
"""
import numpy as np
rst = np.zeros((width, height))
def f(x: int, y: int) -> int:
y += k
a1 = 2 ** (width * x + y % width)
a2 = (y // width) // a1
return 1 if (a2 & 1) == 1 else 0

for y in range(width):
for x in range(height):
rst[y, x] = f(x, y)

return rst[:, ::-1]

def tupper_to_str(k: int, width: int = 17, height: int = 106, show_black: str = "■", show_white: str = "□") -> str:
"""
生成Tupper self referential formula函数的字符画

Parameters:
k - 用于迭代图像的k值
width - 图像的宽
height - 图像的高
show_black - 字符画中的黑色方块
show_white - 字符画中的白色方块

Returns:
字符画字符串
"""
mat = tupper_self_referential_formula(k, width, height)
rst = ''
for y in range(width):
for x in range(height):
if mat[y, x] == 1:
rst += show_black
else:
rst += show_white
rst += '\n'
return rst

def tupper_to_img(k: int, width: int = 17, height: int = 106, filename: str = 'tupper.png', origin: Literal['upper', 'lower'] = 'upper'):
"""
生成Tupper self referential formula函数的字符画

Parameters:
k - 用于迭代图像的k值
width - 图像的宽
height - 图像的高
filename - 输出图像的文件名
origin - 输出图像的方式(upper或者lower)

Returns:
类型是numpy.ndarray矩阵,里面只有0和1
"""
import matplotlib.pyplot as plt
rst = tupper_self_referential_formula(k, width, height)
plt.figure(figsize=(15, 10))
plt.imshow(rst, origin=origin)
plt.savefig(filename)

return rst


k = 92898203278702907929705938676672021500394791427205757369123489204565300324859717082409892641951206664564991991489354661871425872649524078000948199832659815275909285198829276929014694628110159824930931595166203271443269827449505707655085842563682060910813942504507936625555735585913273575050118552353192682955310220323463465408645422334101446471078933149287336241772448338428740302833855616421538520769267636119285948674549756604384946996184385407505456168240123319785800909933214695711828013483981731933773017336944656397583872267126767778549745087854794302808950100966582558761224454242018467578959766617176016660101690140279961968740323327369347164623746391335756442566959352876706364265509834319910419399748338894746638758652286771979896573695823608678008814861640308571256880794312652055957150464513950305355055495262375870102898500643010471425931450046440860841589302890250456138060738689526283389256801969190204127358098408264204643882520969704221896973544620102494391269663693407573658064279947688509910028257209987991480259150865283245150325813888942058

rst = tupper_to_str(k, 22, 160)
print(rst)
tupper_to_img(k, 22, 160)