Coverage for src/hypra/hpl/classical.py: 90.24%
60 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"""Proved low-weight conversion to logarithms and classical polylogarithms."""
3from typing import cast
5import sympy as s
7from .._typing import Interval, as_integer_tuple
8from ..errors import UnsupportedExpressionError
9from ..symbolic import HPL, reduce_known_hpl
10from .reduction import _extract, extract_hpl_logs, lyndon_reduce
13def classical_hpl(expression: s.Expr, z: s.Symbol, *, domain: Interval) -> s.Expr:
14 """Reduce every weight-two and weight-three ternary HPL at z.
16 Requires the declared real interval 0<z<1. Higher mixed words remain HPLs;
17 this does not claim a complete high-weight classical-function basis.
19 Args:
20 expression: Exact expression containing HPL atoms.
21 z: Argument of the HPLs to reduce.
22 domain: Declared real interval, `"0<z<1"`.
23 """
24 if not isinstance(expression, s.Expr) or not isinstance(z, s.Symbol): 24 ↛ 25line 24 didn't jump to line 25 because the condition on line 24 was never true
25 raise TypeError("Use a SymPy expression and symbolic argument")
26 if domain != "0<z<1": 26 ↛ 27line 26 didn't jump to line 27 because the condition on line 26 was never true
27 raise UnsupportedExpressionError("Explicit domain='0<z<1' is required")
28 if expression.has(s.Float, s.nan, s.zoo, s.oo, -s.oo): 28 ↛ 29line 28 didn't jump to line 29 because the condition on line 28 was never true
29 raise UnsupportedExpressionError("Classical conversion requires exact finite input")
30 result = reduce_known_hpl(expression)
31 formulas = {
32 (1, 0): -s.polylog(2, z) - s.log(z) * s.log(1 - z),
33 (-1, 0): s.polylog(2, -z) + s.log(z) * s.log(1 + z),
34 (1, -1): -s.log(2) * s.log(1 - z) + s.polylog(2, (1 - z) / 2) - s.polylog(2, s.S.Half),
35 (-1, 1): -s.log(2) * s.log(1 + z) + s.polylog(2, (1 + z) / 2) - s.polylog(2, s.S.Half),
36 }
37 # Binary weight three has only two irreducible words after extracting
38 # leading ones and trailing zeros: 001=Li3(z), and 011 below. The latter
39 # is fixed by derivative log(1-z)**2/(2*z) and its zero origin limit.
40 binary_011 = (
41 s.log(z) * s.log(1 - z) ** 2 / 2
42 + s.log(1 - z) * s.polylog(2, 1 - z)
43 - s.polylog(3, 1 - z)
44 + s.zeta(3)
45 )
46 # A real formula from u=1/(1+z), rather than substitution across the
47 # Li_n(1+z) cuts in the positive-letter formula.
48 u = 1 / (1 + z)
49 L = s.log(1 + z)
50 negative_011 = (
51 s.zeta(3) - s.polylog(3, u) - L * s.polylog(2, u) + L**2 * s.log(z) / 2 - L**3 / 3
52 )
54 # The real substitution u=(1∓z)/2 evaluates H_{∓1,±1,±1}.
55 def opposite_repeat(sign):
56 logarithm = s.log(1 - sign * z)
57 argument = (1 - sign * z) / 2
58 return (
59 s.polylog(3, s.S.Half)
60 + logarithm**2 * s.log((1 + sign * z) / 2) / 2
61 + logarithm * s.polylog(2, argument)
62 - s.polylog(3, argument)
63 )
65 minus_plus_plus = opposite_repeat(1)
66 plus_minus_minus = -opposite_repeat(-1)
67 minus_minus_plus = (
68 -(s.log(1 + z) ** 2) * s.log(1 - z) / 2
69 - s.log(1 + z) * formulas[(1, -1)]
70 + plus_minus_minus
71 )
72 ternary_a = (
73 s.log(1 + z) * s.polylog(2, z) + binary_011.subs(z, z**2) / 2 - negative_011 - binary_011
74 )
75 changes = {}
76 for atom in result.atoms(HPL):
77 if atom.argument != z:
78 if atom.argument.has(z): 78 ↛ 80line 78 didn't jump to line 80 because the condition on line 78 was always true
79 raise UnsupportedExpressionError("First transform HPLs to the common argument")
80 continue
81 word = as_integer_tuple(atom.word)
82 if len(word) == 3 and all(letter in (0, 1) for letter in word):
83 extracted = extract_hpl_logs(atom)
84 changes[atom] = extracted.xreplace({HPL((0, 1, 1), z): binary_011})
85 elif len(word) == 3 and all(letter in (0, -1) for letter in word):
86 # Relabel only the abstract shuffle alphabet, not the argument.
87 # This extracts leading -1 and trailing zero factors without any
88 # complex negation or logarithmic branch substitution.
89 binary = tuple(-letter for letter in word)
90 extracted = _extract(binary, z, True, True)
91 relabeled = extracted.xreplace(
92 {
93 h: HPL(tuple(-letter for letter in as_integer_tuple(h.word)), z)
94 for h in extracted.atoms(HPL)
95 }
96 )
97 changes[atom] = reduce_known_hpl(cast(s.Expr, relabeled)).xreplace(
98 {HPL((0, -1, -1), z): negative_011}
99 )
100 elif len(word) == 3 and all(letter in (-1, 1) for letter in word):
101 # Abstract two-letter shuffle extraction, not argument relabeling.
102 binary = tuple(0 if letter == -1 else 1 for letter in word)
103 extracted = _extract(binary, z, True, True)
104 relabeled = extracted.xreplace(
105 {
106 h: HPL(
107 tuple(-1 if letter == 0 else 1 for letter in as_integer_tuple(h.word)), z
108 )
109 for h in extracted.atoms(HPL)
110 }
111 )
112 replacements = {HPL(w, z): f for w, f in formulas.items()}
113 replacements.update(
114 {HPL((-1, -1, 1), z): minus_minus_plus, HPL((-1, 1, 1), z): minus_plus_plus}
115 )
116 changes[atom] = reduce_known_hpl(cast(s.Expr, relabeled.xreplace(replacements)))
117 elif len(word) == 3:
118 # A square-log identity proves A=H(-1,0,1). Pulling A back by
119 # M(z)=(1-z)/(1+z) and reducing its shuffle words solves for the
120 # other genuinely ternary Lyndon primitive B=H(-1,1,0).
121 a_at_one = (
122 s.log(2) * s.zeta(2)
123 - s.Rational(3, 2) * s.zeta(3)
124 + s.polylog(3, s.S.Half)
125 + s.log(2) * s.polylog(2, s.S.Half)
126 + s.log(2) ** 3 / 3
127 )
128 hm = HPL((-1,), z)
129 hm1 = HPL((-1, 1), z)
130 known = (
131 a_at_one
132 - s.zeta(2) * hm
133 - s.log(2) * hm**2 / 2
134 + hm**3 / 6
135 - s.log(2) * hm1
136 + hm1 * hm
137 - HPL((-1, -1, 0), z)
138 - 2 * HPL((-1, -1, 1), z)
139 )
140 ternary_b = classical_hpl(known, z, domain=domain) - ternary_a.subs(
141 z, (1 - z) / (1 + z)
142 )
143 reduced = lyndon_reduce(atom).xreplace(
144 {HPL((-1, 0, 1), z): ternary_a, HPL((-1, 1, 0), z): ternary_b}
145 )
146 # Remaining words use at most two letters, so recursion decreases
147 # the number of genuinely ternary primitive atoms to zero.
148 changes[atom] = classical_hpl(cast(s.Expr, reduced), z, domain=domain)
149 elif word in formulas:
150 changes[atom] = formulas[cast(tuple[int, int], word)]
151 return cast(s.Expr, result.xreplace(changes))