Coverage for src/hypra/hpl/at_i_reduction.py: 96.03%

105 statements  

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

1"""At-i constant bases generated from shuffle, Möbius and square identities. 

2 

3Only the retained primitive word names are chosen to agree with HPL 2.0. 

4Every coefficient is derived by rational elimination; no formula table or 

5numerical integer-relation fit is used. The primitive atoms are ordinary 

6HPLPM(word, I) objects and keep their independent numerical evaluation. 

7""" 

8 

9from collections.abc import Mapping 

10from fractions import Fraction 

11from functools import lru_cache 

12from itertools import product 

13from types import MappingProxyType 

14 

15import sympy as s 

16 

17from .algebra import shuffle_words 

18from .alternating import hpl_alternating_at_one 

19from .euler_reduction import _scalar_polynomial, _subtract 

20from .notation import HPLPM, pm_to_letters 

21from .regularization import hpl_endpoint_polynomial 

22 

23_ALPHABET = (-1, 0, 1) # minus, zero, plus 

24_ENDPOINT_LOG = s.Symbol("at_i_endpoint_log") 

25_MOBIUS_FORMS = { 

26 0: ((1, -1),), 

27 1: ((0, -1),), 

28 -1: ((0, -1), (1, 1), (-1, -1)), 

29} 

30 

31# Identifiers of the free HPLI constants in the pinned HPLatI4/5/6 sources. 

32# They specify coordinates only; the reduction coefficients are generated. 

33_PRIMITIVES = { 

34 4: ((0, -1, 1, -1),), 

35 5: ( 

36 (-1, 1, -1, -1, -1), 

37 (-1, 1, -1, -1, 1), 

38 (-1, 1, -1, 1, 1), 

39 (-1, 1, 1, -1, -1), 

40 (-1, 1, 1, 1, -1), 

41 (0, -1, 1, -1, -1), 

42 (0, 1, -1, -1, -1), 

43 (0, 1, 1, -1, -1), 

44 (0, 1, 1, -1, 1), 

45 ), 

46 6: ( 

47 (-1, 1, -1, -1, -1, -1), 

48 (-1, 1, -1, -1, -1, 1), 

49 (-1, 1, -1, -1, 1, 1), 

50 (-1, 1, -1, 1, 1, 1), 

51 (-1, 1, 1, -1, -1, -1), 

52 (-1, 1, 1, -1, -1, 1), 

53 (-1, 1, 1, 1, -1, -1), 

54 (-1, 1, 1, 1, -1, 1), 

55 (0, -1, -1, 1, -1, -1), 

56 (0, -1, 1, -1, -1, -1), 

57 (0, -1, 1, 1, 1, -1), 

58 (0, 1, -1, -1, -1, -1), 

59 (0, 1, -1, -1, 0, -1), 

60 (0, 1, -1, 0, -1, -1), 

61 (0, 1, -1, 1, 0, -1), 

62 (0, 1, 1, -1, -1, -1), 

63 (0, 1, 1, -1, -1, 1), 

64 (0, 1, 1, -1, 0, -1), 

65 (0, 1, 1, -1, 0, 1), 

66 (0, 1, 1, -1, 1, -1), 

67 (0, 1, 1, 0, -1, -1), 

68 (0, 1, 1, 1, -1, -1), 

69 (0, 1, 1, 1, 0, -1), 

70 ), 

71} 

72 

73 

74@lru_cache(maxsize=1093) 

75def _endpoint_constant(word): 

76 # M(z)=(1-z)/(1+z) has -log(1-M(z))=-log(z)-log(2)+o(1). 

77 # Matching the constant term at the tangential origin fixes the pullback. 

78 letters = pm_to_letters(HPLPM.from_indices(word, s.I).word) 

79 return s.expand( 

80 s.Add( 

81 *( 

82 coefficient 

83 * hpl_endpoint_polynomial(ordinary, _ENDPOINT_LOG).subs(_ENDPOINT_LOG, -s.log(2)) 

84 for ordinary, coefficient in letters.items() 

85 ) 

86 ) 

87 ) 

88 

89 

90@lru_cache(maxsize=1093) 

91def _mobius_terms(word): 

92 if not word: 

93 return (((), s.S.One),) 

94 result = {} 

95 for tail, coefficient in _mobius_terms(word[1:]): 

96 for letter, factor in _MOBIUS_FORMS[word[0]]: 

97 key = (letter,) + tail 

98 result[key] = result.get(key, s.S.Zero) + factor * coefficient 

99 result[()] = _endpoint_constant(word) 

100 return tuple((key, s.expand(value)) for key, value in result.items() if value != 0) 

101 

102 

103def _trailing_zeros(word): 

104 count = 0 

105 for letter in reversed(word): 

106 if letter: 

107 break 

108 count += 1 

109 return count 

110 

111 

112@lru_cache(maxsize=127) 

113def _square_value(word): 

114 # Under u=z², omega_0(u)=2*omega_0(z), omega_1(u)=omega_minus(z). 

115 # At z=i, u=-1 on its upper boundary. Trailing zeros acquire log(-1)=i*pi. 

116 count = _trailing_zeros(word) 

117 return s.expand( 

118 s.Rational(1, 2) ** word.count(0) 

119 * (-1) ** word.count(-1) 

120 * s.Add( 

121 *( 

122 (s.I * s.pi) ** k / s.factorial(k) * hpl_alternating_at_one(word[: len(word) - k]) 

123 for k in range(count + 1) 

124 ) 

125 ) 

126 ) 

127 

128 

129def _fractional_row(weight): 

130 # Li_n(z/(z-1)) has kernel pullbacks omega_0 -> omega_0+omega_1, 

131 # omega_1 -> -omega_1. Ordinary omega_1=(omega_plus+omega_minus)/2. 

132 # Continuing z from (0,1) to i stays off the original function's cut. 

133 result = {} 

134 for prefix in product(_ALPHABET, repeat=weight - 1): 

135 coefficient = -(s.Rational(1, 2) ** (1 + sum(letter != 0 for letter in prefix))) 

136 for final in (-1, 1): 

137 result[prefix + (final,)] = coefficient 

138 return result 

139 

140 

141@lru_cache(maxsize=7) 

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

143 """Canonical PM at-i values through weight six, with immutable cached results.""" 

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

145 raise ValueError("The at-i basis supports weights zero through six") 

146 if weight == 0: 

147 return MappingProxyType({(): s.S.One}) 

148 

149 def lower(word): 

150 return at_i_weight_reductions(len(word))[word] 

151 

152 words = tuple(product(_ALPHABET, repeat=weight)) 

153 primitives = frozenset(_PRIMITIVES.get(weight, ())) 

154 pivots = {} 

155 

156 def add(coefficients, rhs): 

157 row = { 

158 word: Fraction(coefficient) for word, coefficient in coefficients.items() if coefficient 

159 } 

160 scalar = _scalar_polynomial(rhs) 

161 while row: 

162 pivot = min( 

163 row, 

164 key=lambda word: (word in primitives, word[0] != 1 and word[-1] != 0, word), 

165 ) 

166 scale = row.pop(pivot) 

167 if pivot not in pivots: 

168 pivots[pivot] = ( 

169 {word: coefficient / scale for word, coefficient in row.items()}, 

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

171 ) 

172 break 

173 terms, replacement = pivots[pivot] 

174 _subtract(scalar, scale, replacement) 

175 _subtract(row, scale, terms) 

176 else: 

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

178 raise ArithmeticError("At-i constant identities produced an inconsistent row") 

179 

180 for split in range(1, weight // 2 + 1): 

181 for left in product(_ALPHABET, repeat=split): 

182 for right in product(_ALPHABET, repeat=weight - split): 

183 add(shuffle_words(left, right), lower(left) * lower(right)) 

184 

185 for word in words: 

186 parity = (-1) ** word.count(1) 

187 row, rhs = {word: parity}, s.S.Zero 

188 for target, coefficient in _mobius_terms(word): 

189 if len(target) == weight: 

190 row[target] = row.get(target, 0) - coefficient 

191 else: 

192 rhs += coefficient * lower(target) 

193 # M(i)=-i is reached through the lower quadrant. Negation sends each 

194 # plus letter to its negative; the tangential log shifts by -i*pi. 

195 rhs -= parity * s.Add( 

196 *( 

197 (-s.I * s.pi) ** k / s.factorial(k) * lower(word[:-k]) 

198 for k in range(1, _trailing_zeros(word) + 1) 

199 ) 

200 ) 

201 add(row, rhs) 

202 

203 for word in product((-1, 0), repeat=weight): 

204 add({word: 1}, _square_value(word)) 

205 from .special import _quarter_period_polylog 

206 

207 classical = ( 

208 s.I * s.pi / 2 

209 if weight == 1 

210 else _quarter_period_polylog(weight, 1) - _quarter_period_polylog(weight, -1) 

211 ) 

212 add({(0,) * (weight - 1) + (1,): 1}, classical) 

213 if weight in (3, 4): 

214 add(_fractional_row(weight), s.polylog(weight, (1 - s.I) / 2)) 

215 if set(words).difference(pivots) != primitives: 215 ↛ 216line 215 didn't jump to line 216 because the condition on line 215 was never true

216 raise ArithmeticError("At-i identities did not span the chosen primitive basis") 

217 

218 @lru_cache(maxsize=729) 

219 def resolved(word): 

220 if word in primitives: 

221 return {HPLPM.from_indices(word, s.I): Fraction(1)} 

222 terms, rhs = pivots[word] 

223 result = rhs.copy() 

224 for other, coefficient in terms.items(): 

225 _subtract(result, coefficient, resolved(other)) 

226 return result 

227 

228 return MappingProxyType( 

229 { 

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

231 for word in words 

232 } 

233 )