Coverage for src/hypra/hpl/special.py: 95.59%
98 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-26 22:12 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-26 22:12 +0000
1"""Exact origin-continuation HPL values at i, without fitted constant tables."""
3from functools import lru_cache
5import sympy as s
7from .._typing import IntegerWord
8from ..errors import UnsupportedExpressionError
9from ..symbolic import HPL, reduce_known_hpl
10from .classical import classical_hpl
11from .multiple_polylog import MultiplePolylog
12from .notation import HPLPM, PMWord, _pm_indices, letters_to_pm, pm_to_letters
13from .numeric import _word
14from .reduction import _extract
17def _quarter_period_polylog(order, sign):
18 """Split Li_n(±i) into its even eta and odd Dirichlet-beta sums."""
19 if order == 2:
20 beta = s.Catalan
21 elif order % 2:
22 half = (order - 1) // 2
23 beta = (
24 (-1) ** half
25 * s.euler(2 * half)
26 * s.pi**order
27 / (4 ** (half + 1) * s.factorial(2 * half))
28 )
29 else:
30 # Separate the two odd residue classes in the Hurwitz zeta. Its
31 # relation to polygamma uses only the convergent defining series.
32 beta = 2 * s.polygamma(order - 1, s.Rational(1, 4)) / (
33 4**order * s.factorial(order - 1)
34 ) - (1 - s.Rational(1, 2) ** order) * s.zeta(order)
35 return -(1 - s.Rational(1, 2) ** (order - 1)) * s.zeta(order) / 2**order + sign * s.I * beta
38def _reduce_at_i_constants(expression):
39 expression = expression.xreplace(
40 {
41 atom: s.polylog(atom.args[0], s.simplify(atom.args[1]))
42 for atom in expression.atoms(s.polylog)
43 }
44 )
45 replacements = {}
46 for atom in expression.atoms(s.polylog):
47 order, argument = atom.args
48 if order.is_Integer is not True or order < 2: 48 ↛ 49line 48 didn't jump to line 49 because the condition on line 48 was never true
49 continue
50 order = int(order)
51 if order == 3 and argument == (1 + s.I) / 2:
52 # Compare the cubic antiderivative of H_011(i) with its exact
53 # shuffle/Möbius/square reduction. Both integration constants are
54 # fixed at the origin; their difference proves this conjugate
55 # relation without fitting numerical values or assuming a basis.
56 replacements[atom] = (
57 -s.polylog(3, (1 - s.I) / 2)
58 - 5 * s.pi**2 * s.log(2) / 96
59 + s.log(2) ** 3 / 24
60 + 35 * s.zeta(3) / 32
61 )
62 elif order == 3 and argument == s.S.Half:
63 # The trilogarithm reflection identity at its midpoint.
64 replacements[atom] = 7 * s.zeta(3) / 8 - s.pi**2 * s.log(2) / 12 + s.log(2) ** 3 / 6
65 elif argument in (s.I, -s.I):
66 replacements[atom] = _quarter_period_polylog(order, 1 if argument == s.I else -1)
67 elif order == 2 and argument in ((1 + s.I) / 2, (1 - s.I) / 2):
68 # Landen's identity Li2(z/(z-1))=-Li2(z)-log(1-z)^2/2,
69 # at z=∓i. The radial path meets no transformed branch cut.
70 original = -s.I if argument == (1 + s.I) / 2 else s.I
71 replacements[atom] = (
72 -_quarter_period_polylog(2, 1 if original == s.I else -1)
73 - s.log(1 - original) ** 2 / 2
74 )
75 elif (s.Abs(argument) - 1).is_positive is True:
76 # Jonquiere inversion on principal branches. Exact positive real
77 # arguments take the same lower-cut boundary as SymPy polylog.
78 logarithm = s.log(-argument)
79 replacements[atom] = -((-1) ** order) * s.polylog(order, 1 / argument) - (
80 (2 * s.pi * s.I) ** order
81 / s.factorial(order)
82 * s.bernoulli(order, s.S.Half + logarithm / (2 * s.pi * s.I))
83 )
84 result = expression.xreplace(replacements)
85 # Inversion may introduce (1±i)/2 dilogarithms, so reduce those in a
86 # second pass. Inversion strictly decreases the modulus and cannot cycle.
87 if result != expression and result.has(s.polylog):
88 result = _reduce_at_i_constants(result)
89 return s.expand(result.xreplace({atom: s.expand_complex(atom) for atom in result.atoms(s.log)}))
92@lru_cache(maxsize=512)
93def _segment(word, start, displacement):
94 """Iterated integral of a word with no singular final letter at start."""
95 indices, arguments = [], []
96 zeros, previous = 0, displacement
97 for letter in word:
98 pole = letter - start
99 if pole == 0:
100 zeros += 1
101 else:
102 indices.append(zeros + 1)
103 arguments.append(s.expand(s.cancel(previous / pole)))
104 zeros, previous = 0, pole
105 sign = (-1) ** (len(indices) - word.count(1))
106 return sign * MultiplePolylog(s.Tuple(*indices), s.Tuple(*arguments))
109@lru_cache(maxsize=512)
110def _first_half(word):
111 if not word:
112 return s.S.One
113 midpoint = s.I / 2
114 z = s.Symbol("_hpl_midpoint")
115 extracted = _extract(word, z, False, True)
116 return extracted.xreplace(
117 {
118 atom: s.log(midpoint) if atom.word == (0,) else _segment(tuple(atom.word), 0, midpoint)
119 for atom in extracted.atoms(HPL)
120 }
121 )
124def _at_i_multiple_polylogs(word):
125 # Compose the first half with the reversal of the segment i -> i/2.
126 # Both series have cumulative argument products with modulus <= 1/2.
127 return _first_half(word) + s.Add(
128 *(
129 (-1) ** cut * _segment(word[:cut][::-1], s.I, -s.I / 2) * _first_half(word[cut:])
130 for cut in range(1, len(word) + 1)
131 )
132 )
135@lru_cache(maxsize=1093)
136def _ordinary_at_i_raw(word: tuple[int, ...]) -> s.Expr:
137 """Exact H_word(i) on continuation from the origin through the upper quadrant.
139 All ternary words through weight six reduce to exact logarithms and
140 classical or multiple polylogarithms. Pure repeated letters and simple
141 classical words are also supported through weight 24.
143 Args:
144 word: Expanded ternary word evaluated at the positive imaginary unit.
145 """
146 if not word: 146 ↛ 147line 146 didn't jump to line 147 because the condition on line 146 was never true
147 return s.S.One
148 z = s.Dummy("real_formula_argument", positive=True)
149 if len(word) <= 3:
150 # Obtain the proved real formula first, then continue it analytically.
151 # Continue along z=i*t. All transformed arguments avoid their cuts,
152 # except the cubic square-log identity: its argument 1-z**2 reaches
153 # the lower polylog cut and log(z**2) the upper logarithm cut. Those
154 # are exactly the principal boundary values used by SymPy here.
155 # This does not widen classical_hpl's real-only public contract.
156 formula = classical_hpl(HPL(word, z), z, domain="0<z<1")
157 else:
158 formula = reduce_known_hpl(HPL(word, z))
159 if formula.has(HPL):
160 if len(word) <= 6:
161 return _at_i_multiple_polylogs(word)
162 raise UnsupportedExpressionError(
163 "At i, mixed words above weight six are not implemented"
164 )
165 value = formula.subs(z, s.I)
166 return _reduce_at_i_constants(value)
169@lru_cache(maxsize=1093)
170def _ordinary_at_i_reduced(word: tuple[int, ...]) -> s.Expr:
171 if len(word) > 6:
172 return _ordinary_at_i_raw(word)
173 from .at_i_reduction import at_i_weight_reductions
175 basis = at_i_weight_reductions(len(word))
176 return s.expand(
177 s.Add(
178 *(
179 coefficient * basis[_pm_indices(pm_word)]
180 for pm_word, coefficient in letters_to_pm(word).items()
181 )
182 )
183 )
186def hpl_at_i(word: IntegerWord) -> s.Expr:
187 """Return the exact ordinary HPL at i in the HPL 2.0 constant basis.
189 Through weight six, generated identities leave one, nine and 23 chosen
190 HPLPM primitives at weights four, five and six. Their numerical evaluation
191 uses independent convergent MPL expressions. Repeated single letters and
192 simple classical words extend to weight 24.
193 """
194 return _ordinary_at_i_reduced(_word(word))
197@lru_cache(maxsize=1093)
198def _pm_at_i_raw(word: tuple[int, ...]) -> s.Expr:
199 """Cached independent exact MPL expression, bypassing the canonical reducer.
201 This private hook lets retained HPLPM at-i primitives evaluate numerically
202 without calling their own symbolic reduction again.
203 """
204 terms = pm_to_letters(HPLPM.from_indices(word, s.I).word)
205 return s.Add(
206 *(coefficient * _ordinary_at_i_raw(letters) for letters, coefficient in terms.items())
207 )
210def hpl_pm_at_i(word: PMWord) -> s.Expr:
211 """Return the exact plus/minus HPL at i in the canonical weight-six basis."""
212 indices = _pm_indices(word)
213 if len(word) > 6: 213 ↛ 214line 213 didn't jump to line 214 because the condition on line 213 was never true
214 raise UnsupportedExpressionError("At i, plus/minus words support weight at most six")
215 from .at_i_reduction import at_i_weight_reductions
217 return at_i_weight_reductions(len(indices))[indices]