Coverage for src/hypra/hpl/euler_reduction.py: 95.14%

159 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-26 22:12 +0000

1"""Euler-sum reductions from colored double shuffle and distribution. 

2 

3The four sign choices project onto even summation indices, giving 

4sum_{colors} ES(a,b;colors) = 2**(2-a-b) zeta(a,b). Combined with shuffle, 

5stuffle and their convergent single-zeta(1) difference, this determines every 

6admissible depth-two colored sum at weights three, five and seven. At all 

7depths through weight six, the pullback u=t/(1+t) supplies Li_n(1/2), closing 

8the system in zeta, log(2), and Li_n(1/2), with one additional HPLs6 primitive. 

9""" 

10 

11from collections import defaultdict 

12from collections.abc import Mapping 

13from fractions import Fraction 

14from functools import lru_cache 

15from itertools import product 

16from types import MappingProxyType 

17 

18import sympy as s 

19 

20from ..symbolic import MZV, EulerSum 

21from .algebra import shuffle_words 

22from .endpoints import reduce_mzv 

23from .stuffle import _stuffle 

24 

25 

26def _hpl_word(indices): 

27 word, previous, sign = [], 1, 1 

28 for index, color in indices: 

29 letter = previous * color 

30 word.extend([0] * (index - 1) + [letter]) 

31 previous = letter 

32 sign *= letter 

33 return tuple(word), sign 

34 

35 

36def _euler_word(word): 

37 indices, zeros, previous, sign = [], 0, 1, 1 

38 for letter in word: 

39 if letter == 0: 

40 zeros += 1 

41 else: 

42 indices.append((zeros + 1, previous * letter)) 

43 zeros, previous = 0, letter 

44 sign *= letter 

45 return tuple(indices), sign 

46 

47 

48def _colored_shuffle(left, right): 

49 a, sign_a = _hpl_word(left) 

50 b, sign_b = _hpl_word(right) 

51 result = defaultdict(int) 

52 for word, count in shuffle_words(a, b).items(): 

53 indices, sign = _euler_word(word) 

54 result[indices] += count * sign * sign_a * sign_b 

55 return dict(result) 

56 

57 

58def _depth_one(word): 

59 index, color = word[0] 

60 if color == 1: 

61 return s.zeta(index) 

62 return -s.log(2) if index == 1 else -(1 - s.Rational(1, 2) ** (index - 1)) * s.zeta(index) 

63 

64 

65@lru_cache(maxsize=3) 

66def euler_depth_two_reductions(weight: int) -> Mapping[tuple[tuple[int, int], ...], s.Expr]: 

67 """Return derived reductions for all convergent depth-two sums at weight 3,5,7.""" 

68 if weight not in (3, 5, 7): 68 ↛ 69line 68 didn't jump to line 69 because the condition on line 68 was never true

69 raise ValueError("Depth-two Euler reduction supports odd weights three through seven") 

70 words = tuple( 

71 ((split, first), (weight - split, second)) 

72 for split in range(1, weight) 

73 for first, second in product((-1, 1), repeat=2) 

74 if (split, first) != (1, 1) 

75 ) 

76 rows = [ 

77 ({word: 1}, reduce_mzv(MZV(tuple(index for index, _ in word)))) 

78 for word in words 

79 if all(color == 1 for _, color in word) 

80 ] 

81 for split in range(1, weight): 

82 for first, second in product((-1, 1), repeat=2): 

83 left, right = ((split, first),), ((weight - split, second),) 

84 if left == ((1, 1),) or right == ((1, 1),): 

85 continue 

86 value = _depth_one(left) * _depth_one(right) 

87 rows.extend( 

88 ((_colored_shuffle(left, right), value), (dict(_stuffle(left, right)), value)) 

89 ) 

90 for color in (-1, 1): 

91 right = ((weight - 1, color),) 

92 difference = defaultdict(int, _colored_shuffle(((1, 1),), right)) 

93 for word, count in _stuffle(((1, 1),), right): 

94 difference[word] -= count 

95 rows.append((dict(difference), s.S.Zero)) 

96 for split in range(2, weight): 

97 rows.append( 

98 ( 

99 { 

100 ((split, first), (weight - split, second)): 1 

101 for first, second in product((-1, 1), repeat=2) 

102 }, 

103 s.Rational(2) ** (2 - weight) * reduce_mzv(MZV((split, weight - split))), 

104 ) 

105 ) 

106 return MappingProxyType(_solve(rows, words, depth_one_known=True)) 

107 

108 

109def _scalar_polynomial(expression): 

110 result = {} 

111 for term in s.Add.make_args(s.expand(expression)): 

112 coefficient, monomial = term.as_coeff_Mul(rational=True) 

113 if coefficient: 

114 if not isinstance(coefficient, s.Rational): 114 ↛ 115line 114 didn't jump to line 115 because the condition on line 114 was never true

115 raise ArithmeticError("Expected rational coefficients in the constant basis") 

116 result[monomial] = Fraction(int(coefficient.p), int(coefficient.q)) 

117 return result 

118 

119 

120def _subtract(target, scale, source): 

121 for term, coefficient in source.items(): 

122 remaining = target.get(term, 0) - scale * coefficient 

123 if remaining: 

124 target[term] = remaining 

125 else: 

126 target.pop(term, None) 

127 

128 

129def _solve(rows, words, *, depth_one_known=False): 

130 pivots = {} 

131 for coefficients, rhs in rows: 

132 row = {word: Fraction(count) for word, count in coefficients.items() if count} 

133 for word in list(row): 

134 if depth_one_known and len(word) == 1: 

135 rhs -= s.Rational(row.pop(word)) * _depth_one(word) 

136 scalar = _scalar_polynomial(rhs) 

137 if any(word[0] == (1, 1) for word in row): 137 ↛ 138line 137 didn't jump to line 138 because the condition on line 137 was never true

138 raise ArithmeticError("Divergent colored double-shuffle terms did not cancel") 

139 while row: 

140 pivot = min(row, key=lambda word: (-len(word), word)) 

141 scale = row.pop(pivot) 

142 if pivot not in pivots: 

143 pivots[pivot] = ( 

144 {word: count / scale for word, count in row.items()}, 

145 {term: coefficient / scale for term, coefficient in scalar.items()}, 

146 ) 

147 break 

148 terms, replacement = pivots[pivot] 

149 _subtract(scalar, scale, replacement) 

150 for word, count in terms.items(): 

151 remaining = row.get(word, 0) - scale * count 

152 if remaining: 

153 row[word] = remaining 

154 else: 

155 row.pop(word, None) 

156 else: 

157 if scalar: 157 ↛ 158line 157 didn't jump to line 158 because the condition on line 157 was never true

158 raise ArithmeticError( 

159 "Colored double-shuffle relations produced an inconsistent row" 

160 ) 

161 if len(pivots) != len(words): 161 ↛ 162line 161 didn't jump to line 162 because the condition on line 161 was never true

162 raise ArithmeticError("Colored double-shuffle system did not span the requested weight") 

163 

164 @lru_cache(maxsize=324) 

165 def resolved(word): 

166 terms, rhs = pivots[word] 

167 result = rhs.copy() 

168 for other, count in terms.items(): 

169 _subtract(result, count, resolved(other)) 

170 return result 

171 

172 return { 

173 word: s.Add(*(s.Rational(count) * term for term, count in resolved(word).items())) 

174 for word in words 

175 } 

176 

177 

178@lru_cache(maxsize=7) 

179def _colored_compositions(weight: int) -> tuple[tuple[tuple[int, int], ...], ...]: 

180 if not weight: 

181 return ((),) 

182 return tuple( 

183 ((first, color),) + tail 

184 for first in range(1, weight + 1) 

185 for color in (-1, 1) 

186 for tail in _colored_compositions(weight - first) 

187 ) 

188 

189 

190def _admissible(weight): 

191 return tuple(word for word in _colored_compositions(weight) if word[0] != (1, 1)) 

192 

193 

194@lru_cache(maxsize=6) 

195def euler_weight_reductions(weight: int) -> Mapping[tuple[tuple[int, int], ...], s.Expr]: 

196 """Generate every convergent colored Euler reduction through weight six. 

197 

198 At weight six, ES((5,1),(-1,-1))=HPLs6-zeta(6) is retained as the 

199 additional primitive. This chooses a spanning basis without asserting 

200 algebraic independence of its constants. 

201 """ 

202 if type(weight) is not int or not 1 <= weight <= 6: 202 ↛ 203line 202 didn't jump to line 203 because the condition on line 202 was never true

203 raise ValueError("General Euler reduction supports weights one through six") 

204 if weight == 1: 

205 return MappingProxyType({((1, -1),): -s.log(2)}) 

206 words = _admissible(weight) 

207 rows = [] 

208 for word in words: 

209 if len(word) == 1: 

210 rows.append(({word: 1}, _depth_one(word))) 

211 elif all(color == 1 for _, color in word): 

212 rows.append(({word: 1}, reduce_mzv(MZV(tuple(index for index, _ in word))))) 

213 elif len(word) == 2 and weight % 2: 

214 rows.append(({word: 1}, euler_depth_two_reductions(weight)[word])) 

215 for split in range(1, weight): 

216 for left in _admissible(split): 

217 for right in _admissible(weight - split): 

218 rhs = ( 

219 euler_weight_reductions(split)[left] 

220 * euler_weight_reductions(weight - split)[right] 

221 ) 

222 rows.extend( 

223 ((_colored_shuffle(left, right), rhs), (dict(_stuffle(left, right)), rhs)) 

224 ) 

225 for word in _admissible(weight - 1): 

226 difference = defaultdict(int, _colored_shuffle(((1, 1),), word)) 

227 for indices, count in _stuffle(((1, 1),), word): 

228 difference[indices] -= count 

229 rows.append((dict(difference), s.S.Zero)) 

230 for indices in sorted({tuple(index for index, _ in word) for word in words if word[0][0] >= 2}): 

231 rows.append( 

232 ( 

233 { 

234 tuple(zip(indices, colors, strict=True)): 1 

235 for colors in product((-1, 1), repeat=len(indices)) 

236 }, 

237 s.Rational(2) ** (len(indices) - weight) * reduce_mzv(MZV(indices)), 

238 ) 

239 ) 

240 

241 # Under u=t/(1+t), omega_0(u)=omega_0(t)-omega_-1(t) and 

242 # omega_1(u)=omega_-1(t). Every term ends in -1, so all endpoint sums 

243 # converge. This supplies the half-argument periods at weights four to six. 

244 half_value = defaultdict(int) 

245 for prefix in product((0, -1), repeat=weight - 1): 

246 indices, sign = _euler_word(prefix + (-1,)) 

247 half_value[indices] += (-1) ** prefix.count(-1) * sign 

248 polylog = s.polylog(weight, s.S.Half) 

249 if weight == 3: 

250 polylog = 7 * s.zeta(3) / 8 - s.pi**2 * s.log(2) / 12 + s.log(2) ** 3 / 6 

251 rows.append((dict(half_value), polylog)) 

252 if weight == 6: 

253 # Choose the one additional period in the shipped weight-six basis. 

254 # Keeping its exact strict sum avoids introducing a new opaque symbol. 

255 primitive = ((5, -1), (1, -1)) 

256 rows.append(({primitive: 1}, EulerSum((5, 1), (-1, -1)))) 

257 return MappingProxyType(_solve(rows, words))