Coverage for src/hypra/hpl/mzv_reduction.py: 96.92%
86 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-26 22:12 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-26 22:12 +0000
1"""Small-weight MZV reductions generated from exact double-shuffle relations.
3No numerical fitting or upstream table is used. Admissible shuffle and stuffle
4products have the same convergent product on the right. For the otherwise
5divergent factor zeta(1), subtract the two products before taking the limit:
6their common divergent term cancels. Duality and the beta-integral height-one
7identity complete the system through weight eight.
8"""
10from collections import defaultdict
11from collections.abc import Mapping
12from fractions import Fraction
13from functools import lru_cache
14from types import MappingProxyType
16import sympy as s
18from ..symbolic import MZV
19from .algebra import shuffle_words
20from .stuffle import _stuffle
23@lru_cache(maxsize=9)
24def _compositions(weight: int) -> tuple[tuple[int, ...], ...]:
25 if not weight:
26 return ((),)
27 return tuple(
28 (first,) + tail for first in range(1, weight + 1) for tail in _compositions(weight - first)
29 )
32def _admissible(weight):
33 return tuple(indices for indices in _compositions(weight) if indices[0] >= 2)
36def _word(indices):
37 return tuple(letter for index in indices for letter in (0,) * (index - 1) + (1,))
40def _indices(word):
41 result, zeros = [], 0
42 for letter in word:
43 if letter == 0:
44 zeros += 1
45 else:
46 result.append(zeros + 1)
47 zeros = 0
48 return tuple(result)
51def _shuffle(left, right):
52 return {
53 _indices(word): count for word, count in shuffle_words(_word(left), _word(right)).items()
54 }
57def _quasi_shuffle(left, right):
58 return {
59 tuple(index for index, _ in word): count
60 for word, count in _stuffle(
61 tuple((index, 1) for index in left), tuple((index, 1) for index in right)
62 )
63 }
66def _pivot_key(indices):
67 # Keep zeta(5,3), the one new weight-eight primitive, when it is free.
68 # This is a chosen spanning set, not an algebraic-independence assertion.
69 return indices == (5, 3), -len(indices), indices
72@lru_cache(maxsize=7)
73def mzv_weight_reductions(weight: int) -> Mapping[tuple[int, ...], s.Expr]:
74 """Generate exact reductions at a requested weight between two and eight."""
75 if type(weight) is not int or not 2 <= weight <= 8:
76 raise ValueError("Double-shuffle reduction supports weights two through eight")
77 # Imported here because the public reducer invokes this bounded solver.
78 from .endpoints import _height_one, dual_indices
80 def lower(indices):
81 return mzv_weight_reductions(sum(indices))[indices]
83 rows = []
84 for indices in _admissible(weight):
85 if len(indices) == 1:
86 rows.append(({indices: 1}, s.zeta(weight)))
87 elif all(index == 1 for index in indices[1:]):
88 rows.append(({indices: 1}, _height_one(indices[0], len(indices))))
89 dual = dual_indices(indices)
90 if dual != indices:
91 rows.append(({indices: 1, dual: -1}, s.S.Zero))
92 for split in range(2, weight - 1):
93 for left in _admissible(split):
94 for right in _admissible(weight - split):
95 rhs = lower(left) * lower(right)
96 rows.extend(((_shuffle(left, right), rhs), (_quasi_shuffle(left, right), rhs)))
97 if weight > 2:
98 for indices in _admissible(weight - 1):
99 difference = defaultdict(int, _shuffle((1,), indices))
100 for word, count in _quasi_shuffle((1,), indices).items():
101 difference[word] -= count
102 rows.append((dict(difference), s.S.Zero))
104 pivots = {}
105 for coefficients, rhs in rows:
106 row = {indices: Fraction(count) for indices, count in coefficients.items() if count}
107 if any(indices[0] < 2 for indices in row): 107 ↛ 108line 107 didn't jump to line 108 because the condition on line 107 was never true
108 raise ArithmeticError("Divergent double-shuffle terms did not cancel")
109 while row:
110 pivot = min(row, key=_pivot_key)
111 scale = row.pop(pivot)
112 if pivot not in pivots:
113 pivots[pivot] = (
114 {indices: count / scale for indices, count in row.items()},
115 s.expand(rhs / s.Rational(scale)),
116 )
117 break
118 terms, replacement = pivots[pivot]
119 rhs -= s.Rational(scale) * replacement
120 for indices, count in terms.items():
121 remaining = row.get(indices, 0) - scale * count
122 if remaining:
123 row[indices] = remaining
124 else:
125 row.pop(indices, None)
126 else:
127 if s.expand(rhs) != 0: 127 ↛ 128line 127 didn't jump to line 128 because the condition on line 127 was never true
128 raise ArithmeticError("Double-shuffle relations produced an inconsistent row")
130 @lru_cache(maxsize=64)
131 def resolved(indices):
132 if indices not in pivots:
133 return MZV(indices)
134 terms, rhs = pivots[indices]
135 return s.expand(
136 rhs - s.Add(*(s.Rational(count) * resolved(other) for other, count in terms.items()))
137 )
139 return MappingProxyType({indices: resolved(indices) for indices in _admissible(weight)})