Skip to main content

Spectral lattice-fermion encoder for 2D and 4D chess (D4 / B4 symmetry, graph-Laplacian eigenbasis, 640-dim / 45 056-dim HDC)

Project description

chess_spectral (Python)

Python reference implementation of the 640-dim (2D) and 45 056-dim (4D) spectral chess encoders, sibling of the C17 port in ../src/. Use this for REPL / LLM / notebook analysis; use the C binary for batch throughput.

Install

From PyPI (recommended):

pip install chess-spectral

The base install pulls only numpy and scipy. If you need PGN ingest via chess_spectral.corpus, request the [corpus] extra to add python-chess:

pip install "chess-spectral[corpus]"

Package page: https://pypi.org/project/chess-spectral/

From source

Editable install from a local checkout:

pip install -e docs/chess-maths/chess-spectral/python/

From a git URL (pin a commit in production):

pip install "git+https://github.com/lemonforest/mlehaptics.git@COMMIT#subdirectory=docs/chess-maths/chess-spectral/python"

After install, two console scripts are on your $PATH:

chess-spectral --help            # 2D CLI (formerly `python spectral_py.py`)
chess-spectral-4d --help         # 4D CLI

Both packages also expose __version__:

>>> import chess_spectral, chess_spectral_4d
>>> chess_spectral.__version__, chess_spectral_4d.__version__
('1.1.3', '1.1.3')

In-place (no install)

The legacy workflow still works: every test and analysis script uses sys.path.insert to bootstrap off the python/ directory, so pytest docs/chess-maths/chess-spectral/python/tests/ runs without any install.

Output is byte-identical to the C CLI — the spectral csv command here produces the same bytes as the C spectral csv does on the same input file. The Python CLI (chess-spectral, entry point chess_spectral.cli:main) mirrors the C CLI subcommand-for-subcommand.

Layout

chess_spectral/
  encoder.py             # encode_640(pos) → np.ndarray(640,)
  frame.py               # v2 .spectral[z] binary I/O + transparent gzip
  csv_export.py          # dist_prev / cos_prev / energies CSV
  cli.py                 # `chess-spectral csv file.spectralz` (2D CLI)
  phase_operators/       # §11 phase-space move generator + is_check (1.2.0+)
chess_spectral_4d/
  cli.py                 # `chess-spectral-4d tables-verify --phase all`
pyproject.toml           # PEP 621 packaging metadata
tests/
  test_parity.py         # Python output == C output (5 tests)
  phase_operators/       # §11 validation suite (92 tests; 1.2.0+)

Quick start

>>> from chess_spectral import (
...     encode_640, channel_energies, read_encodings, fen_to_pos,
... )

>>> pos = fen_to_pos("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
>>> enc = encode_640(pos)
>>> enc.shape
(640,)

>>> channel_energies(enc)
{'A1': 0.0, 'A2': 19.845, 'B1': 45.2825, 'B2': 45.2825,
 'E': 322.57, 'F1': 88.77, 'F2': 1851.01, 'F3': 1507.65,
 'FA': 19.92, 'FD': 0.0}

# Read a whole game that was encoded by either C or Python
>>> hdr, arr = read_encodings("game.spectralz")  # transparent gzip
>>> arr.shape
(161, 640)

CLI

python spectral_py.py csv        game.spectralz -o game.csv
python spectral_py.py encode     -i game.ndjson -o game.spectralz -z
python spectral_py.py encode-fen --fen "..."   -o single.spectral
python spectral_py.py version

Phase operators (§11)

chess_spectral ships a phase-space move generator and check detector as the chess_spectral.phase_operators subpackage (added in 1.2.0). The primitives compute all moves and check relationships as modular arithmetic on a single integer per square — phi(r, c) = r·67 + c·7 mod 640 — rather than geometric coordinates. They are a drop-in equivalent to python-chess's pseudo_legal_moves + is_check, validated at 100% on the reference corpus, and compose naturally with the spectral encoder's coprime phase structure.

import chess
from chess_spectral.phase_operators import (
    occupation_aware_moves_c,   # pseudo-legal dests from a square
    available_castles,          # legal castles for side-to-move
    phasecast_is_check,         # is the mover's king attacked?
    move_leaves_king_in_check,  # would this move expose our king?
)

board = chess.Board()

# Pseudo-legal destinations for the knight on b1 (the 1,2 indexed
# from bottom-left: row 0, col 1). mover_charge is +1 for white.
dests = occupation_aware_moves_c(board, "N", 0, 1, +1)
# -> frozenset({(2, 0), (2, 2)})   (a3 and c3)

# Check detection
phasecast_is_check(board)  # False on the starting position

Validation coverage (see PHASE_OPERATOR_SUPPLEMENT.md for the full research record):

  • Empty-board pseudo-legal destinations for every piece type at every square — 416 / 416 pairs (§11.3).
  • Occupation-aware pseudo-legal moves including en passant and castling — 1153 / 1153 at n=100 positions (§11.4).
  • is_check on arbitrary pseudo-legal transitions — 3393 / 3393 (§11.5 path 1).

When to use phase operators vs python-chess

Both give you the same answers. The phase-space formulation is faster than python-chess's pseudo_legal_moves in pure Python (~7 µs/piece for the fastest solution vs ~15–20 µs for python-chess's equivalent), and slower than python-chess's bitboard is_check (phasecast_is_check runs ~138 µs vs ~22 µs for python-chess). The subpackage exists because the phase-space formulation is the substrate the §11/§12 spectral research is built on — not because it universally out-performs python-chess. If you're writing a search engine hot loop, python-chess's bitboards are the right choice. If you're doing phase-space research (similarity, partition detection, encoder composition) or need the operators as primitives in C via a future codegen port, the subpackage is the right choice.

Parity with C

The encoder uses the same tables (PAWN_ANTI_FIBER, DIAG_DEV) as the codegen that feeds the C side. Tables are rebuilt at import time from encoder_512 primitives plus the directed pawn adjacency from chess_pawn_laplacian — so both implementations can be verified from first principles.

Run the parity suite:

python tests/test_parity.py

The critical test is test_csv_matches_c_byte_for_byte — reads the C- produced .spectral file, runs the Python CSV exporter, asserts the output equals the C CSV bit-for-bit.

Why two implementations

C Python
Throughput µs/encode ms/encode
REPL / notebooks
LLM-pasteable binary code
Scipy / numpy exploration
Embeds in mobile / web
Exact numerical reference tables baked at build rebuilt from primitives

Develop new channels in Python first (faster iteration, scipy.linalg at hand, no rebuild loop). Once the math is frozen, port to C and verify parity via the test suite.

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

chess_spectral-1.2.6.tar.gz (1.8 MB view details)

Uploaded Source

Built Distributions

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

chess_spectral-1.2.6-cp313-cp313-win_amd64.whl (3.2 MB view details)

Uploaded CPython 3.13Windows x86-64

chess_spectral-1.2.6-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

chess_spectral-1.2.6-cp313-cp313-macosx_11_0_arm64.whl (3.3 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

chess_spectral-1.2.6-cp312-cp312-win_amd64.whl (3.2 MB view details)

Uploaded CPython 3.12Windows x86-64

chess_spectral-1.2.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

chess_spectral-1.2.6-cp312-cp312-macosx_11_0_arm64.whl (3.3 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

chess_spectral-1.2.6-cp311-cp311-win_amd64.whl (3.2 MB view details)

Uploaded CPython 3.11Windows x86-64

chess_spectral-1.2.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

chess_spectral-1.2.6-cp311-cp311-macosx_11_0_arm64.whl (3.3 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

chess_spectral-1.2.6-cp310-cp310-win_amd64.whl (3.2 MB view details)

Uploaded CPython 3.10Windows x86-64

chess_spectral-1.2.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

chess_spectral-1.2.6-cp310-cp310-macosx_11_0_arm64.whl (3.3 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file chess_spectral-1.2.6.tar.gz.

File metadata

  • Download URL: chess_spectral-1.2.6.tar.gz
  • Upload date:
  • Size: 1.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for chess_spectral-1.2.6.tar.gz
Algorithm Hash digest
SHA256 a7ae5c39232a7b45061165933dba45588bde5a8aa0a8dbdac58a74bcb5683ab4
MD5 a708d6e736c5a182bbd5242886ed3a2e
BLAKE2b-256 a3184db28d8ab141110cc5fda66380836bd36f77177a3365e3b3d75bdb5e25ee

See more details on using hashes here.

Provenance

The following attestation bundles were made for chess_spectral-1.2.6.tar.gz:

Publisher: chess-spectral-publish.yml on lemonforest/mlehaptics

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

File details

Details for the file chess_spectral-1.2.6-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for chess_spectral-1.2.6-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 37ea16a9d96da6eb40be53badff35d64e9b3c7c4fd12b1cbe5e2560be54f5018
MD5 7f48ca698560f271ec6d7aa2eab22fb6
BLAKE2b-256 55631c47eb2b32810f75cfeb2cf6e0a67713e50b4de000c4eaf66d371ddccc2b

See more details on using hashes here.

Provenance

The following attestation bundles were made for chess_spectral-1.2.6-cp313-cp313-win_amd64.whl:

Publisher: chess-spectral-publish.yml on lemonforest/mlehaptics

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

File details

Details for the file chess_spectral-1.2.6-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for chess_spectral-1.2.6-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 58e114164d44cc361f1da62b1e9d91011a53847e1d47ec1e8da01a37e06f1f9f
MD5 3aa7b880731cf6ca69618bb48af557f0
BLAKE2b-256 20092b050a23867b39e69e9b2fd31cdc6495024429a27145792a8254d561b25f

See more details on using hashes here.

Provenance

The following attestation bundles were made for chess_spectral-1.2.6-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl:

Publisher: chess-spectral-publish.yml on lemonforest/mlehaptics

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

File details

Details for the file chess_spectral-1.2.6-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for chess_spectral-1.2.6-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0e6112a94227e7fe297ac39eb14633c5e9b69209922d6865373c1d0dd6fcf1d5
MD5 3163f191223bf4a353328c528b189522
BLAKE2b-256 cea0a1f2414d7843343b32935f0e84fe77657880b4aa0b0561028aa9b03cb4a8

See more details on using hashes here.

Provenance

The following attestation bundles were made for chess_spectral-1.2.6-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: chess-spectral-publish.yml on lemonforest/mlehaptics

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

File details

Details for the file chess_spectral-1.2.6-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for chess_spectral-1.2.6-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 aecf77ca3c0d926a5badb9dfc8c6cf75bc79966f24aa8041d53aa474e05db833
MD5 ea44fa450e1af8ec041f5ea0a5aa2e79
BLAKE2b-256 14e6764f0153cdfcbc336514affb7cb1f0ea682865377358d95dbf11589149ed

See more details on using hashes here.

Provenance

The following attestation bundles were made for chess_spectral-1.2.6-cp312-cp312-win_amd64.whl:

Publisher: chess-spectral-publish.yml on lemonforest/mlehaptics

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

File details

Details for the file chess_spectral-1.2.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for chess_spectral-1.2.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b21af9b2a2895853c73fc8032900cc9eb746374d020f4873ead263a320da63b7
MD5 e0b355811a84289bd0ac2614557c1201
BLAKE2b-256 5de9e35cae180b68756218bf8cf868312828d83674c0e4daf220bdcfb6de1ac0

See more details on using hashes here.

Provenance

The following attestation bundles were made for chess_spectral-1.2.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl:

Publisher: chess-spectral-publish.yml on lemonforest/mlehaptics

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

File details

Details for the file chess_spectral-1.2.6-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for chess_spectral-1.2.6-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cba382a72cb6f7842584e0c40e58260921bce90487499df27244555cc19daa21
MD5 57d93df0a8da6c69d6a7a45ba7ee3387
BLAKE2b-256 ceda157d41fff257aea37f82c1a675edb002cb0b16969669f215b4a778f9033d

See more details on using hashes here.

Provenance

The following attestation bundles were made for chess_spectral-1.2.6-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: chess-spectral-publish.yml on lemonforest/mlehaptics

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

File details

Details for the file chess_spectral-1.2.6-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for chess_spectral-1.2.6-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 b66fcb4a5942f07acbfde644d6f407b1ef45bdd76d0cd58d9f5a518d830d7c1b
MD5 982ff3376ede1dc606527321074a87d7
BLAKE2b-256 dabfaf5e2290a5cde7f4194f3dfd2e5a91f6325bda0a6aaa5380a28b4363dcd0

See more details on using hashes here.

Provenance

The following attestation bundles were made for chess_spectral-1.2.6-cp311-cp311-win_amd64.whl:

Publisher: chess-spectral-publish.yml on lemonforest/mlehaptics

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

File details

Details for the file chess_spectral-1.2.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for chess_spectral-1.2.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c08c3eb8c6a02e1e1caa99137757408ced716c54bf76de04af0e3a9bf2c4f803
MD5 262ef0e42bcaae54d941853cc9091eb7
BLAKE2b-256 d9b3bb92ee69f71fec0a1b40a7ce10e45062fd4000335a2db444ee62a55a6033

See more details on using hashes here.

Provenance

The following attestation bundles were made for chess_spectral-1.2.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl:

Publisher: chess-spectral-publish.yml on lemonforest/mlehaptics

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

File details

Details for the file chess_spectral-1.2.6-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for chess_spectral-1.2.6-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3ac655b24bc37d522770e344a79a89d735a3e072276e262b5199249e9bd4421b
MD5 3f088fc7de6292110e9041a649eb92e8
BLAKE2b-256 36a76d4162cb57584231ba584fab085e99daa598560bf4bd83a88cf07425949f

See more details on using hashes here.

Provenance

The following attestation bundles were made for chess_spectral-1.2.6-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: chess-spectral-publish.yml on lemonforest/mlehaptics

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

File details

Details for the file chess_spectral-1.2.6-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for chess_spectral-1.2.6-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 39a556ccfec9b7ac4b94a50c34d9b53b87a583a25ef015fc947be0fe55e584c1
MD5 c458a22221921f7ecf27fdb1b23a46d4
BLAKE2b-256 bad431e3d4f4b949258a94014b57a5ea98f30b3334ed562b1342d9204b7db45d

See more details on using hashes here.

Provenance

The following attestation bundles were made for chess_spectral-1.2.6-cp310-cp310-win_amd64.whl:

Publisher: chess-spectral-publish.yml on lemonforest/mlehaptics

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

File details

Details for the file chess_spectral-1.2.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for chess_spectral-1.2.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7ad0bafb078f7aacb5859a1489ef3cccf3bc9b12e5ebeea354147e0967eebd17
MD5 f74a23e1a3fa5fd405190f77273e0838
BLAKE2b-256 7c9a0428d803242da998c342954d2bb7628d380cfe8ee76c81d87f9456cc0949

See more details on using hashes here.

Provenance

The following attestation bundles were made for chess_spectral-1.2.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl:

Publisher: chess-spectral-publish.yml on lemonforest/mlehaptics

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

File details

Details for the file chess_spectral-1.2.6-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for chess_spectral-1.2.6-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ebf481df147b354558ff3ebd7916467070164efae6d5f7f4a613bd0ec44802a9
MD5 472dd3d97681d011ff0400c6111f3094
BLAKE2b-256 2551101a43748379392fbc89050352352a0b962d4699c686ad975d01dce7e17f

See more details on using hashes here.

Provenance

The following attestation bundles were made for chess_spectral-1.2.6-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: chess-spectral-publish.yml on lemonforest/mlehaptics

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