Coverage for src/hypra/hpl/inversion.py: 96.15%

38 statements  

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

1"""Explicit upper/lower inversion boundary values from real HPL transforms.""" 

2 

3from collections import defaultdict 

4from functools import lru_cache 

5from typing import cast 

6 

7import sympy as s 

8 

9from .._typing import Boundary, IntegerWord, Interval 

10from ..errors import UnsupportedExpressionError 

11from ..symbolic import HPL 

12from .regularization import hpl_endpoint_polynomial 

13from .transforms import _validate 

14 

15_ENDPOINT_LOG = s.Symbol("_inversion_endpoint_log") 

16_PULLBACK = {0: ((0, -1),), 1: ((0, 1), (1, 1)), -1: ((0, -1), (-1, 1))} 

17 

18 

19@lru_cache(maxsize=1024) 

20def _inversion_terms(word, sign): 

21 """Integrate the pulled-back suffix ODE and fix its endpoint constant. 

22 

23 At z->1-, -log(1-(1/z +/- i0)) differs from -log(1-z) 

24 by +/- i*pi plus a vanishing log(z). The endpoint polynomial therefore 

25 determines the integration constant even for divergent leading-one words; 

26 no ordinary finite value is assigned to their singular endpoint. 

27 """ 

28 if not word: 

29 return (((), s.S.One),) 

30 terms: defaultdict[tuple[int, ...], s.Expr] = defaultdict(lambda: s.S.Zero) 

31 for tail, coefficient in _inversion_terms(word[1:], sign): 

32 for letter, factor in _PULLBACK[word[0]]: 

33 terms[(letter, *tail)] += factor * coefficient 

34 endpoint = cast( 

35 s.Expr, 

36 hpl_endpoint_polynomial(word, _ENDPOINT_LOG).subs(_ENDPOINT_LOG, sign * s.I * s.pi), 

37 ) 

38 constant = endpoint - sum( 

39 coefficient * hpl_endpoint_polynomial(w, _ENDPOINT_LOG).subs(_ENDPOINT_LOG, 0) 

40 for w, coefficient in terms.items() 

41 ) 

42 terms[()] = s.expand(constant) 

43 return tuple((w, s.expand(c)) for w, c in terms.items() if c != 0) 

44 

45 

46def invert_hpl( 

47 word: IntegerWord, 

48 z: s.Symbol, 

49 *, 

50 boundary: Boundary, 

51 domain: Interval | None = None, 

52) -> s.Expr: 

53 """Return H_word(1/z +/- i0), for explicitly real 0<z<1. 

54 

55 boundary='upper' selects +i0; 'lower' selects -i0. These choices refer 

56 to the argument 1/z, not the direction of the intermediate negative cut. 

57 Supports ternary words through weight eight, with exact endpoint constants, 

58 and repeated-letter words through weight 24. 

59 

60 Args: 

61 word: Expanded ternary word; repeated-letter words admit higher weight. 

62 z: SymPy symbol for the interior coordinate. 

63 boundary: Side of approach to 1/z. 

64 domain: Declare `"0<z<1"` explicitly. 

65 """ 

66 word = _validate(word, z, domain) 

67 if boundary not in ("upper", "lower"): 

68 raise UnsupportedExpressionError("Inversion boundary must be 'upper' or 'lower'") 

69 if not word: 69 ↛ 70line 69 didn't jump to line 70 because the condition on line 69 was never true

70 return s.S.One 

71 if len(set(word)) == 1: 

72 logarithm = HPL((0,), z) 

73 sign = 1 if boundary == "upper" else -1 

74 primitive = { 

75 0: -logarithm, 

76 1: logarithm + HPL((1,), z) + sign * s.I * s.pi, 

77 -1: HPL((-1,), z) - logarithm, 

78 }[word[0]] 

79 return primitive ** len(word) / s.factorial(len(word)) 

80 if len(word) > 8: 

81 raise UnsupportedExpressionError("General inversion supports weight at most eight") 

82 sign = 1 if boundary == "upper" else -1 

83 return s.Add( 

84 *( 

85 coefficient * (HPL(term, z) if term else s.S.One) 

86 for term, coefficient in _inversion_terms(word, sign) 

87 ) 

88 )