A package that exposes some matroid operation and creations
Project description
chromatic-matroids
A self-contained Python library for computing chromatic invariants of matroids. No CAS required; the only runtime dependency is NumPy.
pip install chromatic-matroids
Table of contents
- Quick start
- Matroids
- Compositions and set compositions
- Quasi-symmetric functions
- Word quasi-symmetric functions (WQSym / NCQSym)
- Chromatic invariants
- Lattice statistics
- Running the tests
Quick start
from chromatic_matroids import (
uniform_matroid,
chromatic_quasisymmetric_function,
chromatic_non_commutative_quasisymmetric_function,
compute_chromatic_polynomial,
z_rank, z_index,
)
m = uniform_matroid(3, 2) # U(3,2)
print(compute_chromatic_polynomial(m)) # [2, -3, 1] → (q-1)(q-2)
print(chromatic_quasisymmetric_function(m).coefficients)
# {'(1,1,1)': 6, '(1,2)': 3}
print(chromatic_non_commutative_quasisymmetric_function(m).coefficients)
# {'(1|2|3)': 1, '(1|3|2)': 1, ...} (9 terms, all coefficient 1)
Matroids
Constructors
from chromatic_matroids import (
Matroid,
uniform_matroid, schubert_matroid, nested_matroid, graphic_matroid,
)
# Explicit bases
m = Matroid(frozenset([1, 2, 3]), {frozenset([1, 2]), frozenset([1, 3]), frozenset([2, 3])})
# U(n, r): all r-subsets of {1, …, n} are bases
u = uniform_matroid(4, 2)
# Schubert matroid sh({1,…,n}, A): B = {b₁<…<bᵣ} is a basis iff bᵢ ≤ aᵢ for all i
s = schubert_matroid(4, frozenset([2, 3]))
# Nested matroid: chain of flats X₁ ⊂ X₂ ⊂ … with rank bounds
ne = nested_matroid(4, 2,
(frozenset({1, 2}), frozenset({1, 2, 3, 4})),
(1, 2))
# Graphic matroid: ground set = edges, bases = spanning forests
K3 = graphic_matroid([(1, 2), (1, 3), (2, 3)], {1, 2, 3})
The Matroid constructor validates the basis exchange axiom and raises if the input is invalid.
Methods
| Method | Returns | Description |
|---|---|---|
m.rank(S) |
int |
Rank of a subset S |
m.independent_sets() |
set[frozenset] |
All independent sets |
m.is_nested() |
bool |
True if m is a nested matroid |
m.extend(e) |
Matroid |
Add e as a coloop |
m.relabel(bij) |
Matroid |
Relabel ground set via bijection dict |
Generators
from chromatic_matroids import (
generate_schubert_matroids, # all 2^n Schubert matroids on {1,…,n}
generate_loopless_schubert_matroids, # loopless subset
generate_loopless_nested_matroids, # all n! loopless nested matroids on {1,…,n}
generate_nested_matroids_doublechains, # nested matroids from double chains
)
matroids = generate_loopless_nested_matroids(4) # 24 matroids
Compositions and set compositions
Composition
A composition of n is an ordered list of positive integers summing to n.
from chromatic_matroids import Composition
c = Composition([2, 1, 3]) # parts=[2,1,3], n=6
c = Composition("(2,1,3)") # from string repr
c.rest() # Composition([1, 3])
c.prepend(4) # Composition([4, 2, 1, 3])
Composition.generate_all_composition(4) # all 2^(n-1) compositions, cached
Composition.quasi_shuffles(Composition([1]), Composition([1]))
# {'(2)': 1, '(1,1)': 2}
SetComposition
An ordered partition of a finite set of integers.
from chromatic_matroids import SetComposition
sc = SetComposition([[1, 2], [3, 4], [5]]) # (1,2|3,4|5)
sc = SetComposition("(1,2|3,4|5)") # from string repr
sc.first() # frozenset({1, 2})
sc.rest() # SetComposition([[3, 4], [5]])
sc.alpha() # Composition([2, 2, 1]) — block sizes
sc.relabel({1: 10, 2: 20, 3: 30, 4: 40, 5: 50})
sc.relabel(None) # standardise to {1, 2, …, n}
SetComposition.generate_all_setcompositions(3) # 13 set compositions on {1,2,3}
SetComposition.quasi_shuffles(sc1, sc2) # dict[str, int]
Quasi-symmetric functions
QSymFunction represents an element of QSym in the monomial basis M_α.
from chromatic_matroids import QSymFunction, Composition
f = QSymFunction(monomial_basis=Composition([1, 2])) # M_{(1,2)}
g = QSymFunction(monomial_basis={"(1,2)": 2, "(3,)": -1})
zero = QSymFunction()
h = f + g # addition
h = f * g # quasi-shuffle product
h = f._scalarMultiple(3)
f.coefficients # dict[str, int]
Word quasi-symmetric functions
NCQSymFunction represents an element of WQSym in the monomial basis M_σ
indexed by set compositions.
from chromatic_matroids import NCQSymFunction, SetComposition
sc = SetComposition([[1, 2], [3]])
f = NCQSymFunction(monomial_basis=sc) # M_{(1,2|3)}
g = NCQSymFunction(monomial_basis="(1|2,3)")
h = NCQSymFunction(monomial_basis={"(1|2)": 2, "(2|1)": -1})
f + g # addition
f * g # quasi-shuffle product
f._scalarMultiple(3)
f.comu() # forgetful map to QSym: M_σ ↦ M_{α(σ)}
f.coefficients # dict[str, int]
Chromatic invariants
from chromatic_matroids import (
compute_chromatic_polynomial,
chromatic_quasisymmetric_function,
chromatic_non_commutative_quasisymmetric_function,
stable_matroids_setcompositions,
)
m = uniform_matroid(3, 2)
# Characteristic polynomial χ(M, q) = Σ_{A⊆E} (-1)^|A| q^{r(E)-r(A)}
# Returns [c₀, c₁, …, cᵣ] so that χ = c₀ + c₁q + … + cᵣqʳ
poly = compute_chromatic_polynomial(m) # [2, -3, 1] → (q-1)(q-2)
# Chromatic QSym (commutative)
q = chromatic_quasisymmetric_function(m)
print(q.coefficients) # {'(1,1,1)': 6, '(1,2)': 3}
# Chromatic WQSym (non-commutative lift)
nc = chromatic_non_commutative_quasisymmetric_function(m)
print(nc.coefficients) # 9 terms, each coefficient 1
# Stability check: σ is stable for M if the greedy scoring has a unique maximiser
sc = SetComposition([[2], [1]])
stable_matroids_setcompositions(uniform_matroid(2, 1), sc) # True
The chromatic WQSym is the sum of M_σ over all set compositions σ that are stable for M (i.e. the score function Σᵢ |B ∩ Sᵢ| · i has a unique maximising basis B). The QSym image is the forgetful map M_σ ↦ M_{α(σ)}.
Lattice statistics
These functions analyse the integer lattice structure of the WQSym coefficient matrix via the Smith Normal Form (SNF).
from chromatic_matroids import (
smith_normal_form_factors,
z_rank,
invariant_factors,
z_index,
generate_loopless_nested_matroids,
)
# SNF of an arbitrary integer matrix
smith_normal_form_factors([[2, 0], [0, 3]]) # [1, 6]
smith_normal_form_factors([[6, 4]]) # [2]
matroids = generate_loopless_nested_matroids(3)
z_rank(matroids) # 6 (= 3!)
invariant_factors(matroids) # [d₁, …, d₆] with d₁ | d₂ | … | d₆
z_index(matroids) # d₁ · … · d₆ = [sat(M) : M]
Z-index: given the ℤ-module M spanned by the WQSym coefficient vectors, its saturation is sat(M) = {x ∈ ℤᴺ : cx ∈ M for some c > 0}. The Z-index is [sat(M) : M]; it equals 1 if and only if M is saturated.
Running the tests
python3 -m venv venv && source venv/bin/activate
pip install -e "package[test]"
pytest package/tests/ -v
224 tests covering all modules, including parametrised rank theorems:
z_rank = d! and QSym rank = 2^(d−1) for loopless nested matroids of size d.
License
MIT — see LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file chromatic_matroids-1.0.2.tar.gz.
File metadata
- Download URL: chromatic_matroids-1.0.2.tar.gz
- Upload date:
- Size: 31.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d3756d80750e0286d54639e38552c0b88e97e8f34b24273ff209da43dcc17c42
|
|
| MD5 |
de3a3198d3f45b39d1129a6c897ce0a8
|
|
| BLAKE2b-256 |
35f9cdea17618b877a9a772bb0e7b7723386744234ddccc7a9b2d187ff458f1b
|
Provenance
The following attestation bundles were made for chromatic_matroids-1.0.2.tar.gz:
Publisher:
release.yml on raulpenaguiao/chromatic-matroids
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
chromatic_matroids-1.0.2.tar.gz -
Subject digest:
d3756d80750e0286d54639e38552c0b88e97e8f34b24273ff209da43dcc17c42 - Sigstore transparency entry: 1748157904
- Sigstore integration time:
-
Permalink:
raulpenaguiao/chromatic-matroids@7aa3647782a00d71023676cc9d2820dfb8706897 -
Branch / Tag:
refs/tags/release-1.0.2 - Owner: https://github.com/raulpenaguiao
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7aa3647782a00d71023676cc9d2820dfb8706897 -
Trigger Event:
push
-
Statement type:
File details
Details for the file chromatic_matroids-1.0.2-py3-none-any.whl.
File metadata
- Download URL: chromatic_matroids-1.0.2-py3-none-any.whl
- Upload date:
- Size: 28.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
171fb80148dda52bba437f725fc78655d4c0c34f100a90d8129a5e5bd5284e3f
|
|
| MD5 |
1f3c4a8bd73affd5cfa448e9b9d7af29
|
|
| BLAKE2b-256 |
626bf7d7622e7249324640cf44856bd77a0076c10aa27c3a1b7abc589cdada61
|
Provenance
The following attestation bundles were made for chromatic_matroids-1.0.2-py3-none-any.whl:
Publisher:
release.yml on raulpenaguiao/chromatic-matroids
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
chromatic_matroids-1.0.2-py3-none-any.whl -
Subject digest:
171fb80148dda52bba437f725fc78655d4c0c34f100a90d8129a5e5bd5284e3f - Sigstore transparency entry: 1748157975
- Sigstore integration time:
-
Permalink:
raulpenaguiao/chromatic-matroids@7aa3647782a00d71023676cc9d2820dfb8706897 -
Branch / Tag:
refs/tags/release-1.0.2 - Owner: https://github.com/raulpenaguiao
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7aa3647782a00d71023676cc9d2820dfb8706897 -
Trigger Event:
push
-
Statement type: