Skip to main content

mapFolding

PyPI Python versions Python Tests License: CC BY-NC 4.0

Exact enumeration tools for map folding, stamp folding, semi-meanders, and meanders.

mapFolding is a typed Python research package for counting distinct foldings of one- and multidimensional maps. It provides:

  • stable dispatch functions for ordinary, rotationally symmetric, and divided computations;
  • native algorithms for map folding, semi-meanders, and meanders;
  • a unified interface to 33 implemented OEIS sequences, including exact formula relationships;
  • readable source algorithms, generated optimized implementations, and optional NumPy, Numba, Pandas, and Codon routes; and
  • cross-implementation tests against known sequence values.

Earlier versions of this project were used to compute new terms for OEIS A001415, the number of ways to fold a 2 × n strip of stamps.

This is exact combinatorial enumeration: running time and memory requirements grow quickly. Start with small inputs. The supported high-level interfaces are in mapFolding.basecamp and mapFolding.oeis; other modules include evolving research code.

Quick start

Quick start your exploration of a new-to-you algorithm? Quick start your idea for improving an algorithm? Quick start your dissection of an algorithm's states?

Considering the versatility and sophistication of this package, you can accomplish many things quickly, but a "Quick start" section doesn't make sense.

Public interfaces

Goal Interface
Count all foldings of a map mapFolding.basecamp.countFolds(mapShape, ...)
Count rotationally symmetric foldings mapFolding.basecamp.countFoldsSymmetric(mapShape, ...)
Count semi-meanders or meanders mapFolding.basecamp.countMeanders(kind, n, ...)
Calculate any implemented OEIS term mapFolding.oeis.oeisIDfor_n(oeisID, n, ...)
Convert a map-folding OEIS index to dimensions mapFolding.oeis.makeMapShape(oeisID, n)
Retrieve cached known values mapFolding.oeis.getValuesKnown(oeisID)
List implemented sequences getOEISids

oeisIDfor_n dispatches to the appropriate folding algorithm, meander algorithm, symmetric-folding algorithm, or exact formula. Run getOEISids for the current list and descriptions of all supported sequences.

The map-folding sequence mappings are:

OEIS ID Problem mapShape for index n
A000136 Strip of n labeled stamps (1, n)
A001415 2 × n strip (2, n)
A001416 3 × n strip (3, n)
A001417 n-dimensional 2 × ⋯ × 2 map (2,) * n
A195646 n-dimensional 3 × ⋯ × 3 map (3,) * n
A001418 n × n sheet (n, n)
A007822 Symmetric foldings of 2n + 1 stamps (1, 2 * n)

For A007822, pass (1, 2 * n) to countFoldsSymmetric; the function's computational shape differs from the 2n + 1 stamps in the sequence description.

OEIS metadata and b-files are cached locally for 30 days. Missing or stale entries are refreshed from oeis.org; stale cached data remains available if a refresh fails.

Algorithm selection and long computations

Assume this is always out of date.

Leave flow='' for the default implementation. The alternate selectors exist for research, validation, and performance comparisons:

Interface Supported flow values
countFolds '', daoOfMapFolding, numba, theorem2, theorem2Codon, theorem2Numba, theorem2Trimmed
countFoldsSymmetric '', asynchronous, theorem2, theorem2Codon, theorem2Numba, theorem2Trimmed
countMeanders '', matrixMeanders, matrixNumPy, matrixPandas

The Numba, Pandas, and Codon selectors require their corresponding extras. For OEIS sequences with multiple exact identities, the f argument to oeisIDfor_n selects a formula; leaving it empty uses the default route.

Pass pathLikeWrite to a counting function to preserve a result. An existing directory receives a generated filename such as p2x6.totalFolds; an explicit target file is also supported. The destination is write-tested before computation, and an existing target is not overwritten.

countFolds can split work with an integer, computationDivisions='cpu', or computationDivisions='maximum'. Dividing this algorithm repeats substantial work and is usually slower, so leave computationDivisions=None unless you are deliberately studying the parallel implementation.

Repository guide

This is a little out of date.

Path Role
mapFolding/basecamp.py Stable high-level dispatch for folding and meander computations
mapFolding/oeis/ OEIS dispatch, formulas, metadata, and cached values
mapFolding/algorithms/ Handwritten source algorithms
mapFolding/synthesized/ Generated implementations; regenerate these instead of editing them directly
mapFolding/kitAST/ Project-specific AST transformations and module generators
mapFolding/_e/ Experimental elimination-based algorithms and analysis
mapFolding/tests/ Main correctness, dispatch, filesystem, and parameter tests
mapFolding/reference/ Historical implementations, completed jobs, notes, and research artifacts
easyRun/ Benchmark and exploration harnesses

General-purpose transformation primitives developed alongside this project now live in astToolkit and astToolFactory. The transformation pipeline retained here is specific to generating and validating mapFolding implementations.

Installation

mapFolding requires Python 3.13 or newer.

pip install mapFolding

To avoid bloat, some packages are optional:

Extra Purpose
pandas Pandas and Arrow meander implementation
codon Codon-compiled implementations on Linux
ortools Experimental constraint-propagation work
testing Test dependencies
development Broader development and analysis dependencies

For example:

pip install "mapFolding[codon,pandas]"

Development, or to have the archives, notes, and other supplemental materials

Create and activate a virtual environment, then install both development extras:

git clone https://github.com/hunterhogan/mapFolding.git
cd mapFolding
python -m venv .venv
pip install -e ".[development,testing]"

The test suite compares independent implementations with known OEIS values and stored data samples. When adding an algorithm variant, begin with mapFolding/tests/test_computations.py and register the new flow beside the existing implementations.

Citation

To cite this software, use the metadata in CITATION.cff. BibTeX files for the mathematical literature are collected in citations/.

Research references

Sur les Chevauchements des Permutations

Folding a strip of stamps

A map-folding problem

Multi-dimensional map-folding

A transfer matrix approach to the enumeration of plane meanders

Stamp Foldings, Semi-Meanders, and Open Meanders: Fast Generation Algorithms

Foldings and meanders

jOEIS: Java Online Encyclopedia of Integer Sequences

The Online Encyclopedia of Integer Sequences

Computing new values

  • Folds of multidimensional map (2, 2, 2, 2, 2, 2, 2, 2), which is A001417(8): estimated 14 days with my current hardware and a non-published algorithm.
  • Folds of multidimensional map (2, 21), which is A001415(21): estimated 6 days with my current hardware and one of the compiled "Theorem 2" algorithm versions.
  • Folds of multidimensional map (2, 22), which is A001415(22): estimated 15 days with my current hardware and one of the compiled "Theorem 2" algorithm versions.
  • Folds of multidimensional map (3, 15), which is A001416(15): estimated 11 days with my current hardware and one of the compiled "Theorem 2" algorithm versions.
  • Semi-meanders with 46 crossings, which is A000682(46): estimated 1–2 hours if I had 64 GB of system memory.

Computation times assume:

  1. No power interruptions. (I don't have a UPS, and in the last two months the longest gap between power interruptions was 8 days 6 hours.)
  2. No system crashes.
  3. Single-core computations are given priority over other tasks.
  4. The core of single-core computations is regularly boosted.
  5. No heat issues.

Status reminders

  1. Acceleration
    1. Building Py 3.14z (tail-call) is not working.
    2. Codon: new release, new features.
  2. Remote execution
    1. Google Colab: memmap causes everything to go to "disk" and the physical memory is unused.
    2. GitHub Codespaces: ignores memmap and terminates on OOM.
  3. astToolkit: figure out a better container system than Ingredients Module, Ingredients Function, etc.
  4. Dao of map folding
    1. Overhaul how I store and access type metadata for ndarray and other containers. Affects:
      1. StateMapFolding
      2. Shatter dataclass
      3. ast transformations
    2. Run 2^8 without a crash or power outage.
    3. Compute (2, 21), (2, 22), and (3, 15).
  5. Symmetric 1xn
    1. Symmetry algorithm is too slow.
    2. Asynchronous + accelerated doesn't work yet.
  6. Matrix Meanders
    1. I need a lot more tools in kitAST.
    2. I hate getTotalBuckets but it should be a brilliant function.
  7. Permutations
    1. The base algorithm is a dumpster fire.
    2. Bilateral concurrent can't produce a folding, but uses too much memory and is too slow to count folds.
  8. Reduce arches: slower than counting by hand.
  9. _e
    1. Constraint propagation or SAT
      1. ortools won't apply concurrency, and I don't know how to think about divisions of the problem space that actually reduce computation time.
      2. Understanding other packages is harder than a self-enema with a firehouse.
    2. 2^n-dimensional (aka elimination by crease)
      1. Computation algorithms needed for: conditional predecessors, conditional successors, DomainLeaf首零Plus零 in dimension零, the range of a pile (aka pile options, aka choices leaf), pinPile二ByCrease, pinPile二Ante首ByCrease, and pinPile零Ante首零AfterDepth4.
      2. Acceleration.
      3. Bug in boxOfFunctionsReduction2上nDimensional.
      4. Finish collecting 2^7 data.
      5. Fastest algorithm for 2^5. Can't complete 2^6. I mean, wtf?
      6. PermutationSpace and its OOP structure: I don't really know what I am doing.
      7. The reduce-it concept is strong: reorient more code around it. Resolve the tension with PermutationSpace._solidifyLeafSpace().
    3. Elimination: atrophied.
    4. Insert leaves: But for the data storage, it would be powerful. IDK anything about data compression. I lost the code I wrote for the notation I created to store graphs, and it seems I am too stupid to recreate or reverse engineer my own code from my own files AND OTHER FUNCTIONS. fml. Ironically, LLMs can't figure it out either because I guess my idea was novel. F.M.L.

Hunter, this list is incomplete: you quit writing after you got upset. Resume at mapFolding.kitAST.

  1. Research 1
    1. Finish run D11.
    2. Start partial runs in the R series.
    3. Make NumPy version.
    4. 2-sequence representation, 3-sequence representation, or something else?
    5. Hypothesis: relationships are best described by the sum of n and hypothetical n, modulated by parity.

My recovery

2011 August: Homeless since YouTube channel subscribers

CC-BY-NC-4.0

Metadata

Release files for mapFolding 0.37.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mapFolding 0.37.1
File Size Uploaded
mapfolding-0.37.1.tar.gz 411.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mapFolding 0.37.1
File Interpreter ABI Platform
mapfolding-0.37.1-py3-none-any.whl Python 3 none any Details

Total release size: 921.5 kB

Release files / mapfolding-0.37.1.tar.gz

Download URL mapfolding-0.37.1.tar.gz
Size 411.0 kB
Tags Source
SHA-256 checksum
How to use checksums
db79ba014918fdac10cb63e88eaa14433b4c0b8b95cf25e35f08eca1f2f43486
BLAKE2b-256 checksum
How to use checksums
6edb83b7b107ba523ade2aea994caa27138835b319bbceb1bb9766da1d7b11dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / mapfolding-0.37.1-py3-none-any.whl

Download URL mapfolding-0.37.1-py3-none-any.whl
Size 510.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
141fd39e8a2169c19c3647fc7dcbe60a3324cb591958abfe1ab588162881fde0
BLAKE2b-256 checksum
How to use checksums
ed82339de6bf1cff4c2cc6c5143bf2189c3c7ad563a5bf6f062b631141be38ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

0.37.2

2 release files

This release

0.37.1 This release

2 release files

0.37.0

2 release files

0.36.0

2 release files

0.35.1

2 release files

0.35.0

2 release files

0.34.0

2 release files

0.33.0

2 release files

0.32.0

2 release files

0.31.0

2 release files

0.30.0

2 release files

0.26.0

2 release files

0.25.0

2 release files

0.24.1

2 release files

0.24.0

2 release files

0.23.1

2 release files

0.23.0

2 release files

0.19.0

2 release files

0.18.4

1 release file

0.18.3

2 release files

0.18.2

2 release files

0.18.1

2 release files

0.17.1

2 release files

0.17.0

2 release files

0.16.2

2 release files

0.16.1

2 release files

0.16.0

2 release files

0.15.4

2 release files

0.15.3

2 release files

0.13.1

2 release files

0.13.0

2 release files

0.12.3

2 release files

0.12.2

2 release files

0.12.1

2 release files

0.12.0

2 release files

0.11.4

2 release files

0.11.1

2 release files

0.11.0

2 release files

0.10.0

2 release files

0.9.5

2 release files

0.9.4

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.6

2 release files

0.8.5

2 release files

0.8.4

2 release files

0.8.3

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.12

2 release files

0.3.11

2 release files

0.3.9

2 release files

0.3.8

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page