SilverSight/r/hachimoji_encode.r
allaun 3b6baec64e wip: durability snapshot of local working tree (pre-existing, uncommitted)
Snapshot of previously-uncommitted local work so nothing is lost after the
power outage. NOT reviewed for correctness — a WIP checkpoint, not a feature:
- multi-language hachimoji encoders (c/cpp/fortran/julia/octave/r/scala/go/rust/coq)
- formal Lean WIP (BraidTree, Eisenstein, HachimojiCapture, MathlibConnect,
  ModularFormBridge, ClusterManifold) + lakefile + E8Sidon edit
- docs/, experiments/ (epyc oisc benches), deploy/, scripts, test scaffolding
- .gitignore: exclude **/target/ and Coq build artifacts

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-02 20:49:53 -05:00

136 lines
4.3 KiB
R

#!/usr/bin/env Rscript
# Hachimoji Encoder + AngrySphinx Gate — R Implementation
# Run: Rscript hachimoji_encode.r
LETTERS_HACHIMOJI <- c("Phi", "Rho", "Lambda", "Kappa", "Sigma", "Omega", "Pi", "Zeta")
HA_LETTER_MAP <- new.env(hash = TRUE)
HA_LETTER_MAP[["1"]] <- 0L
HA_LETTER_MAP[["9"]] <- 0L
HA_LETTER_MAP[["28"]] <- 3L
HA_LETTER_MAP[["73"]] <- 0L
HA_LETTER_MAP[["126"]] <- 1L
HA_LETTER_MAP[["252"]] <- 3L
HA_LETTER_MAP[["344"]] <- 5L
HA_LETTER_MAP[["585"]] <- 0L
HA_LETTER_MAP[["757"]] <- 1L
HA_LETTER_MAP[["1134"]] <- 5L
sigma3 <- function(n) {
if (n <= 0) return(0L)
n <- as.integer(n)
total <- 0L
for (d in 1L:n) {
if (n %% d == 0L) {
total <- total + (d * d * d)
}
}
total
}
hachimoji_letter <- function(sigma3_value) {
key <- as.character(sigma3_value)
if (exists(key, envir = HA_LETTER_MAP, inherits = FALSE)) {
get(key, envir = HA_LETTER_MAP)
} else {
as.integer(sigma3_value %% 8L)
}
}
cartan_weight <- function(a, b) {
a <- as.integer(a)
b <- as.integer(b)
if (a == b) {
273L
} else if ((a %/% 2L) == (b %/% 2L)) {
256L
} else {
0L
}
}
angrysphinx_gate <- function(elements) {
n <- length(elements)
collision_count <- 0L
sum_counts <- new.env(hash = TRUE)
for (i in seq_len(n)) {
for (j in i:n) {
s <- as.integer(elements[i] + elements[j])
key <- as.character(s)
if (exists(key, envir = sum_counts, inherits = FALSE)) {
cnt <- get(key, envir = sum_counts)
if (cnt == 1L) {
collision_count <- collision_count + 1L
}
assign(key, cnt + 1L, envir = sum_counts)
} else {
assign(key, 1L, envir = sum_counts)
}
}
}
energy <- max(0L, 273L + 17L * collision_count - 256L * collision_count)
passed <- collision_count <= 1L
list(passed = passed, collisions = collision_count, energy_remaining = energy)
}
hachimoji_encode <- function(n) {
s3 <- sigma3(n)
idx <- hachimoji_letter(s3)
letter <- LETTERS_HACHIMOJI[idx + 1L]
list(n = n, sigma3 = s3, index = idx, letter = letter)
}
# ── Main ──────────────────────────────────────────────────────────────
cat("=== Hachimoji Encoder + AngrySphinx Gate ===\n")
cat(sprintf("Language: R Run with: Rscript %s\n\n", "hachimoji_encode.r"))
# ── sigma3 unit tests ─────────────────────────────────────────────────
stopifnot(sigma3(0) == 0L)
stopifnot(sigma3(1) == 1L)
stopifnot(sigma3(2) == 9L)
cat("[PASS] sigma3 unit tests\n")
# ── Test vector ────────────────────────────────────────────────────────
expected <- data.frame(
n = 1:10,
sigma3 = c(1L, 9L, 28L, 73L, 126L, 252L, 344L, 585L, 757L, 1134L),
letter = c("Phi", "Phi", "Kappa", "Phi", "Rho", "Kappa",
"Omega", "Phi", "Rho", "Omega"),
idx = c(0L, 0L, 3L, 0L, 1L, 3L, 5L, 0L, 1L, 5L),
stringsAsFactors = FALSE
)
cat("Inputs: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n\n")
for (i in seq_len(nrow(expected))) {
n <- expected$n[i]
res <- hachimoji_encode(n)
cat(sprintf("n=%-2d: sigma3=%-5d, letter=%s (%d)\n",
n, res$sigma3, res$letter, res$index))
stopifnot(res$sigma3 == expected$sigma3[i])
stopifnot(res$letter == expected$letter[i])
stopifnot(res$index == expected$idx[i])
}
# ── Gate tests ─────────────────────────────────────────────────────────
cat("\nGate tests:\n")
gate_test <- function(vals, exp_passed, exp_collisions, exp_energy, label) {
res <- angrysphinx_gate(vals)
cat(sprintf(" %s -> passed=%s, collisions=%d, energy=%d\n",
label, res$passed, res$collisions, res$energy_remaining))
stopifnot(res$passed == exp_passed)
stopifnot(res$collisions == exp_collisions)
stopifnot(res$energy_remaining == exp_energy)
}
gate_test(c(1L, 2L), TRUE, 0L, 273L, "[1,2]")
gate_test(c(1L, 2L, 3L), TRUE, 1L, 34L, "[1,2,3]")
gate_test(c(1L, 2L, 3L, 4L), FALSE, 3L, 0L, "[1,2,3,4]")
cat("\nAll tests passed.\n")