Skip to content

Instantly share code, notes, and snippets.

@vsimko
Last active February 2, 2017 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vsimko/09a2f2fcbd0f2ebfb689658e2bf1cfdc to your computer and use it in GitHub Desktop.
Save vsimko/09a2f2fcbd0f2ebfb689658e2bf1cfdc to your computer and use it in GitHub Desktop.
## Load the library after installing GMP
library(gmp)
library(raster)
plot_tupper <- function(k) {
## The tupper's formula
tupper = function(x, y, k){
z1 <- as.bigz(y + k)
z2 <- as.bigq(z1/17)
z3 <- 2^(-17 * x - as.bigz(z1 %% 17))
0.5 < floor(as.bigz(z2 * z3) %% 2)
}
## The x and y axis
x = 0:105
y = 0:16
## Compute the matrix
a <- matrix(0, nc = length(x), nr = length(y))
for(i in seq_along(x)){
a[ ,107 - i] = rev(tupper(x[i], y, k = k))
}
## fancy plot of the matrix
image(t(a), xlim = c(-.05,1.05), ylim = c(-.1,1.1),
col = c("white", "red"), axes = FALSE)
axis(1, seq(0,1, along.with = x), lab = x)
axis(2, seq(0,1, along.with = y), lab = y)
box()
}
## The Tupper's formula k-constant
k = as.bigz("960939379918958884971672962127852754715004339660129306651505519271702802395266424689642842174350718121267153782770623355993237280874144307891325963941337723487857735749823926629715517173716995165232890538221612403238855866184013235585136048828693337902491454229288667081096184496091705183454067827731551705405381627380967602565625016981482083418783163849115590225610003652351370343874461848378737238198224849863465033159410054974700593138339226497249461751545728366702369745461014655997933798537483143786841806593422227898388722980000748404719")
plot_tupper(k)
k = as.bigz("11797141106855527331188906015212023034547618470358870180459302883739790059329963817369939335803289200025277861973972325957380714431535789059393840433249860973507069708025377887506048916190985311845772930209507955424991303924383160273904321222637413658433828104479625625306031653573738003917484660226948301771745521242940777821231075229676161166999188997027202373628792148982040254452713727967120128269125738221031768847415492632975233602660338058908479740243775939815770064197418393410658443527070211588261337114869760")
plot_tupper(k)
k = as.bigz("29884267913300106440505373369056661011210357589969190481608917907314416438074322929159357376637141330314412863126964693465646031770557080340234406930386551801163408125186931844457859660898497123808742662520169080456174773600199636338763959278792712390471533838035395830184209186500889800493051954315622699489772084470410556917257719424799451693356660742892963036745057935860790454713359168181244578442104122980418599221082808683363690288296191485896328928319396489052114499155577610312357982159195576611287072768")
plot_tupper(k)
k = as.bigz("8803083179650014122900592808939551383087598649290659121045457517734138469532940114803750834003046293138684358879523417159943240785111211457413283296513658484893843534659676067216567350308167906637193946148719452921516764928614817489115500945888606184926446211361638714519676376760620144013221515684198313478249622972970856681413749885909432821028133915022787996039777196677181971304226468252811289283016647194051443965614627137106950653286639561277804654235472056902306527812918431275348904224536681084854762971622491063358005968896")
plot_tupper(k)
#' Reads PNG files with 2 colors (0=black, 1=red) of size 106x17
custom_k <- function(filename) {
r <- flip(raster(filename), direction = "y")
m <- (1 - as.matrix(r))[1:17, 1:106]
as.bigz(paste(c("0b", m), collapse = "")) * 17
}
k <- custom_k("~/custom.png")
plot_tupper(k)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment