Coverage for src/hypra/polylogs.py: 88.24%

64 statements  

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

1"""Branch-explicit polylogarithm argument transformations from iterated integrals. 

2 

3The six cross-ratio arguments in HypExp Appendix A.1 permute 0, 1 and infinity. 

4Their pulled-back one-forms therefore close on the binary HPL alphabet. Each 

5transformation below integrates those forms recursively and fixes its constant 

6using the endpoint logarithmic polynomial. No transformation table is used. 

7""" 

8 

9from functools import lru_cache 

10from typing import cast 

11 

12import sympy as s 

13 

14from ._typing import Boundary, Interval 

15from .errors import UnsupportedExpressionError 

16from .hpl.regularization import hpl_endpoint_polynomial 

17from .nielsen import NielsenPolylog 

18from .symbolic import HPL, reduce_known_hpl 

19 

20_T = s.Symbol("polylog_endpoint_log") 

21# Images of dx/x and dx/(1-x), expressed in dz/z and dz/(1-z). 

22_FORMS = { 

23 "reflection": {0: ((1, -1),), 1: ((0, -1),)}, 

24 "fractional": {0: ((0, 1), (1, 1)), 1: ((1, -1),)}, 

25 "inverse": {0: ((0, -1),), 1: ((0, 1), (1, 1))}, 

26 "inverse_reflection": {0: ((1, 1),), 1: ((0, -1), (1, -1))}, 

27 "reflection_inverse": {0: ((0, -1), (1, -1)), 1: ((0, 1),)}, 

28} 

29 

30 

31@lru_cache(maxsize=2048) 

32def _word_transform(word: tuple[int, ...], mode: str, sign: int): 

33 """Return a linear HPL combination as (word, exact coefficient) pairs. 

34 

35 Inversion and 1-1/z are normalized at z=1. The other maps are normalized 

36 at z=0. Endpoint finite parts fix integration constants without assigning 

37 finite values to divergent functions. For x>1 on the selected side, 

38 -log(1-x) has finite part sign*i*pi at the preimage of x=1. 

39 """ 

40 if not word: 

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

42 terms = {} 

43 for tail, coefficient in _word_transform(word[1:], mode, sign): 

44 for letter, factor in _FORMS[mode][word[0]]: 

45 key = (letter, *tail) 

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

47 

48 if mode in ("reflection", "inverse", "inverse_reflection"): 

49 phase = 0 if mode == "reflection" else sign * s.I * s.pi 

50 constant = hpl_endpoint_polynomial(word, _T).subs(_T, phase) 

51 else: 

52 # At x=0, only all-zero words have a logarithmic constant. 

53 constant = ( 

54 (sign * s.I * s.pi) ** len(word) / s.factorial(len(word)) 

55 if all(letter == 0 for letter in word) 

56 else s.S.Zero 

57 ) 

58 if mode in ("inverse", "reflection_inverse"): 

59 constant -= s.Add( 

60 *( 

61 coefficient * hpl_endpoint_polynomial(key, _T).subs(_T, 0) 

62 for key, coefficient in terms.items() 

63 ) 

64 ) 

65 terms[()] = constant 

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

67 

68 

69def transform_polylog_arguments( 

70 expression: s.Expr, 

71 z: s.Symbol, 

72 *, 

73 domain: Interval, 

74 boundary: Boundary | None = None, 

75) -> s.Expr: 

76 """Express classical and Nielsen polylogarithms at a common real argument. 

77 

78 Supported arguments are z, 1-z, z/(z-1), 1-1/z, 1/z and 1/(1-z), 

79 with domain='0<z<1' and total weight at most eight per atom. Arguments 

80 above one require boundary='upper' or 'lower', referring to the argument 

81 of each original function. Negative arguments are analytic for these 

82 origin-regular words and need no side choice. 

83 

84 The result uses exact binary HPLs at z, with simple words replaced by 

85 logarithms and classical polylogarithms. It covers the argument relations 

86 in HypExp Appendix A.1 without imposing its particular named-function 

87 basis or changing global evaluation rules. Constant arguments are retained; 

88 unknown variable arguments and unsupported orders raise explicitly. 

89 

90 Args: 

91 expression: Exact expression containing SymPy polylog or NielsenPolylog. 

92 z: Common interior argument, a commutative SymPy Symbol. 

93 domain: Declare the real interval '0<z<1'. 

94 boundary: Optional upper or lower side for arguments above one. 

95 """ 

96 if not isinstance(expression, s.Expr) or not isinstance(z, s.Symbol): 96 ↛ 97line 96 didn't jump to line 97 because the condition on line 96 was never true

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

98 if z.is_commutative is not True: 98 ↛ 99line 98 didn't jump to line 99 because the condition on line 98 was never true

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

100 if domain != "0<z<1": 100 ↛ 101line 100 didn't jump to line 101 because the condition on line 100 was never true

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

102 if boundary not in (None, "upper", "lower"): 102 ↛ 103line 102 didn't jump to line 103 because the condition on line 102 was never true

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

104 if expression.has(s.Float, s.nan, s.zoo, s.oo, -s.oo): 104 ↛ 105line 104 didn't jump to line 105 because the condition on line 104 was never true

105 raise UnsupportedExpressionError("Polylogarithm transformation requires exact finite input") 

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

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

108 candidates = ( 

109 (z, "identity"), 

110 (1 - z, "reflection"), 

111 (z / (z - 1), "fractional"), 

112 (1 / z, "inverse"), 

113 (1 / (1 - z), "inverse_reflection"), 

114 (1 - 1 / z, "reflection_inverse"), 

115 ) 

116 replacements = {} 

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

118 argument = atom.args[-1] 

119 if not argument.has(z): 

120 continue 

121 if isinstance(atom, NielsenPolylog): 

122 weight = int(atom.n + atom.p) 

123 else: 

124 order = atom.args[0] 

125 if not isinstance(order, s.Integer) or order < 1: 

126 raise UnsupportedExpressionError( 

127 "Classical polylog orders must be positive integers" 

128 ) 

129 weight = int(order) 

130 if weight > 8: 

131 raise UnsupportedExpressionError( 

132 "Polylogarithm transformation supports weight at most eight" 

133 ) 

134 word = ( 

135 (0,) * int(atom.n) + (1,) * int(atom.p) 

136 if isinstance(atom, NielsenPolylog) 

137 else (0,) * (weight - 1) + (1,) 

138 ) 

139 mode = next((name for target, name in candidates if s.cancel(argument - target) == 0), None) 

140 if mode is None: 

141 raise UnsupportedExpressionError(f"Unsupported polylogarithm argument {argument}") 

142 if mode in ("inverse", "inverse_reflection") and boundary is None: 

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

144 if mode == "identity": 

145 replacements[atom] = HPL(word, z) 

146 else: 

147 replacements[atom] = s.Add( 

148 *( 

149 coefficient * (HPL(key, z) if key else s.S.One) 

150 for key, coefficient in _word_transform( 

151 word, mode, -1 if boundary == "lower" else 1 

152 ) 

153 ) 

154 ) 

155 return reduce_known_hpl(cast(s.Expr, expression.xreplace(replacements)))