Skip to content

Capped Compositions with Repetition#59

Merged
jwood000 merged 29 commits intomainfrom
repcapped-compositions
Mar 2, 2026
Merged

Capped Compositions with Repetition#59
jwood000 merged 29 commits intomainfrom
repcapped-compositions

Conversation

@jwood000
Copy link
Owner

@jwood000 jwood000 commented Mar 2, 2026

Summary

This PR adds end-to-end support for capped (maximum-part) compositions with repetition across counting, generation, ranking, unranking (nth), sampling, and iteration, including weak and mapped-zero variants where applicable. It also hardens binomial coefficient wrappers (double and GMP) against invalid inputs to avoid unsafe signed→unsigned behavior, and fixes permuteIter() correctness for permutation-of-partition constrained iterator cases.

Key Changes

  • Added capped repetition composition support across compositionsGeneral(), compositionsRank(), compositionsSample(), and compositionsIter().

  • Implemented exact inclusion–exclusion counting in the GMP path, with double wrappers computing the full sum exactly before converting to double.

  • Hardened nChooseK() and nChooseKGmp() to return 0 for invalid (n, k) and avoid unsafe signed→unsigned conversion into GMP binomial routines.

  • Fixed iterator dispatch for permutation-of-partition constrained iterator cases so permuteIter() produces correct iteration results.

  • Added tests covering the new capped composition families and permutation-of-partition iterator equivalence checks.


No existing APIs were removed; changes are additive and backward compatible.

Introduce a templated PopulateMat for writing combination/permutation
results directly into a column-major matrix buffer, with explicit
instantiations for int and double
Use clearer, more descriptive function names in declarations,
definitions, and explicit instantiations
Introduce `CountCompsRepLenCap` (double and GMP) and wire it into
dispatch for `CompRepCapped` and `PrmRepCapped` via the new
`CompsRepLenCap` count class.
Append CompRepCapped to keep enum values stable, include it in capped
type lists, and classify capped repetition compositions in
DiscoverPType.
Introduce FillTailRep to rebuild the tail while preserving the total
sum, and add a NextCompositionRep overload that advances compositions
under a per-part cap.
Introduce `CompsGenRepCapped` overloads for raw pointers and `RMatrix`
to generate compositions using `v.size() - 1` as the cap, with explicit
template instantiations for `int` and `double`.
- Add `CompRepCapped` and `PrmRepCapped` dispatch paths in
  `PartitionsManager`
- Split permutation-with-repetition generation into `PartsStdPermRep`
  and a new `PartsGenPermRep` overload
- Consolidate population helpers via `PopulateUtils` and
  `CompositionsUtils` includes
Introduce `nthCompsRepCapped` (double) and `nthCompsRepCappedGmp` (mpz)
and wire them into `GetNthPartsFunc`. Also correct the final element
computation for positive/offset representations and document the offset
and block-scan invariants.
Permutation-partition (Prm*) cases lack a NextPartition-based iterator;
dispatch them to the general constraint machinery to avoid invalid
partition state and preserve correct output.
Add sum/== permutation cases that validate results against
partitionsGeneral + permuteGeneral, and include table-input equivalence
checks. This clearly tests all of the permutation PartitionTypes.
Route `PartitionType::CompRepCapped` to a dedicated `NextRepCompCapped`
wrapper and initialize only the cap (`pivot`) in `SetPartValues`,
documenting why other iterator state is unused for this mode
Introduce `rankCompsRepCapped` and GMP variant, and wire them into
`GetRankPartsFunc` for `PartitionType::CompRepCapped`.
mpz_bin_uiui takes unsigned arguments; return 0 for invalid (n < 0, m <
0, m > n) to avoid unintended huge computations and keep combinatorial
conventions.
Use the mpz_class implementation for inclusion–exclusion to avoid
precision loss from large intermediate binomial terms, and move the
detailed formula documentation to the GMP routine.
Compute double counts only when below Significand53 and fall back to mpz
implementations when the double result is non-finite or exceeds the
limit.
Exercise capped composition generation with repetition, including mapped
inputs, parallel execution, GMP bigz lower bounds, and sample/rank
round-trips.
Update declarations, definitions, and call sites to use the corrected
name.
Introduce `CountCompsRepCapZNotWk` and `CompsRepZeroCap`, and wire the
new `PartitionType::CmpRpCapZNotWk` factory case to use it.
Thread one_or_zero through CompsGenRepCapped, NextCompositionRep(cap),
and FillTailRep to share logic between 0- and 1-based representations.
Introduce `PartitionType::CmpRpCapZNotWk` and route generation through
`CompsGenRepCapped<1>`. Adjust capped start vector discovery to account
for `includeZero` and update related type lists and comments.
Extend `PartitionType` and name mappings with `CompRepWeakCap`, and
include it in `CappedPTypeArr` while keeping existing numeric values
stable.
Route new PartitionType variants through NextCompositionRep and
initialize `pivot` (cap) for these modes in Partitions::SetPartValues.
- Implement nth/rank functions for `CmpRpCapZNotWk` (dbl and GMP paths)
- Introduce `CompRepWeakCap` partition type and route
  counting/generation to capped logic
- Update partition type discovery and function dispatch tables
  accordingly
Extend testthat suites to exercise `compositionsGeneral`,
`compositionsRank`, and `compositionsSample` across `CmpRpCapZNotWk` and
`CompRepWeakCap` (including mapped and bigz/GMP cases, plus parallel
execution).
Expand NEWS entries to cover distinct and capped compositions, parallel
ranking, performance improvements, bug fixes, and update the 2.10.0
release date.
@jwood000 jwood000 merged commit a659d27 into main Mar 2, 2026
13 checks passed
@jwood000 jwood000 deleted the repcapped-compositions branch March 2, 2026 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant