Skip to content

Add the optional combinatorics library: statistics on permutations and integer partitions - #659

Closed
enumeratio wants to merge 1 commit into
cortex-js:mainfrom
enumeratio:combinatorics-package
Closed

enumeratio wants to merge 1 commit into
cortex-js:mainfrom
enumeratio:combinatorics-package

Conversation

@enumeratio

Copy link
Copy Markdown
Contributor

@cortex-js/compute-engine/combinatorics is a new, opt-in entry point. Importing it declares nothing; loadCombinatorics(ce, { areas }) is synchronous and idempotent per engine and area, and returns a report of the heads and types declared and any head skipped because the engine already defines it. areas is 'permutations', 'partitions', or both (the default). Loading both takes about 10-25 ms; no definition is boxed until its head is first called. The new entry adds about 34 KB gzipped to the esm-min bundle only for those who import it.

const ce = new ComputeEngine();
loadCombinatorics(ce, { areas: ['permutations'] });
ce.expr(['MajorIndex', ['List', 3, 1, 2]]).evaluate(); // 1

What is in the first slice

  • Permutations (about 350 statistics): MajorIndex, Inversions, Denert, ...
  • Integer partitions (about 195 statistics): LargestPart, DurfeeSquare, AddableCells, ...

Elements are the engine's own lists: a permutation is its one-line list (what Permutations yields), a partition is its list of parts (what IntegerPartitions yields). A group-theory Permutation(...) is also read. The domains are permutation and integer_partition, declared as aliases of list<integer>; the exact membership check (a bijection of 1..n, non-increasing positive parts) is in the handler, since a refinement type is not available yet.

Each statistic is an Epsil function literal, compiled ahead of time by scripts/combinatorics/generate.ts into src/compute-engine/combinatorics/compiled.ts as MathJSON, so the entry does not carry the Epsil parser or text. A few carry a TypeScript kernel that a test holds equal to the definition.

Data and FindStat

  • data/combinatorics/examples/: one row per example, per area.
  • data/combinatorics/findstat/: FindStat statistic identifiers, our match records (matches.json), and FindStat's values as test fixtures (values/St*.json), with a README crediting FindStat. The values are mathematical facts, credited to FindStat.
  • data/combinatorics/MANIFEST.json: counts.

All but a few statistics carry a FindStat identifier and equal its values on every permutation up to size 5 and every partition up to size 6 that FindStat tabulates; the others are held to their examples. A generate.ts --check step in the corpus-pipeline job fails on drift.

Tests

Jest runs the data directly (test/compute-engine/combinatorics/): every example row through evaluate(), with .N(), the Epsil route and the compiler's refusal on the first row of each head; each head against FindStat's values on small objects; each kernel against its definition; the loader; and the precompiled file against the sources. The standard run takes about 90 s. CE_NIGHTLY=1 runs every row on every route and a sweep to size 7 (test/compute-engine/nightly/combinatorics-sweep.test.ts).

What declines

  • compile(): no statistic has a compiled form; the compiler declines and the call falls back to the interpreter.
  • Out of domain: a list outside the domain, or one that is not a list of integers, makes evaluate return undefined, so the call stays unevaluated (MajorIndex([1, 1])). No Error, no NaN.

Naming

A statistic defined on several collections gets one head per collection, because every domain is a list<integer> alias and one operator could not tell them apart. The permutation statistic keeps the bare name (MajorIndex) and the others are prefixed by the collection (WordMajorIndex). The two areas in this slice share no head.

Part of #531

…binatorics): loadCombinatorics declares about 350 statistics of a permutation and about 195 of an integer partition as operators over the engine's own lists, each defined by an Epsil function literal precompiled to MathJSON, with an optional TypeScript kernel held equal to it, identified with a FindStat statistic and checked against its values; a list outside the domain stays unevaluated and a statistic declines compile(). Part of cortex-js#531
@arnog

arnog commented Nov 23, 2026

Copy link
Copy Markdown
Member

Landed in f42a02d, with fixes made while landing (see the commit message). Thanks!

@arnog arnog closed this Nov 23, 2026
arnog added a commit that referenced this pull request Nov 23, 2026
…(m, n, d), the optional combinatorics library, PiecewiseExpand

- #657: @cortex-js/compute-engine/runtime exports sysHelpers, the stateless
  helpers that compiled code calls as _SYS.<name> (#527 item 1).
- #658: Mod(m, n, d) is d + Mod(m - d, n), in [d, d + n) (#548). Every
  compile target declines the three-operand form; the derivative, limit,
  enclosure and measurement code leave it unevaluated. Fixed on landing:
  .N() reads exact operands exactly as the two-operand Mod does (exact
  cancellation of d, a symbol with an exact value near a jump, a dividend
  past the range of a double, no second evaluation of an impure operand);
  exact radicals stay exact; the type is real only for real operands.
- #659: the optional combinatorics entry, loadCombinatorics(ce, { areas }),
  with about 350 permutation and 195 integer-partition statistics checked
  against FindStat (#531). Fixed on landing: the report of a re-load, a
  skipped head declared once the engine's definition is removed, the CI
  comment, string nodes without source offsets in compiled.ts, the synonym
  head LargestCycleLength removed (LongestCycleLength stays), one compiler
  decline test per area, and notes on list literals and time limits.
- #660: PiecewiseExpand rewrites Abs, Sign, Min, Max and Heaviside into a
  Which (#516). Fixed on landing: an argument declared complex or number is
  not split; the arm limit is checked before the product is built; Max()
  and Min() with no operand; exact conditions between radicals; an impure
  expression is returned unchanged; the lazy-operator list and the search
  ranking test.
- Issues CE-1734, CE-1735, CE-1736 filed from the decline census of #638.

Co-authored-by: enumeratio <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants