Coverage for src/hypra/hpl/transforms.py: 91.18%
48 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"""Branch-explicit, bounded argument transformations of harmonic polylogarithms."""
3from functools import lru_cache
4from typing import cast
6import sympy as s
8from .._typing import IntegerWord, Interval, as_integer_tuple
9from ..errors import UnsupportedExpressionError
10from ..symbolic import HPL
11from .algebra import integrate_from_zero, shuffle_words
12from .endpoints import hpl_at_one
13from .reduction import _extract
16def _validate(word, z, domain):
17 if domain != "0<z<1":
18 raise UnsupportedExpressionError("Explicit domain='0<z<1' is required")
19 if not isinstance(z, s.Symbol): 19 ↛ 20line 19 didn't jump to line 20 because the condition on line 19 was never true
20 raise TypeError("The argument must be a SymPy Symbol")
21 checked = HPL(word, z)
22 word = as_integer_tuple(checked.word)
23 if len(word) > 24: 23 ↛ 24line 23 didn't jump to line 24 because the condition on line 23 was never true
24 raise UnsupportedExpressionError("Transformation weight exceeds 24")
25 return word
28def _h(word, z):
29 return HPL(word, z) if word else s.S.One
32def negate_hpl(word: IntegerWord, z: s.Symbol, *, domain: Interval | None = None) -> s.Expr:
33 """Express H_word(-z) at positive z on the upper principal-cut boundary.
35 Trailing zeroes are first extracted with shuffle algebra. Their explicit
36 logarithms acquire +i*pi; origin-analytic factors use letter reversal.
38 Args:
39 word: Expanded ternary word.
40 z: SymPy symbol for the interior coordinate.
41 domain: Declare `"0<z<1"` explicitly.
42 """
43 word = _validate(word, z, domain)
44 if not word: 44 ↛ 45line 44 didn't jump to line 45 because the condition on line 44 was never true
45 return s.S.One
46 if all(a == 0 for a in word):
47 return (s.log(z) + s.I * s.pi) ** len(word) / s.factorial(len(word))
48 if word[-1] == 0:
49 expanded = _extract(word, z, False, True)
50 return cast(
51 s.Expr,
52 expanded.xreplace(
53 {atom: negate_hpl(atom.word, z, domain=domain) for atom in expanded.atoms(HPL)}
54 ),
55 )
56 return (-1) ** sum(a != 0 for a in word) * _h(tuple(-a for a in word), z)
59@lru_cache(maxsize=256)
60def _reflect(word, z):
61 if not word:
62 return s.S.One
63 if all(a == 1 for a in word):
64 return (-s.log(z)) ** len(word) / s.factorial(len(word))
65 if word[0] == 1:
66 # Shuffle H_1 H_tail. Every other word has fewer leading ones,
67 # so recursion strictly decreases the leading-one count at this weight.
68 counts = shuffle_words((1,), word[1:])
69 multiplicity = counts.pop(word)
70 return s.expand(
71 (
72 -s.log(z) * _reflect(word[1:], z)
73 - sum(count * _reflect(other, z) for other, count in counts.items())
74 )
75 / multiplicity
76 )
77 endpoint = hpl_at_one(word)
78 return endpoint - integrate_from_zero(_reflect(word[1:], z) / (1 - z), z)
81def reflect_hpl(word: IntegerWord, z: s.Symbol, *, domain: Interval | None = None) -> s.Expr:
82 """Express H_word(1-z) at z for binary words of weight at most eight.
84 The explicit real interval excludes all branch ambiguities. Endpoint constants retain exact MZV objects where unreduced. The extra
85 letter generated by -1 is not guessed.
87 Args:
88 word: Expanded binary word.
89 z: SymPy symbol for the interior coordinate.
90 domain: Declare `"0<z<1"` explicitly.
91 """
92 word = _validate(word, z, domain)
93 if len(word) > 8 or any(a not in (0, 1) for a in word):
94 raise UnsupportedExpressionError("Reflection supports binary words through weight eight")
95 return _reflect(word, z)