Coverage for src/hypra/nielsen.py: 91.28%

103 statements  

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

1"""Exact Nielsen generalized-polylogarithm notation, with explicit convention. 

2 

3S_{n,p}(z) = H_{0^n,1^p}(z), n,p positive integers. This is a lossless 

4notation layer, not a general HPL-to-classical-polylogarithm reduction. 

5""" 

6 

7from typing import Self, cast 

8 

9import sympy as s 

10from sympy.core.function import ArgumentIndexError 

11 

12from ._typing import ExactInteger, ExactScalar 

13from .symbolic import HPL 

14 

15 

16class NielsenPolylog(s.Function): 

17 r"""S_{n,p}(z)=HPL((0,)*n+(1,)*p,z), n,p>=1. 

18 

19 On 0<z<1 this equals 

20 (-1)**(n+p-1)/((n-1)! p!) * integral_0^1 

21 log(t)**(n-1) * log(1-z*t)**p dt/t. 

22 Elsewhere the symbol uses the matching principal HPL continuation, with 

23 no automatic branch transformations. Construction retains this notation. 

24 """ 

25 

26 nargs = 3 

27 

28 def __new__( 

29 cls, 

30 n: ExactInteger, 

31 p: ExactInteger, 

32 argument: ExactScalar, 

33 **options: object, 

34 ) -> Self: 

35 """Construct the exact SymPy function. 

36 

37 Args: 

38 n: Positive count of zero letters. 

39 p: Positive count of one letters. 

40 argument: SymPy argument of the generalized polylogarithm. 

41 **options: Keyword options forwarded to SymPy's function constructor. 

42 """ 

43 for index in (n, p): 

44 if isinstance(index, bool) or not isinstance(index, (int, s.Integer)) or index < 1: 

45 raise ValueError("Nielsen indices n and p must be exact positive integers") 

46 if n + p > 10000: 46 ↛ 47line 46 didn't jump to line 47 because the condition on line 46 was never true

47 raise ValueError("Nielsen weight exceeds the notation resource limit") 

48 return cast( 

49 Self, 

50 super().__new__(cls, s.Integer(n), s.Integer(p), argument, **options), 

51 ) 

52 

53 @property 

54 def n(self) -> s.Integer: 

55 """Number of initial zeros in the expanded HPL word.""" 

56 return cast(s.Integer, self.args[0]) 

57 

58 @property 

59 def p(self) -> s.Integer: 

60 """Number of final ones in the expanded HPL word.""" 

61 return cast(s.Integer, self.args[1]) 

62 

63 @property 

64 def argument(self) -> s.Expr: 

65 """Argument of the generalized polylogarithm.""" 

66 return cast(s.Expr, self.args[2]) 

67 

68 def as_hpl(self) -> HPL: 

69 """Losslessly expand into zeros followed by ones, without reduction.""" 

70 return HPL((0,) * int(self.n) + (1,) * int(self.p), self.argument) 

71 

72 def fdiff(self, argindex: int = 3) -> s.Expr: 

73 if argindex != 3: 73 ↛ 74line 73 didn't jump to line 74 because the condition on line 73 was never true

74 raise ArgumentIndexError(self, argindex) 

75 if self.n > 1: 

76 return NielsenPolylog(int(self.n) - 1, self.p, self.argument) / self.argument 

77 return (-s.log(1 - self.argument)) ** self.p / (s.factorial(self.p) * self.argument) 

78 

79 def _eval_derivative(self, s): 

80 return self.fdiff(3) * self.argument.diff(s) 

81 

82 def _eval_rewrite_as_HPL(self, n, p, argument, **hints): 

83 return self.as_hpl() 

84 

85 def _eval_rewrite_as_polylog(self, n, p, argument, **hints): 

86 if p == 1: 86 ↛ 88line 86 didn't jump to line 88 because the condition on line 86 was always true

87 return s.polylog(n + 1, argument) 

88 return self 

89 

90 def _eval_evalf(self, prec): 

91 """Evaluate using the principal HPL germ, retaining ambiguous cut values.""" 

92 import mpmath as mp 

93 from sympy.core.evalf import prec_to_dps 

94 

95 argument = self.argument 

96 if argument.is_number is not True or argument.is_finite is not True or self.n + self.p > 24: 96 ↛ 97line 96 didn't jump to line 97 because the condition on line 96 was never true

97 return self 

98 if argument == 0: 

99 return s.S.Zero 

100 if argument == 1: 

101 from .hpl.endpoints import hpl_at_one 

102 

103 return hpl_at_one(self.as_hpl().word).evalf(prec_to_dps(prec) + 4) 

104 if argument.is_negative is True and argument != -1: 

105 from .hpl.boundary_numeric import evaluate_hpl_boundary 

106 

107 digits = max(2, prec_to_dps(prec) + 8) 

108 if digits > 800: 108 ↛ 109line 108 didn't jump to line 109 because the condition on line 108 was never true

109 return self 

110 region = "-1<x<0" if (argument + 1).is_positive is True else "x<-1" 

111 result = evaluate_hpl_boundary( 

112 self.as_hpl().word, argument, region=region, boundary="upper", dps=digits 

113 ) 

114 # Binary words ending in 1 are regular at zero; the negative ray 

115 # is not a cut for Nielsen functions. Their exact value is real. 

116 return s.Float(mp.nstr(result.value.real, n=digits), precision=prec) 

117 if argument == -1: 

118 reduced = reduce_nielsen(self) 

119 return reduced.evalf(prec_to_dps(prec) + 4) if reduced != self else self 

120 result = self.as_hpl()._eval_evalf(prec) 

121 return self if result.has(HPL) else result 

122 

123 def _latex(self, printer): 

124 return ( 

125 rf"S_{{{printer._print(self.n)},{printer._print(self.p)}}}" 

126 rf"\!\left({printer._print(self.argument)}\right)" 

127 ) 

128 

129 

130def _expression(expression: ExactScalar) -> s.Expr: 

131 if type(expression) is int: 131 ↛ 132line 131 didn't jump to line 132 because the condition on line 131 was never true

132 expression = s.Integer(expression) 

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

134 raise TypeError("Provide an exact symbolic expression, not source text") 

135 return expression 

136 

137 

138def nielsen_to_hpl(expression: ExactScalar) -> s.Expr: 

139 """Replace every Nielsen atom by its exact expanded HPL word. 

140 

141 Args: 

142 expression: Exact symbolic expression containing Nielsen atoms. 

143 """ 

144 expression = _expression(expression) 

145 return cast( 

146 s.Expr, 

147 expression.xreplace({atom: atom.as_hpl() for atom in expression.atoms(NielsenPolylog)}), 

148 ) 

149 

150 

151def hpl_to_nielsen(expression: ExactScalar) -> s.Expr: 

152 """Replace only words 0^n 1^p (n,p positive); leave other HPLs untouched. 

153 

154 Args: 

155 expression: Exact symbolic expression containing HPL atoms. 

156 """ 

157 expression = _expression(expression) 

158 replacements = {} 

159 for atom in expression.atoms(HPL): 

160 word = tuple(atom.word) 

161 n = 0 

162 while n < len(word) and word[n] == 0: 

163 n += 1 

164 p = len(word) - n 

165 if n and p and all(letter == 1 for letter in word[n:]): 

166 replacements[atom] = NielsenPolylog(n, p, atom.argument) 

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

168 

169 

170def reduce_nielsen(expression: ExactScalar) -> s.Expr: 

171 """Reduce p=1 to Li_{n+1}, and exact constants at zero and +/-1. 

172 

173 Endpoint constants use proved MZV/Euler-sum reductions, retaining exact 

174 irreducible constants. Special-value reductions are bounded at weight 24. 

175 

176 Args: 

177 expression: Exact symbolic expression containing Nielsen atoms. 

178 """ 

179 expression = _expression(expression) 

180 from .hpl.alternating import hpl_alternating_at_one, reduce_euler_sum 

181 from .hpl.endpoints import hpl_at_one 

182 

183 replacements = {} 

184 for atom in expression.atoms(NielsenPolylog): 

185 if atom.p == 1: 

186 replacements[atom] = s.polylog(atom.n + 1, atom.argument) 

187 elif atom.argument == 0: 

188 replacements[atom] = s.S.Zero 

189 elif atom.n + atom.p <= 24: 189 ↛ 184line 189 didn't jump to line 184 because the condition on line 189 was always true

190 if atom.argument == 1: 

191 replacements[atom] = hpl_at_one(atom.as_hpl().word) 

192 elif atom.argument == -1: 

193 word = (0,) * int(atom.n) + (-1,) * int(atom.p) 

194 replacements[atom] = (-1) ** atom.p * reduce_euler_sum(hpl_alternating_at_one(word)) 

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