in quine python tupper ~ read.

Tupper's self-referential formula

So here's something cool. This formula:

$$\frac{1}{2} < \lfloor mod(\lfloor\frac{y}{17}\rfloor2^{-17\lfloor x \rfloor -mod(\lfloor y\rfloor,17)},2)\rfloor$$

will literally plot itself if you look at the right region.

where k is this 543-digit integer:

960 939 379 918 958 884 971 672 962 127 852 754 715 004 339 660 129 306 651 505 519 271 702 802 395 266 424 689 642 842 174 350 718 121 267 153 782 770 623 355 993 237 280 874 144 307 891 325 963 941 337 723 487 857 735 749 823 926 629 715 517 173 716 995 165 232 890 538 221 612 403 238 855 866 184 013 235 585 136 048 828 693 337 902 491 454 229 288 667 081 096 184 496 091 705 183 454 067 827 731 551 705 405 381 627 380 967 602 565 625 016 981 482 083 418 783 163 849 115 590 225 610 003 652 351 370 343 874 461 848 378 737 238 198 224 849 863 465 033 159 410 054 974 700 593 138 339 226 497 249 461 751 545 728 366 702 369 745 461 014 655 997 933 798 537 483 143 786 841 806 593 422 227 898 388 722 980 000 748 404 719  

So how exactly does this work? Let's try to decipher what's happening in the inequality.

Saying the floor of something is greater than a half just means its greater than one:

$$1 \leq mod(\lfloor\frac{y}{17}\rfloor2^{-17\lfloor x \rfloor -mod(\lfloor y\rfloor,17)},2)$$

If we let $y = 17q + r$ where $0 \leq r < 17$, the expression simplifies to:

$$1 \leq mod(\frac{q}{2^{17x+r}},2)$$

Asking if the modulo 2 of a number is greater than 1, amounts to asking if the floor of the number is odd, so the inequality we had before amounts to asking if $\lfloor mod(\frac{q}{2^{17x+r}},2) \rfloor $ is odd.

Now if you notice, our expression is dividing $q$ by a power of two, which if you're familiar with binary arithmetic equates to asking if the $17x+r$th bit of q is a 1.

So $q$ is essentially just the bits of the image we want to display, and the inequality is just a way of mapping the bits of $q$ to positions on the graph $(x,r)$

With an understanding of how the formula works, we can derive a more general formula for an arbitrary resolution, and find an N for any image of our choosing.

$$\frac{1}{2} < \lfloor mod(\lfloor\frac{y}{H}\rfloor2^{-H\lfloor x \rfloor -mod(\lfloor y\rfloor,H)},2)\rfloor$$

# convert a .png image file to a .bmp image file using PIL

from PIL import Image  
import numpy as np

col = Image.open("/Users/benjaminyu/Pictures/snoo.png")  
gray = col.convert('L')  
gray = gray.resize((128,int((float(gray.size[1])*float(128/float(gray.size[0]))))), Image.ANTIALIAS)


# Let numpy do the heavy lifting for converting pixels to pure black or white
bw = np.asarray(gray).copy()

# Pixel range is 0...255, 256/2 = 128
bw[bw < 128] = 1    # White  
bw[bw >= 128] = 0 # Black  
N = int("".join(map(lambda x: str(x), np.ravel(np.flipud(bw.T)))),2) * bw.shape[0]

print N  

So we can encode any arbitrary picture into a number N:

N = 2934623275416912475030491109196223507870408304003274787529399398373409470947725212282507513985364121401993299590057603536651058434430428000713934034262420539588839714855313659476041329252205216284053036373985592156078116097862529167048970032268372106689275943029232884582183802275608406984310060487284077383545964976626049402237758337424825667882109775758119069759711086159475402546442908115604247479467761707595602823277222590174148061721144661562877935837684987331648950202379210419761418529491052519903672658483467127112354245170241000511651371573682684652138489833128947164860555689296831469585401404266519623158869788893484136525119276348088537397595057764763706428854503607822157361565041165736407127934729927206490929693840877341534651602519824327919988642906846941938143188348416246303450729216256492285763699631779017092550576801609733463569141992832078904210560389835678854985222553314212302157089647040770218576400856148712692549612864827859634848567874230553830231008608480551526185331610884023950473395627404510338247990509604688305502857258206169437219817544147597583712195213777653405268756727571883678689585163327871720739436240460669418300306624465337083345340464995381274757531302248790539716766397711852327302990770383523606908739248026364599355756834351397558487059050684928260928838432222518793427292433951013483978017470108122865208180548821988646986231703540291159558903789235893979713420832755414423733836281551853729042926352142592526095601876141580214105543791390293602903481652256017836803468399539265356189557558771422077488754514918578855210716867823312306187344566890350234802433750468728689432768555311875399950666919244002684268574753407813634761601994474156237457500418442033482936308192038219163087024314080549797579988103924908630687122751619154127395573557908674276686814377965651263181751997208887462721343142724195461589133490301517242186511008672593881507803523441566952187960237515387625734553156736149727199481694354773270128522179866731509657925460090139528874458275244429430892452942981756076496885441567514879027361692817842321464584459280173324141908504491156943594276771103722834076808201752686726085560071885872984223087682696920575570456442655477225039976727495199911238224829072394783543050873150280650591819431533272367505877158020727584326980883477180928874605391350817024759871309404085387989405329715957150324979071535589966696213464423087118981480999105097535293942661806511503593473513724116877585440751185516253755737795162786017812931574161592606767535714074215598821686593293532932960264437635558765976389576877509305340221758973065089196415837498445497408525156489886212813684243724323725004685820770019255422439909930727267561273169067878674794273010056125234113530987330376846261079028270380444771832099462250359430408276398220720074274559637984804760436078298796343035087959362643005440997645523579810464973937126682694540083574858141767508134773227942068674148031670131717931418689152363586500900535680051127415533991251403919548661081834899052099337098194392197384935184663026546262344068986801756366808527194969649049755136712360954774314964625405934678972712839590470578211817546424938634849289847854768974075481629606601073700446951475887076345447295918394091512682194830312520818996687689385505716564672250993308451896798645087794301454736179531933752575980180053095262026112430109553088168674158571212573903619901314636516438376802721176516985088450078634272548208727838394836195269009603161045007845222625981683707388914297613369778954556464969666114729890039816788008611987751149284082746550112766208312052209559817611969588323556404633247069961729801774315695724440062396745250675401936097773795213305336722643233740567985176560174300445832865640238382675373274271037606039858106682894696450279221097336576593887033982977033445050701383492878778355548463657547230933917003412117674221164036961378317484903511666429985992600722135242153678097475445447559103223641494220495969919597615352258710185128390228610549474981196081491816244219184591728957745644815545422896276054189162468951290185357853361610430259087161986486053419230293766230560812427982806743750137561941534672666011438561263643205387915597505501012317896658902435472712920787093616146812846754247561670673098256668411055723483723692084567264275093528043826421642341526643798201364109953265902247797332233881512376876905775491832541580680462110631962411604960776572987246841750575786400748930967868158655063870179156433850287070223190824787769375734172851199964269451035740979289566079882401273223261310067061915476632854584196707898910527479163663645295174286326363519525815572197529728287375600807852619655034597605353867924297140123622805070381944205650513716454290134805832443444451157184311419910156868409289554683807983110249242741375974240359369882359906025774629741738831739082242574243835891288583403863249591034478333038474016980047411831632234201557906326705010670843157572860789962076004094863029562686271324313636041475731335624439151784655947259452318740012832109494358173866605847931029270764452184619298435460341718297701175482814567128098509020623654863739197228539946449925577892594667783194019174140177224053906492027814008479465063700700820148751768139656986476815902597185582146284727070605978078894511823945488348445276827366156670303755507474115433582411261056679262368762751023162449086431759226266531174049758164577352632233910284433758042743981099850816544790191658943184252792707349325162613913695560734979746878302087927316458577289480139955269882212245203299184694125165342733137841349671020199734260111420186534472078196943004821011381558040042661294167455749340816356028306882591078444373681567241740290605566131305020533059557126560715499839884768296019624488670465052971862717186589417628954021185483642208527902959990388260257883919179452013110812992396384969965573333053950576661974326883712785363315544323913073109445836071572347480037116995200791994392602132754118135407974028778561916293468963500728098108327821644068498654395825016124076034162558054052345352347491282610738480134818886509558024740458088584546601999237568158790446312078354699975979358000396979699037944957751572234892616739917843244132366528868317702223416594649750526197990843458577361927168473772498652415698180679926392669938648987073048836151249093833335193839379728878750036316072143758592955863478336095411405652084454602034006507413616883789500875620245129785602008478364101446537874787569975931670692042282785420068746656178574460092482966788307063453556499807124890274331421456245864188978294849283300156356709334733056597424787335569463024929057934816845299878638415355316969026583608608203385988139253070906177042047230251597354265667817279953753889216339598949104763900051731096838710295656982208543112900311584701675361794925082663270423598807715933774020079490898414570864642092474729075048448L  

And now plotting it:

H = bw.shape[0]  
W = bw.shape[1]

def tupper(x,y):  
    return 0.5 < ((y//H) // (2**(H*x + y%H))) % 2

plt.rc('patch', antialiased=False)  
for x in xrange(W):  
    for yy in xrange(H):
        y = N + yy
        if tupper(x,y):
            plt.bar(left=x, bottom=yy, height=1, width=1, linewidth=0, color='black')

plt.axis('scaled')  

References

Wikipedia-Tupper's self-referential formula

How does Tupper's self-referential formula work

comments powered by Disqus