Coverage for src/hypra/polylog_reduction.py: 88.97%

92 statements  

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

1"""Named-function reductions generated from exact iterated-integral identities. 

2 

3The weight-four/five Nielsen relations needed by HypExp can be obtained by 

4linear algebra, without a formula table. Expand weighted monomials of classical 

5polylogarithms and logarithms into a common binary HPL alphabet. Shuffle their 

6products, and solve for the desired Nielsen word. Constant terms from the 

7argument transformations are retained as separate rows, fixing integration 

8constants and the specified cut side along with the nonconstant terms. 

9""" 

10 

11from functools import lru_cache 

12from typing import cast 

13 

14import sympy as s 

15 

16from ._typing import Boundary, Interval 

17from .errors import UnsupportedExpressionError 

18from .hpl.algebra import _linear_terms 

19from .nielsen import NielsenPolylog, reduce_nielsen 

20from .polylogs import transform_polylog_arguments 

21 

22_Z = s.Symbol("polylog_reduction_argument", positive=True) 

23_PHASE, _ZETA3, _ZETA5 = s.symbols("polylog_phase polylog_zeta3 polylog_zeta5") 

24_ARGUMENTS = ( 

25 _Z, 

26 1 - _Z, 

27 _Z / (_Z - 1), 

28 1 - 1 / _Z, 

29 1 / _Z, 

30 1 / (1 - _Z), 

31) 

32_GENERATORS = ( 

33 (s.log(_Z), 1), 

34 (s.log(1 - _Z), 1), 

35 (s.I * s.pi, 1), 

36 (s.polylog(2, _Z), 2), 

37 (s.polylog(3, _Z), 3), 

38 (s.polylog(3, 1 - _Z), 3), 

39 (s.zeta(3), 3), 

40 *((s.polylog(weight, argument), weight) for weight in (4, 5) for argument in _ARGUMENTS[:3]), 

41 (NielsenPolylog(3, 2, _Z), 5), 

42 (s.zeta(5), 5), 

43) 

44 

45 

46@lru_cache(maxsize=128) 

47def _monomials(weight: int, offset: int = 0) -> tuple[s.Expr, ...]: 

48 if weight == 0: 

49 return (s.S.One,) 

50 if offset == len(_GENERATORS): 

51 return () 

52 expression, degree = _GENERATORS[offset] 

53 return tuple( 

54 expression**power * tail 

55 for power in range(weight // degree + 1) 

56 for tail in _monomials(weight - power * degree, offset + 1) 

57 ) 

58 

59 

60def _features(expression: s.Expr, boundary: Boundary = "upper"): 

61 """Map each HPL word and formal constant monomial to a rational value. 

62 

63 Use i*pi rather than pi as the weight-one constant. The coefficients of 

64 principal cut phases are then rational too. Equating these formal rows is 

65 sufficient for an identity; no transcendence or constant-independence 

66 conjecture is needed. In particular, the empty HPL word is never dropped. 

67 """ 

68 transformed = transform_polylog_arguments(expression, _Z, domain="0<z<1", boundary=boundary) 

69 features = {} 

70 for word, coefficient in _linear_terms(transformed, _Z).items(): 

71 if coefficient.has(_Z): 71 ↛ 72line 71 didn't jump to line 72 because the condition on line 71 was never true

72 raise UnsupportedExpressionError("Named-function basis has a nonconstant coefficient") 

73 formal = coefficient.xreplace({s.pi: -s.I * _PHASE, s.zeta(3): _ZETA3, s.zeta(5): _ZETA5}) 

74 polynomial = s.Poly(s.expand(formal), _PHASE, _ZETA3, _ZETA5) 

75 for monomial, value in polynomial.terms(): 

76 if value.is_Rational is not True: 76 ↛ 77line 76 didn't jump to line 77 because the condition on line 76 was never true

77 raise UnsupportedExpressionError("Named-function constants exceed the exact basis") 

78 if value: 78 ↛ 75line 78 didn't jump to line 75 because the condition on line 78 was always true

79 features[word, monomial] = value 

80 return features 

81 

82 

83@lru_cache(maxsize=5) 

84def _basis(weight: int): 

85 candidates = tuple( 

86 sorted( 

87 set(_monomials(weight)), 

88 key=lambda value: (s.count_ops(value), s.default_sort_key(value)), 

89 ) 

90 ) 

91 columns = [_features(candidate) for candidate in candidates] 

92 rows = tuple(sorted(set().union(*columns))) 

93 matrix = s.ImmutableMatrix([[column.get(row, 0) for column in columns] for row in rows]) 

94 return candidates, rows, matrix 

95 

96 

97@lru_cache(maxsize=180) 

98def _normal_form(n: int, p: int, mode: int, boundary: Boundary) -> s.Expr: 

99 argument = _ARGUMENTS[mode] 

100 source = s.polylog(n + 1, argument) if p == 1 else NielsenPolylog(n, p, argument) 

101 target = _features(source, boundary) 

102 candidates, rows, matrix = _basis(n + p) 

103 if set(target).difference(rows): 103 ↛ 104line 103 didn't jump to line 104 because the condition on line 103 was never true

104 raise UnsupportedExpressionError("Polylogarithm exceeds the named-function basis") 

105 try: 

106 solution = matrix.gauss_jordan_solve( 

107 s.ImmutableMatrix([target.get(row, 0) for row in rows]) 

108 ) 

109 except ValueError as exc: 

110 raise UnsupportedExpressionError( 

111 "Polylogarithm has no proved named-function reduction" 

112 ) from exc 

113 coefficients, parameters = solution[0], solution[1] 

114 if parameters.rows: 114 ↛ 115line 114 didn't jump to line 115 because the condition on line 114 was never true

115 raise UnsupportedExpressionError("Named-function basis is unexpectedly dependent") 

116 return s.Add( 

117 *(coefficients[index, 0] * candidate for index, candidate in enumerate(candidates)) 

118 ) 

119 

120 

121def reduce_polylogs( 

122 expression: s.Expr, 

123 z: s.Symbol, 

124 *, 

125 domain: Interval, 

126 boundary: Boundary | None = None, 

127) -> s.Expr: 

128 """Reduce classical and Nielsen polylogarithms through weight five. 

129 

130 On domain='0<z<1', reduce atoms at z, 1-z, z/(z-1), 1-1/z, 1/z and 

131 1/(1-z) to logs, classical polylogarithms and the single Nielsen function 

132 S_{3,2}(z). In particular S_{2,2} becomes classical and S_{2,3} reduces to 

133 that weight-five basis. The coefficients are generated by exact shuffle 

134 identities and rational linear algebra, with no stored reduction table. 

135 

136 For arguments above one, boundary='upper' or 'lower' specifies the side 

137 of each original polylogarithm argument. Reduced atoms have real arguments 

138 below one; explicit i*pi terms carry the cut data. 

139 Higher weights and unrecognized arguments remain unchanged. Nielsen 

140 constants at zero and +/-1 use reduce_nielsen's exact endpoint reductions. 

141 

142 Args: 

143 expression: Exact expression containing classical or Nielsen polylogs. 

144 z: Common real interior variable, a commutative SymPy Symbol. 

145 domain: Explicit declaration '0<z<1'. 

146 boundary: Required side for transformed arguments above one. 

147 """ 

148 if not isinstance(expression, s.Expr) or not isinstance(z, s.Symbol): 

149 raise TypeError("Use an exact SymPy expression and symbolic target argument") 

150 if z.is_commutative is not True: 

151 raise TypeError("The target argument must be commutative") 

152 if domain != "0<z<1": 

153 raise UnsupportedExpressionError("Explicit domain='0<z<1' is required") 

154 if boundary not in (None, "upper", "lower"): 

155 raise ValueError("boundary must be upper, lower, or omitted") 

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

157 raise UnsupportedExpressionError("Polylogarithm reduction requires exact finite input") 

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

159 raise UnsupportedExpressionError("Polylogarithm expression exceeds the operation budget") 

160 expression = reduce_nielsen(expression) 

161 replacements = {} 

162 arguments = tuple(cast(s.Expr, argument.xreplace({_Z: z})) for argument in _ARGUMENTS) 

163 for atom in expression.atoms(s.polylog, NielsenPolylog): 

164 argument = cast(s.Expr, atom.args[-1]) 

165 if not argument.has(z): 165 ↛ 166line 165 didn't jump to line 166 because the condition on line 165 was never true

166 continue 

167 if isinstance(atom, NielsenPolylog): 

168 n, p = int(atom.n), int(atom.p) 

169 else: 

170 order = atom.args[0] 

171 if not isinstance(order, s.Integer) or not 1 <= order <= 5: 

172 continue 

173 n, p = int(order) - 1, 1 

174 if n + p > 5: 

175 continue 

176 mode = next( 

177 (index for index, target in enumerate(arguments) if s.cancel(argument - target) == 0), 

178 None, 

179 ) 

180 if mode is None: 

181 continue 

182 if mode in (4, 5) and boundary is None: 

183 raise UnsupportedExpressionError("Arguments above one require an explicit boundary") 

184 replacements[atom] = _normal_form(n, p, mode, boundary or "upper").xreplace({_Z: z}) 

185 return cast(s.Expr, expression.xreplace(replacements))