"""Reproduce the two exact theta-matroid examples of arXiv:2608.07342."""

from itertools import product
from math import comb, prod


def corank_counts(lengths, limit):
    """Count complements of flats by the nullity of the dual subset."""
    strands = len(lengths)
    counts = [0] * (limit + 1)
    sizes = [range(min(length, limit + strands - 1) + 1) for length in lengths]
    for multiplicities in product(*sizes):
        support = sum(size > 0 for size in multiplicities)
        corank = sum(multiplicities) - min(support, strands - 1)
        if corank > limit:
            continue
        if support < strands and 1 in multiplicities:
            continue
        counts[corank] += prod(comb(length, used)
                               for length, used in zip(lengths, multiplicities))
    return counts


def verify():
    log_concave = corank_counts((1, 25, 26, 26), 3)
    assert log_concave[1:] == [17850, 933425, 49120475]
    assert log_concave[2] ** 2 < log_concave[1] * log_concave[3]

    lengths = (1, 27, 28, 28)
    coranks = corank_counts(lengths, 4)
    assert coranks[1:] == [22275, 1264437, 75732840, 1648585224]
    rank = sum(lengths) - len(lengths) + 1
    field = 59
    lift = [coranks[0]] + [
        field ** (rank + 1 - i) * coranks[i - 1] + coranks[i]
        for i in range(1, len(coranks))
    ]
    assert lift[3] < lift[2] and lift[3] < lift[4]
    assert field * sum(lengths) + 1 == 4957
    print("Both published examples reproduced. Lifted ranks 78, 79, 80:")
    print(*(lift[i] for i in (4, 3, 2)), sep="\n")


if __name__ == "__main__":
    verify()

