Coverage for src/hypra/hpl/stuffle.py: 91.67%

72 statements  

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

1"""Exact finite-cutoff quasi-shuffle algebra for convergent endpoint constants.""" 

2 

3from functools import lru_cache 

4 

5import sympy as s 

6 

7from .._typing import as_integer_tuple 

8from ..errors import UnsupportedExpressionError 

9from ..symbolic import MZV, EulerSum 

10 

11_MAX_WEIGHT = 12 

12_MAX_TERMS = 8192 

13 

14 

15@lru_cache(maxsize=4096) 

16def _stuffle(left, right): 

17 if not left: 

18 return ((right, 1),) 

19 if not right: 

20 return ((left, 1),) 

21 terms = {} 

22 merged = (left[0][0] + right[0][0], left[0][1] * right[0][1]) 

23 for first, a, b in ( 

24 (left[0], left[1:], right), 

25 (right[0], left, right[1:]), 

26 (merged, left[1:], right[1:]), 

27 ): 

28 for tail, count in _stuffle(a, b): 

29 word = (first,) + tail 

30 terms[word] = terms.get(word, 0) + count 

31 if len(terms) > _MAX_TERMS: 31 ↛ 32line 31 didn't jump to line 32 because the condition on line 31 was never true

32 raise UnsupportedExpressionError("Constant stuffle exceeds the term budget") 

33 return tuple(terms.items()) 

34 

35 

36def stuffle_expand_constants(expression: s.Expr) -> s.Expr: 

37 """Linearize polynomials in MZV/EulerSum using exact strict stuffle products. 

38 

39 Colors multiply when equal summation indices merge. The identity holds at 

40 finite common outer cutoff, then passes to the convergent outer-cutoff 

41 limits used by both constant classes, including alternating leading index 

42 one. No divergent or regularized constants are introduced. Scalar factors 

43 cannot themselves contain endpoint constants inside arbitrary functions. 

44 Output uses MZV for all-positive colors, EulerSum otherwise. Maximum total 

45 weight is twelve, powers eight, and intermediate linear terms 8192. 

46 

47 Args: 

48 expression: Exact polynomial in multiple zeta values and Euler sums. 

49 """ 

50 if not isinstance(expression, s.Expr): 50 ↛ 51line 50 didn't jump to line 51 because the condition on line 50 was never true

51 raise TypeError("Use an exact SymPy expression") 

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

53 raise UnsupportedExpressionError("Stuffle expansion requires exact finite input") 

54 

55 def multiply(left, right): 

56 terms = {} 

57 for a, x in left.items(): 

58 for b, y in right.items(): 

59 if sum(i for i, _ in a + b) > _MAX_WEIGHT: 

60 raise UnsupportedExpressionError("Constant stuffle exceeds weight twelve") 

61 for word, count in _stuffle(a, b): 

62 terms[word] = terms.get(word, s.S.Zero) + x * y * count 

63 if len(terms) > _MAX_TERMS: 63 ↛ 64line 63 didn't jump to line 64 because the condition on line 63 was never true

64 raise UnsupportedExpressionError("Constant stuffle exceeds the term budget") 

65 return terms 

66 

67 def polynomial(node): 

68 if isinstance(node, (MZV, EulerSum)): 

69 indices = as_integer_tuple(node.indices) 

70 if sum(indices) > _MAX_WEIGHT: 70 ↛ 71line 70 didn't jump to line 71 because the condition on line 70 was never true

71 raise UnsupportedExpressionError("Constant stuffle exceeds weight twelve") 

72 colors = (1,) * len(node.indices) if isinstance(node, MZV) else node.colors 

73 return {tuple(zip(indices, as_integer_tuple(colors), strict=True)): s.S.One} 

74 if not node.has(MZV, EulerSum): 

75 return {(): node} 

76 if node.is_Add: 

77 result = {} 

78 for arg in node.args: 

79 for word, coefficient in polynomial(arg).items(): 

80 result[word] = result.get(word, s.S.Zero) + coefficient 

81 if len(result) > _MAX_TERMS: 81 ↛ 82line 81 didn't jump to line 82 because the condition on line 81 was never true

82 raise UnsupportedExpressionError("Constant stuffle exceeds the term budget") 

83 return result 

84 if node.is_Mul: 

85 result = {(): s.S.One} 

86 for arg in node.args: 

87 result = multiply(result, polynomial(arg)) 

88 return result 

89 if node.is_Pow and node.exp.is_Integer and 0 <= node.exp <= 8: 

90 result, base = {(): s.S.One}, polynomial(node.base) 

91 for _ in range(int(node.exp)): 

92 result = multiply(result, base) 

93 return result 

94 raise UnsupportedExpressionError( 

95 "Only polynomial endpoint-constant expressions are supported" 

96 ) 

97 

98 def constant(word): 

99 if not word: 

100 return s.S.One 

101 indices, colors = zip(*word) 

102 return MZV(indices) if all(color == 1 for color in colors) else EulerSum(indices, colors) 

103 

104 return s.Add( 

105 *(coefficient * constant(word) for word, coefficient in polynomial(expression).items()) 

106 )