Skip to main content

Python reference implementation of Oana & Chiru (2026) — A Mathematical Framework for Four-Dimensional Chess (DOI 10.3390/appliedmath6030048).

Project description

python-chess4d-oana-chiru

Python reference implementation of:

Oana & Chiru, A Mathematical Framework for Four-Dimensional Chess, MDPI AppliedMath 6(3):48, 2026. DOI 10.3390/appliedmath6030048.

The source paper lives in hoodoos/ (treat as read-only reference).

Coordinate convention

Internally 0-based: B = {0,…,7}^4 ⊂ Z^4 (4096 cells). The paper's 1-based {1,…,8}^4 notation is preserved in docstrings and converted only at the UI boundary. See CLAUDE.md for the indexing gotcha (the reference UI is also 0-based; the central mixed-color slice block is at theoretical (z, w) ∈ {4, 5}×{4, 5} / UI (z, w) ∈ {3, 4}×{3, 4}).

Status

0.3.0 — core engine, legality, and corpus tooling are in. Implemented: all six piece types with paper-faithful move generation (rook / bishop / knight / queen / king / pawn), multi-king legality per §3.4 Def 3 (a move is legal iff no king of the mover is attacked afterwards), X-axis castling with global attack safety, Y- and W-axis en passant, pawn promotion on the terminal rank of each forward axis, draw detection (50-move rule + threefold repetition via 4D state hash), .c4d move notation with round-trip I/O, chess-spectral integration (optional), and a random-playout corpus generator writing the chess-maths-the-movie nested layout.

Not yet implemented: search / evaluation, a UI, Oana-Chiru 4D-aware opening books. See CLAUDE.md for architectural invariants.

Spectral encoding (optional)

chess4d integrates with the chess-spectral framework (developed in the sibling mlehaptics repo) for physics-grounded analysis of 4D positions. The encoder maps a GameState to an 11-channel, 45 056-dimensional float32 spectral vector and writes streams of frames as spectralz v4 files. Install with the spectral extra (pulls chess-spectral directly from GitHub, plus numpy and scipy):

pip install -e .[spectral]

Encode a single position:

from chess4d import initial_position
from chess4d.spectral import encode_position

gs = initial_position()
vec = encode_position(gs)  # (45056,) float32

Encode a game and write it to a spectralz v4 file:

from chess4d.spectral import write_spectralz

write_spectralz("game.spectralz", start_state, move_list)

The 11 channels cover the six piece types (with pawns split by forward axis per Oana & Chiru Def. 11) plus board-parity and side-to-move signals. See the chess-spectral notebooks in the mlehaptics repo for channel semantics and reconstruction examples.

Corpus generation

chess4d-corpus-gen writes a reproducible random-playout corpus in the chess-maths-the-movie nested layout:

./corpus/<run_id>/
  manifest.json                    # run metadata + per-game rows
  c4d/game_NNN.c4d                 # compact 4D move notation
  ndjson/game_NNN.ndjson           # per-ply pos4 snapshots + moves
  spectralz/game_NNN.spectralz     # 45 056-dim per-ply encoding (optional)

<run_id> is auto-minted as corpus_YYYYMMDD_HHMMSS_seedN (or ..._unseeded) and can be overridden with --run-id. Generation is two-pass: the playout pass writes c4d + NDJSON unconditionally, then an optional encoding pass reads the NDJSON and produces spectralz frames with absolute ply numbers.

# default: full-game spectralz for every ply
chess4d-corpus-gen --n-games 10 --seed 42 --output ./corpus

# only encode the final 30 plies per game (c4d + NDJSON still full)
chess4d-corpus-gen --n-games 1 --max-plies 500 --encode-last 30

# playout only — c4d + NDJSON, no spectralz, no [spectral] extra needed
chess4d-corpus-gen --n-games 10 --seed 42 --no-encode

# reproducible named run
chess4d-corpus-gen --n-games 10 --seed 42 --run-id fixed-corpus-v1

Retro-encoding an existing corpus

Because encoding reads from the NDJSON sidecar, you can turn a --no-encode corpus into a spectralz corpus at any point without replaying the games:

# encode every ply of every game in an existing run
chess4d-corpus-encode ./corpus/corpus_20260419_180342_seed42

# tail-only: encode just the final 30 plies of each game
chess4d-corpus-encode ./corpus/corpus_20260419_180342_seed42 --last-n 30

The standalone CLI is driven entirely by NDJSON and updates manifest.json in place. For a given (seed, last_n), the retro-encoded spectralz bytes are identical to those produced by chess4d-corpus-gen --encode-last N on the same inputs.

The NDJSON schema is chess4d-ndjson-v1: line 1 is the format header, line 2 is a game_header record with termination / ply count / seed, and subsequent lines carry per-ply records with the applied move, side_to_move, and a full pos4 dict (2-char pawn values Pw/Py/pw/py, 1-char non-pawns, keyed by linear square index (x<<9) | (y<<6) | (z<<3) | w).

Development

pip install -e .[dev]
pytest -v
mypy --strict src/chess4d
ruff check src tests

License

Unlicense (public domain). See LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_chess4d_oana_chiru-0.3.1.tar.gz (131.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_chess4d_oana_chiru-0.3.1-py3-none-any.whl (76.8 kB view details)

Uploaded Python 3

File details

Details for the file python_chess4d_oana_chiru-0.3.1.tar.gz.

File metadata

File hashes

Hashes for python_chess4d_oana_chiru-0.3.1.tar.gz
Algorithm Hash digest
SHA256 66ac0a552744a771789ab72bcd772fc1f7647c206c1f986efa2a0b9259837a7a
MD5 087dff1bd6a4e03d6a75e962cc9866cb
BLAKE2b-256 2d53f5be91c16eebed68b0ed42840c8a9e0d8246c9ac609d2b654fbbb4d7206a

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_chess4d_oana_chiru-0.3.1.tar.gz:

Publisher: publish-to-pypi.yml on lemonforest/python-chess4d-oana-chiru

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_chess4d_oana_chiru-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_chess4d_oana_chiru-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f7ee7a6d3fddae0c74a703e58a271deaa65729191356748bd466c5449309678b
MD5 52d7770c0e951bb7d9ae13b8fda7470c
BLAKE2b-256 1b48260a8491b52b2c6124cca5efe7c4268c8eb282570ddef0e8b66223414f41

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_chess4d_oana_chiru-0.3.1-py3-none-any.whl:

Publisher: publish-to-pypi.yml on lemonforest/python-chess4d-oana-chiru

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page