Coverage for src/hypra/hpl/maps.py: 100.00%
38 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 binary HPL square and fractional argument maps."""
3from functools import lru_cache
5import sympy as s
7from .._typing import Boundary, IntegerWord, Interval
8from ..errors import UnsupportedExpressionError
9from ..symbolic import HPL
10from .reduction import _extract
11from .transforms import _validate
14def _check(word, z, domain):
15 word = _validate(word, z, domain)
16 if any(letter not in (0, 1) for letter in word):
17 raise UnsupportedExpressionError("This argument map supports only binary HPL words")
18 if len(word) > 10:
19 raise UnsupportedExpressionError("Argument map supports weight at most ten")
20 return word
23@lru_cache(maxsize=1024)
24def _pullback(word, mode):
25 """Expand a tensor word under the exact pullback of its one-forms."""
26 if not word:
27 return (((), s.S.One),)
28 images = (
29 {0: ((0, 2),), 1: ((1, 1), (-1, -1))}
30 if mode == "square"
31 else {0: ((0, 1), (1, 1)), 1: ((1, -1),)}
32 )
33 return tuple(
34 ((letter, *tail), factor * coefficient)
35 for letter, factor in images[word[0]]
36 for tail, coefficient in _pullback(word[1:], mode)
37 )
40def _expression(word, z, mode):
41 return s.Add(
42 *(coefficient * (HPL(w, z) if w else s.S.One) for w, coefficient in _pullback(word, mode))
43 )
46def square_hpl(word: IntegerWord, z: s.Symbol, *, domain: Interval | None = None) -> s.Expr:
47 """Express H_word(z**2) at z for binary words, real 0<z<1.
49 The alphabet expands to {-1,0,1}; all trailing-zero constants follow from
50 log(z**2)=2*log(z) on this declared interval.
52 Args:
53 word: Expanded binary word of weight at most ten.
54 z: SymPy symbol for the interior coordinate.
55 domain: Declare `"0<z<1"` explicitly.
56 """
57 return _expression(_check(word, z, domain), z, "square")
60def fractional_hpl(
61 word: IntegerWord,
62 z: s.Symbol,
63 *,
64 boundary: Boundary,
65 domain: Interval | None = None,
66) -> s.Expr:
67 """Express H_word(z/(z-1) +/- i0), with explicit upper/lower boundary.
69 Original letters must be binary. Mixed trailing zeros are extracted by
70 shuffle before imposing log(z/(z-1))=H_0(z)+H_1(z)+/-i*pi.
71 Origin-analytic words have identical boundary values on this negative ray.
73 Args:
74 word: Expanded binary word of weight at most ten.
75 z: SymPy symbol for the interior coordinate.
76 boundary: Side of approach to z/(z-1).
77 domain: Declare `"0<z<1"` explicitly.
78 """
79 word = _check(word, z, domain)
80 if boundary not in ("upper", "lower"):
81 raise UnsupportedExpressionError("Fractional boundary must be 'upper' or 'lower'")
82 if not word:
83 return s.S.One
84 if word[-1] != 0:
85 return _expression(word, z, "fractional")
86 sign = 1 if boundary == "upper" else -1
87 logarithm = HPL((0,), z) + HPL((1,), z) + sign * s.I * s.pi
88 if all(letter == 0 for letter in word):
89 return logarithm ** len(word) / s.factorial(len(word))
90 extracted = _extract(word, z, False, True)
91 return s.expand(
92 extracted.xreplace(
93 {
94 atom: fractional_hpl(atom.word, z, boundary=boundary, domain=domain)
95 for atom in extracted.atoms(HPL)
96 }
97 )
98 )