Coverage for src/hypra/hpl/regularization.py: 89.06%

80 statements  

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

1"""Explicit shuffle-regularized real endpoint polynomials, not finite limits. 

2 

3T=-log(1-z), z->1 from below. Polynomial constant terms define the shuffle 

4regularization. No stuffle regularization, path choice, or implicit replacement 

5of divergent endpoint values is performed. 

6""" 

7 

8from functools import lru_cache 

9 

10import sympy as s 

11 

12from .._typing import IntegerWord 

13from ..errors import UnsupportedExpressionError 

14from .algebra import shuffle_words 

15from .alternating import hpl_alternating_at_one 

16from .numeric import _word 

17 

18 

19@lru_cache(maxsize=2048) 

20def _polynomial(word, T): 

21 if not word: 

22 return s.S.One 

23 if word[0] != 1: 

24 return hpl_alternating_at_one(word) 

25 if all(letter == 1 for letter in word): 

26 return T ** len(word) / s.factorial(len(word)) 

27 terms = shuffle_words((1,), word[1:]) 

28 multiplicity = terms.pop(word) 

29 return s.expand( 

30 ( 

31 T * _polynomial(word[1:], T) 

32 - sum(count * _polynomial(other, T) for other, count in terms.items()) 

33 ) 

34 / multiplicity 

35 ) 

36 

37 

38def hpl_endpoint_polynomial(word: IntegerWord, T: s.Symbol) -> s.Expr: 

39 """Return the polynomial asymptote in T=-log(1-z), z->1-, for one word. 

40 

41 The omitted remainder tends to zero even after multiplication by a fixed 

42 power of T. Expanded letters are 0,+1,-1; weight limit is 12. For divergent 

43 words this is a regularization object, NOT their finite endpoint value. 

44 T must be an ordinary commutative Symbol. The exact coefficients use 

45 convergent alternating endpoints and no copied special-value tables. 

46 

47 Args: 

48 word: Expanded ternary word of weight at most twelve. 

49 T: Commutative symbol representing -log(1-z). 

50 """ 

51 if type(T) is not s.Symbol or T.is_commutative is not True: 

52 raise TypeError("T must be an ordinary commutative SymPy Symbol") 

53 word = _word(word) 

54 if len(word) > 12: 

55 raise UnsupportedExpressionError( 

56 "Shuffle regularization exceeds the weight-12 resource budget" 

57 ) 

58 return _polynomial(word, T) 

59 

60 

61def shuffle_regularized_hpl_at_one(word: IntegerWord) -> s.Expr: 

62 """Explicit constant term of the real endpoint polynomial; not a limit. 

63 

64 In particular H_1 has regularized constant zero although it diverges. 

65 Existing convergent endpoint APIs retain their divergence errors. This 

66 helper is not used implicitly by the hypergeometric expansion engine. 

67 

68 Args: 

69 word: Expanded ternary word of weight at most twelve. 

70 """ 

71 T = s.Symbol("_regularization_T") 

72 return hpl_endpoint_polynomial(word, T).subs(T, 0) 

73 

74 

75def hpl_expression_endpoint_polynomial( 

76 expression: s.Expr, 

77 variable: s.Symbol, 

78 T: s.Symbol, 

79) -> s.Expr: 

80 """Map an exact polynomial in same-argument HPLs to its endpoint polynomial. 

81 

82 ``variable -> 1-`` and ``T=-log(1-variable)``. Coefficients must be 

83 independent of both variables and are treated as fixed finite constants 

84 at the chosen specialization. No generic limit, integration interchange, 

85 argument transformation, or inverse-HPL asymptotics are performed. 

86 

87 The input must be a SymPy Expr (not source text), polynomial in HPL atoms 

88 all at exactly ``variable``. Rational argument-dependent coefficients, 

89 even apparently removable ones, and unknown variable-dependent functions 

90 are outside this deliberately narrow ring. Products/sums are retained 

91 until polynomial substitution so divergent logarithms can cancel. 

92 

93 Args: 

94 expression: Exact polynomial in same-argument HPLs. 

95 variable: HPL argument approaching one from below. 

96 T: Distinct commutative symbol representing the endpoint logarithm. 

97 """ 

98 from ..symbolic import HPL 

99 

100 if ( 

101 type(variable) is not s.Symbol 

102 or variable.is_commutative is not True 

103 or type(T) is not s.Symbol 

104 or T.is_commutative is not True 

105 or variable == T 

106 ): 

107 raise TypeError("Use distinct ordinary commutative Symbols for variable and T") 

108 if not isinstance(expression, s.Expr): 

109 raise TypeError("expression must be an exact SymPy Expr, not source text") 

110 if expression.has(T, s.Float, s.nan, s.zoo, s.oo, -s.oo): 

111 raise UnsupportedExpressionError("Input must be exact, finite, and independent of T") 

112 if s.count_ops(expression) > 4096: 112 ↛ 113line 112 didn't jump to line 113 because the condition on line 112 was never true

113 raise UnsupportedExpressionError("Endpoint expression exceeds the operation budget") 

114 atoms = sorted(expression.atoms(HPL), key=s.default_sort_key) 

115 if any(atom.argument != variable for atom in atoms): 

116 raise UnsupportedExpressionError( 

117 "All HPLs must have exactly the endpoint variable as argument" 

118 ) 

119 if any(len(atom.word) > 12 for atom in atoms): 119 ↛ 120line 119 didn't jump to line 120 because the condition on line 119 was never true

120 raise UnsupportedExpressionError( 

121 "Endpoint expression exceeds the weight-12 resource budget" 

122 ) 

123 generators = [s.Dummy("endpoint_hpl") for _ in atoms] 

124 replaced = expression.xreplace(dict(zip(atoms, generators, strict=True))) 

125 if replaced.is_commutative is not True: 125 ↛ 126line 125 didn't jump to line 126 because the condition on line 125 was never true

126 raise UnsupportedExpressionError( 

127 "Endpoint polynomial requires commutative scalar coefficients" 

128 ) 

129 if replaced.has(variable): 

130 raise UnsupportedExpressionError( 

131 "Coefficients must be independent of the endpoint variable" 

132 ) 

133 if not atoms: 

134 return expression 

135 

136 # Bound expanded size before Poly expands products; this conservative bound 

137 # may reject a huge expression whose final terms happen to cancel. 

138 def term_budget(node): 

139 if not node.has(*generators): 

140 return 1 

141 if node in generators: 

142 return 1 

143 if node.is_Add: 

144 count = sum(term_budget(arg) for arg in node.args) 

145 elif node.is_Mul: 

146 count = 1 

147 for arg in node.args: 

148 count *= term_budget(arg) 

149 if count > 4096: 149 ↛ 150line 149 didn't jump to line 150 because the condition on line 149 was never true

150 break 

151 elif node.is_Pow and node.exp.is_Integer and 0 <= node.exp <= 12: 

152 count = term_budget(node.base) ** int(node.exp) 

153 else: 

154 raise UnsupportedExpressionError("Input must be polynomial in HPLs, with no inverses") 

155 if count > 4096: 155 ↛ 156line 155 didn't jump to line 156 because the condition on line 155 was never true

156 raise UnsupportedExpressionError("Endpoint expression exceeds the expanded-term budget") 

157 return count 

158 

159 term_budget(replaced) 

160 try: 

161 polynomial = s.Poly(replaced, *generators) 

162 except s.PolynomialError as exc: 

163 raise UnsupportedExpressionError("Input must be polynomial in HPLs") from exc 

164 for monomial, _ in polynomial.terms(): 

165 if sum(degree * len(atom.word) for degree, atom in zip(monomial, atoms, strict=True)) > 12: 165 ↛ 166line 165 didn't jump to line 166 because the condition on line 165 was never true

166 raise UnsupportedExpressionError("Endpoint expression exceeds total HPL weight twelve") 

167 mapping = { 

168 generator: hpl_endpoint_polynomial(atom.word, T) 

169 for generator, atom in zip(generators, atoms, strict=True) 

170 } 

171 return s.expand(polynomial.as_expr().xreplace(mapping))