Coverage for src/hypra/native/half_system.py: 100.00%

88 statements  

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

1"""Canonical half-integer hypergeometric epsilon systems. 

2 

3Independent differential-equation implementation, not upstream package code. 

4The solution is the analytic germ at z=0, continued on the principal sheet. 

5The rationalizing variables use principal square roots. In case A, the path 

6for 0<z<1 is the positive imaginary t axis; the integrals are analytic germs, 

7not an assertion that a real-path integral may be substituted arbitrarily. 

8""" 

9 

10from __future__ import annotations 

11 

12import sympy as s 

13 

14from ..hpl.algebra import integrate_from_zero 

15 

16 

17def canonical_half_series(upper_base, upper_slopes, lower_base, lower_slopes, z, epsilon, order): 

18 """Return a canonical expansion, or None for an unsupported base pattern. 

19 

20 Slopes may be exact symbolic expressions independent of z and epsilon. 

21 Only pF(p-1), p>=2 is covered. The returned polynomial includes order. 

22 Integration/order failures propagate, never returning a partial series. 

23 """ 

24 if isinstance(order, bool) or not isinstance(order, int) or order < 0: 

25 raise ValueError("order must be a nonnegative integer") 

26 a, b = tuple(map(s.sympify, upper_base)), tuple(map(s.sympify, lower_base)) 

27 alpha, beta = tuple(map(s.sympify, upper_slopes)), tuple(map(s.sympify, lower_slopes)) 

28 p = len(a) 

29 if len(alpha) != p or len(beta) != len(b): 

30 raise ValueError("Each parameter needs one slope") 

31 if p < 2 or len(b) != p - 1: 

32 return None 

33 if any( 

34 x.has(epsilon, s.Float, s.oo, -s.oo, s.zoo, s.nan) or (isinstance(z, s.Symbol) and x.has(z)) 

35 for x in alpha + beta 

36 ): 

37 raise ValueError("Slopes must be exact and independent of argument and regulator") 

38 half = s.Rational(1, 2) 

39 if all(x == half for x in a + b): 

40 return _all_half_series(alpha, beta, z, epsilon, order) 

41 if all(x == 0 for x in a) and b.count(half) == 1 and all(x in (half, 1) for x in b): 

42 kind = "A" 

43 elif a.count(half) == 1 and all(x in (half, 0) for x in a) and all(x == 1 for x in b): 

44 kind = "B" 

45 elif p == 2 and sorted(a) == [0, half] and b == (half,): 

46 kind = "C" 

47 else: 

48 return None 

49 t = s.Dummy("t", positive=True) 

50 x = s.Dummy("theta") 

51 if kind == "A": 

52 zt = t**2 / (t**2 - 1) 

53 theta_factor = t * (1 - t**2) / 2 

54 argument = s.I * s.sqrt(z / (1 - z)) 

55 elif kind == "B": 

56 zt = 4 * t / (1 + t) ** 2 

57 theta_factor = t * (1 + t) / (1 - t) 

58 argument = (1 - s.sqrt(1 - z)) / (1 + s.sqrt(1 - z)) 

59 else: 

60 zt = t**2 

61 theta_factor = t / 2 

62 argument = s.sqrt(z) 

63 operator = s.Poly( 

64 s.expand( 

65 x * s.prod(x + v - 1 + w * epsilon for v, w in zip(b, beta)) 

66 - zt * s.prod(x + v + w * epsilon for v, w in zip(a, alpha)) 

67 ), 

68 epsilon, 

69 ) 

70 

71 def theta(value): 

72 return s.expand(theta_factor * s.diff(value, t)) 

73 

74 def apply(poly, value): 

75 if poly == 0 or value == 0: 

76 return s.S.Zero 

77 coefficients = s.Poly(poly, x) 

78 powers = [value] 

79 for _ in range(int(coefficients.degree())): 

80 powers.append(theta(powers[-1])) 

81 return s.expand(sum(c * powers[int(m[0])] for m, c in coefficients.terms())) 

82 

83 def invert(value): 

84 value = s.expand(value) 

85 if value == 0: 

86 return s.S.Zero 

87 if kind == "A": 

88 result = t * integrate_from_zero(2 * value / t**2, t) 

89 elif kind == "B": 

90 result = (1 + t) / (1 - t) * integrate_from_zero(value / t, t) 

91 else: 

92 result = t / (1 - t**2) * integrate_from_zero(2 * value / t**2, t) 

93 for _ in range(p - 1): 

94 result = integrate_from_zero(s.expand(result / theta_factor), t) 

95 return s.expand(result) 

96 

97 coefficients: list[s.Expr] = [s.S.One] 

98 for n in range(1, order + 1): 

99 forcing = -sum( 

100 apply(operator.nth(k), coefficients[n - k]) 

101 for k in range(1, min(n, int(operator.degree())) + 1) 

102 ) 

103 coefficients.append(invert(forcing)) 

104 return sum(value.subs(t, argument) * epsilon**n for n, value in enumerate(coefficients)) 

105 

106 

107def _all_half_series(alpha, beta, z, epsilon, order): 

108 """Gauge-normalized all-half pF(p-1) germ, using only ordinary HPL letters. 

109 

110 L0=(theta-1/2)^(p-1) B in this order, B=(1-z)theta-z/2. 

111 Gauge F=(1-z)^(-1/2)Y, z=t²/(t²-1): conjugated theta 

112 is t(1-t²)/2 d/dt -t²/2. Conjugated B=t/2 d/dt, and 

113 conjugated (theta-1/2)=t(1-t²)/2 d/dt -(1+t²)/2. 

114 Both inverse kernels have only 0,±1 poles. Origin analyticity excludes 

115 the fractional-power homogeneous solutions of the latter operator. 

116 """ 

117 t, x = s.Dummy("t", positive=True), s.Dummy("theta") 

118 half = s.Rational(1, 2) 

119 zt = t**2 / (t**2 - 1) 

120 operator = s.Poly( 

121 s.expand( 

122 x * s.prod(x - half + b * epsilon for b in beta) 

123 - zt * s.prod(x + half + a * epsilon for a in alpha) 

124 ), 

125 epsilon, 

126 ) 

127 

128 def apply(poly, value): 

129 if poly == 0 or value == 0: 

130 return s.S.Zero 

131 polynomial = s.Poly(poly, x) 

132 powers = [value] 

133 for _ in range(int(polynomial.degree())): 

134 last = powers[-1] 

135 powers.append(s.expand(t * (1 - t**2) * s.diff(last, t) / 2 - t**2 * last / 2)) 

136 return s.expand(sum(c * powers[int(m[0])] for m, c in polynomial.terms())) 

137 

138 coefficients: list[s.Expr] = [s.S.One] 

139 for n in range(1, order + 1): 

140 forcing = -sum( 

141 apply(operator.nth(k), coefficients[n - k]) 

142 for k in range(1, min(n, int(operator.degree())) + 1) 

143 ) 

144 for _ in range(len(beta)): 

145 forcing = t / (1 - t**2) * integrate_from_zero(s.expand(2 * forcing / t**2), t) 

146 coefficients.append(integrate_from_zero(s.expand(2 * forcing / t), t)) 

147 argument = s.I * s.sqrt(z / (1 - z)) 

148 return sum(c.subs(t, argument) * epsilon**n for n, c in enumerate(coefficients)) / s.sqrt(1 - z)