#!/usr/bin/env python3 """Exact capped quadratic forms for a single symmetric polynomial witness. F(t) = sum_i c_i (U - s(t))^{a_i} m_{lambda_i}(t), I_cap(F) = int_T F^2, I_0(F) = int_{s tuple: if p == 0: return rem out = [] for (q, c) in rem: if q == p: if c > 1: out.append((q, c - 1)) else: out.append((q, c)) return tuple(sorted(out)) @lru_cache(maxsize=None) def _mul_m_rec(remL: tuple, remM: tuple, maxexp: int) -> dict: if not remL and not remM: return {(): 1} out: dict = {} popts = sorted({p for p, _ in remL} | {0}) qopts = sorted({q for q, _ in remM} | {0}) for p in popts: for q in qopts: if p == 0 and q == 0: continue e = p + q if e > maxexp: continue sub = _mul_m_rec(_dec(remL, p), _dec(remM, q), e) for tail, cnt in sub.items(): key = (e,) + tail out[key] = out.get(key, 0) + cnt return out def mul_m(lam, mu) -> dict: """m_lam * m_mu = sum_nu coeff[nu] m_nu (dimension independent).""" return dict(_mul_m_rec(tuple(sorted(mult(lam).items())), tuple(sorted(mult(mu).items())), 10 ** 9)) def capped_F2(k: int, terms, U: Fr, delta: Fr, Bfun, capped: bool): """Exact sum_{i,j} c_i c_j int (U-s)^{a_i+a_j} m_{li} m_{lj}. `terms` is a list of (coeff Fraction, a int, lambda partition). Returns Fraction. """ # group terms by (a, lam) so identical basis vectors are merged from collections import defaultdict agg = defaultdict(Fr) for (c, a, lam) in terms: agg[(a, tuple(lam))] += c keys = list(agg.items()) # precompute product expansions per (lam, mu) exp_cache: dict = {} def expansion(li, lj): key = (li, lj) if li <= lj else (lj, li) if key not in exp_cache: exp_cache[key] = mul_m(key[0], key[1]) return exp_cache[key] integ_cache: dict = {} def integ(e, nu): key = (e, nu) if key not in integ_cache: if capped: integ_cache[key] = capped_integral(k, e, nu, U, delta, Bfun) else: integ_cache[key] = uncapped_integral(k, e, nu, U) return integ_cache[key] total = Fr(0) for ii in range(len(keys)): (ai, li), ci = keys[ii] for jj in range(ii, len(keys)): (aj, lj), cj = keys[jj] w = ci * cj * (1 if ii == jj else 2) if w == 0: continue e = ai + aj for nu, cc in expansion(li, lj).items(): total += w * cc * integ(e, nu) return Fr(total) if __name__ == "__main__": # p_1^2 = m_2 + 2 m_{1,1} print(mul_m((1,), (1,))) # m_2 m_1 = m_3 + m_{2,1} print(mul_m((2,), (1,))) # m_2^2 = m_4 + m_{3,1} + ... known: m_2^2 = m_4 + m_{3,1} + m_{2,2}? check print(mul_m((2,), (2,)))