Coverage for src/hypra/hpl/real_continuation.py: 87.50%

40 statements  

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

1"""Explicit real-axis boundary continuation, without arbitrary winding paths.""" 

2 

3from typing import cast 

4 

5import sympy as s 

6 

7from .._typing import Boundary, IntegerWord, RealRegion, as_integer_tuple 

8from ..errors import UnsupportedExpressionError 

9from ..symbolic import HPL 

10from .algebra import _as_hpl 

11from .inversion import invert_hpl 

12from .transforms import negate_hpl 

13 

14 

15def analytic_continue_hpl( 

16 word: IntegerWord, 

17 x: s.Symbol, 

18 *, 

19 region: RealRegion, 

20 boundary: Boundary, 

21) -> s.Expr: 

22 """Continue H_word(x +/- i0) in a caller-declared open real region. 

23 

24 Regions are '0<x<1', '-1<x<0', 'x>1', 'x<-1'; boundary is mandatory 

25 'upper' (+i0) or 'lower' (-i0). This is a domain contract, not an inferred 

26 inequality. No value at x=0,+1,-1 or arbitrary winding is assigned. 

27 

28 Args: 

29 word: Expanded ternary word. 

30 x: Real argument variable. 

31 region: Declared open interval for x. 

32 boundary: Side of approach to the real axis. 

33 

34 Returns: 

35 Exact expression in interior HPLs and constants. 

36 """ 

37 if not isinstance(x, s.Symbol): 37 ↛ 38line 37 didn't jump to line 38 because the condition on line 37 was never true

38 raise TypeError("Continuation variable must be a SymPy Symbol") 

39 atom = HPL(word, x) 

40 word = as_integer_tuple(atom.word) 

41 if region not in ("0<x<1", "-1<x<0", "x>1", "x<-1"): 

42 raise UnsupportedExpressionError("Unrecognized open real continuation region") 

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

44 raise UnsupportedExpressionError("Continuation boundary must be 'upper' or 'lower'") 

45 if len(word) > 24 or (region in ("x>1", "x<-1") and len(word) > 8 and len(set(word)) > 1): 

46 raise UnsupportedExpressionError("Continuation exceeds its supported weight limit") 

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

48 return s.S.One 

49 if region == "0<x<1": 

50 return atom 

51 t = s.Dummy("real_interval_argument", positive=True) 

52 if region == "x>1": 

53 return invert_hpl(word, t, boundary=boundary, domain="0<z<1").subs(t, 1 / x) 

54 negated = negate_hpl(word, t, domain="0<z<1") 

55 if boundary == "lower": 

56 negated = negated.xreplace({s.I: -s.I}) 

57 if region == "-1<x<0": 

58 return cast(s.Expr, negated.subs(t, -x)) 

59 # x=-y reverses the boundary: upper x means lower y, and conversely. 

60 negated = _as_hpl(negated, t) 

61 for function in negated.atoms(s.Function): 

62 if function.has(t) and not isinstance(function, HPL): 62 ↛ 63line 62 didn't jump to line 63 because the condition on line 62 was never true

63 raise UnsupportedExpressionError("Unmapped variable function in continuation") 

64 atoms = negated.atoms(HPL) 

65 if any( 65 ↛ 69line 65 didn't jump to line 69 because the condition on line 65 was never true

66 term.xreplace({h: s.S.One for h in atoms}).has(t) 

67 for term in s.Add.make_args(s.expand(negated)) 

68 ): 

69 raise UnsupportedExpressionError("Unmapped variable coefficient in continuation") 

70 opposite = "lower" if boundary == "upper" else "upper" 

71 return s.expand( 

72 negated.xreplace( 

73 {h: invert_hpl(h.word, t, boundary=opposite, domain="0<z<1") for h in atoms} 

74 ) 

75 ).subs(t, -1 / x)