Coverage for src/hypra/native/unit_limit.py: 88.98%

80 statements  

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

1"""Power/log limits of the rationalized hypergeometric coefficients at one.""" 

2 

3from functools import lru_cache 

4 

5import sympy as s 

6 

7from ..errors import UnsupportedExpressionError 

8from ..hpl.algebra import _LOG, _expansion_size, _local_hpl 

9from ..hpl.inversion import invert_hpl 

10from ..hpl.limits import hpl_limit_at_one 

11from ..hpl.series_one import hpl_series_at_one 

12from ..symbolic import HPL 

13 

14 

15@lru_cache(maxsize=256) 

16def _inverted(word, variable): 

17 return invert_hpl(word, variable, boundary="upper", domain="0<z<1") 

18 

19 

20def unit_coefficient_limit(expression, z): 

21 """Combine finite Laurent/log terms using the positive sqrt(1-z) coordinate.""" 

22 if ( 

23 all(atom.argument == z for atom in expression.atoms(HPL)) 

24 and all(atom.args[1] in (z, -z) for atom in expression.atoms(s.polylog)) 

25 and not expression.has(s.sqrt(1 - z), s.sqrt(z)) 

26 ): 

27 return hpl_limit_at_one(expression, z) 

28 

29 t = s.Dummy("endpoint_root", positive=True) 

30 y = s.Dummy("inverse_argument", positive=True) 

31 coordinates = ( 

32 z, 

33 s.sqrt(z), 

34 (1 - s.sqrt(1 - z)) / (1 + s.sqrt(1 - z)), 

35 s.I * s.sqrt(z / (1 - z)), 

36 ) 

37 # Restore the HPL form of the simple polylogs emitted by the kernels. 

38 polylogs = {} 

39 for atom in expression.atoms(s.polylog): 

40 weight, argument = atom.args 

41 for coordinate in coordinates: 41 ↛ 49line 41 didn't jump to line 49 because the loop on line 41 didn't complete

42 if s.cancel(argument - coordinate) == 0: 

43 polylogs[atom] = HPL((0,) * (int(weight) - 1) + (1,), coordinate) 

44 break 

45 if s.cancel(argument + coordinate) == 0: 

46 polylogs[atom] = -HPL((0,) * (int(weight) - 1) + (-1,), coordinate) 

47 break 

48 else: 

49 raise UnsupportedExpressionError("Unrecognized polylogarithm argument at z=1") 

50 expression = expression.xreplace(polylogs) 

51 atoms = sorted(expression.atoms(HPL), key=s.default_sort_key) 

52 generators = [s.Dummy("endpoint_word") for _ in atoms] 

53 _expansion_size(expression) 

54 replaced = expression.xreplace(dict(zip(atoms, generators, strict=True))) 

55 try: 

56 terms = s.Poly(replaced, *generators).terms() if generators else [((), replaced)] 

57 except s.PolynomialError as exc: 

58 raise UnsupportedExpressionError("Endpoint requires polynomial HPL dependence") from exc 

59 

60 coefficients = [] 

61 depth = 0 

62 for powers, coefficient in terms: 

63 coefficient = coefficient.subs(z, 1 - t**2) 

64 leading = coefficient.as_leading_term(t) 

65 _, power = leading.as_coeff_exponent(t) 

66 if leading == 0: 66 ↛ 67line 66 didn't jump to line 67 because the condition on line 66 was never true

67 continue 

68 if not power.is_Integer or power < -24: 68 ↛ 69line 68 didn't jump to line 69 because the condition on line 68 was never true

69 raise UnsupportedExpressionError("Unsupported rationalized endpoint power") 

70 depth = max(depth, -int(power)) 

71 if power <= 0: 

72 coefficients.append((powers, coefficient, int(power))) 

73 

74 local = [] 

75 for atom in atoms: 

76 argument = s.simplify(atom.argument.subs(z, 1 - t**2)) 

77 if s.limit(argument, t, 0, dir="+") == 1: 

78 distance = s.Symbol("_unit_distance", positive=True) 

79 germ = hpl_series_at_one(atom.word, distance, through=depth).removeO() 

80 germ = germ.subs(distance, s.factor(1 - argument)) 

81 elif s.limit(argument / t**-1, t, 0, dir="+") == s.I: 81 ↛ 95line 81 didn't jump to line 95 because the condition on line 81 was always true

82 # The upper real inversion germ continues to the upper 

83 # imaginary axis, with inverse coordinate approaching from below. 

84 inverted = _inverted(tuple(atom.word), y) 

85 substitutions = {} 

86 for hpl in inverted.atoms(HPL): 

87 substitutions[hpl] = s.Add( 

88 *( 

89 y**power * polynomial.subs(_LOG, s.log(y)) 

90 for power, polynomial in _local_hpl(tuple(hpl.word), depth) 

91 ) 

92 ) 

93 germ = inverted.xreplace(substitutions).subs(y, 1 / argument) 

94 else: 

95 raise UnsupportedExpressionError("Unrecognized HPL coordinate at z=1") 

96 local.append(s.series(germ, t, 0, depth + 1).removeO()) 

97 

98 result = s.S.Zero 

99 for powers, coefficient, valuation in coefficients: 

100 rational = s.series(coefficient, t, 0, 1).removeO().subs(s.log(t), _LOG) 

101 product = s.Poly(1, t) 

102 for germ, power in zip(local, powers, strict=True): 

103 if power: 

104 product *= s.Poly(germ.subs(s.log(t), _LOG), t) ** power 

105 product = s.Poly.from_dict( 

106 {degree: term for degree, term in product.terms() if degree[0] <= -valuation}, t 

107 ) 

108 for (power,), term in s.Poly(s.expand(t**depth * rational), t).terms(): 

109 if power <= depth: 109 ↛ 108line 109 didn't jump to line 108 because the condition on line 109 was always true

110 result += s.Add( 

111 *( 

112 term * product.nth(j) * t ** (power + j - depth) 

113 for j in range(depth - power + 1) 

114 ) 

115 ) 

116 value = s.simplify(s.expand(result)) 

117 if value.has(t, _LOG, s.oo, -s.oo, s.zoo, s.nan, s.Limit): 117 ↛ 118line 117 didn't jump to line 118 because the condition on line 117 was never true

118 raise UnsupportedExpressionError("Could not establish a finite rationalized endpoint") 

119 return value