Coverage for src/hypra/native/twof1_shift.py: 0.00%

93 statements  

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

1"""General 2F1 integer shifts of supported half-integer canonical systems.""" 

2 

3from __future__ import annotations 

4 

5from typing import cast 

6 

7import sympy as s 

8 

9from ..errors import UnsupportedExpressionError 

10from ..symbolic import reduce_known_hpl 

11from .integer import _truncate 

12from .matched_half import half_parameters 

13from .planning import laurent_coefficient 

14 

15 

16def _operations(upper, lower): 

17 bases: list[s.Expr] = [s.Rational(1, 2) if base.q == 2 else s.S.Zero for base, _ in upper] 

18 c = s.Rational(1, 2) if lower[0][0].q == 2 else s.S.One 

19 operations = [] 

20 for i, (target, _) in enumerate(upper): 

21 while bases[i] < target: 

22 loss = int(bases[i] == 0) 

23 operations.append(("upper_up", i, tuple(bases), c, loss)) 

24 bases[i] += 1 

25 while bases[i] > target: 

26 loss = int(c - bases[i] == 0) 

27 operations.append(("upper_down", i, tuple(bases), c, loss)) 

28 bases[i] -= 1 

29 target = lower[0][0] 

30 while c > target: 

31 operations.append(("lower_down", None, tuple(bases), c, int(c - 1 == 0))) 

32 c -= 1 

33 while c < target: 

34 loss = int(c - bases[0] == 0) + int(c - bases[1] == 0) 

35 operations.append(("lower_up", None, tuple(bases), c, loss)) 

36 c += 1 

37 return operations 

38 

39 

40def extra_depth(h, eps): 

41 parsed = half_parameters(h, eps) 

42 if parsed is None or len(h.ap) != 2 or len(h.bq) != 1: 

43 return 0 

44 upper, lower = parsed 

45 return sum(op[-1] for op in _operations(upper, lower)) 

46 

47 

48def expand_shifted_twof1(h, eps, order): 

49 """Canonical ODE systems plus algebraic contiguous parameter shifts. 

50 

51 Independent formal slopes avoid illegal division by a zero slope. Apparent 

52 slope singularities are cancelled before the requested rational slopes are 

53 substituted. No parameter limit is numerically guessed. 

54 """ 

55 from .half_system import canonical_half_series 

56 

57 parsed = half_parameters(h, eps) 

58 if parsed is None or len(h.ap) != 2 or len(h.bq) != 1: 

59 return None 

60 upper, lower = parsed 

61 r = sum(base.q == 2 for base, _ in upper) 

62 q = sum(base.q == 2 for base, _ in lower) 

63 if (r, q) not in ((0, 1), (1, 0), (1, 1), (2, 1)): 

64 return None 

65 if lower[0][0].is_Integer and lower[0][0] <= 0 and lower[0][1] == 0: 

66 raise UnsupportedExpressionError("Fixed singular lower parameter") 

67 terminating = [ 

68 -int(base) for base, slope in upper if base.is_Integer and base <= 0 and slope == 0 

69 ] 

70 if terminating: 

71 result = sum( 

72 h.argument**n 

73 / s.factorial(n) 

74 * s.prod(s.rf(a, n) for a in h.ap) 

75 / s.prod(s.rf(b, n) for b in h.bq) 

76 for n in range(min(terminating) + 1) 

77 ) 

78 return _truncate(result, eps, order, int(lower[0][0].is_Integer and lower[0][0] <= 0)) 

79 alpha, beta, gamma = s.symbols("hypra_alpha hypra_beta hypra_gamma") 

80 formal = (alpha, beta) 

81 z = s.Dummy("z", positive=True) 

82 canonical_upper = [s.Rational(1, 2) if base.q == 2 else s.S.Zero for base, _ in upper] 

83 canonical_lower = [s.Rational(1, 2) if lower[0][0].q == 2 else s.S.One] 

84 operations = _operations(upper, lower) 

85 depth = order + sum(op[-1] for op in operations) 

86 if (r, q) == (2, 1): 

87 base = canonical_half_series( 

88 [0, 0], [gamma - alpha, gamma - beta], [s.Rational(1, 2)], [gamma], z, eps, depth 

89 ) 

90 prefactor = sum( 

91 ((gamma - alpha - beta) * eps * s.log(1 - z)) ** k / s.factorial(k) 

92 for k in range(depth + 1) 

93 ) / s.sqrt(1 - z) 

94 result = _truncate(prefactor * base, eps, depth) 

95 else: 

96 result = canonical_half_series( 

97 canonical_upper, formal, canonical_lower, [gamma], z, eps, depth 

98 ) 

99 if result is None: 

100 return None 

101 theta = lambda f: z * s.diff(f, z) 

102 pole_bound = 0 

103 for operation, index, bases, cbase, loss in operations: 

104 a, b = [base + slope * eps for base, slope in zip(bases, formal, strict=True)] 

105 c = cbase + gamma * eps 

106 if operation == "upper_up": 

107 chosen = (a, b)[index] 

108 result = (theta(result) + chosen * result) / chosen 

109 elif operation == "upper_down": 

110 chosen, other = (a, b) if index == 0 else (b, a) 

111 result = ((c - chosen - other * z) * result + (1 - z) * theta(result)) / (c - chosen) 

112 elif operation == "lower_down": 

113 result = (theta(result) + (c - 1) * result) / (c - 1) 

114 if cbase - 1 == 0: 

115 pole_bound += 1 

116 else: 

117 result = c * ((1 - z) * theta(result) / z + (c - a - b) * result) / ((c - a) * (c - b)) 

118 depth -= loss 

119 result = _truncate(result, eps, depth, pole_bound) 

120 actual = {alpha: upper[0][1], beta: upper[1][1], gamma: lower[0][1]} 

121 final = s.S.Zero 

122 for k in range(-pole_bound, order + 1): 

123 coefficient = s.cancel(laurent_coefficient(cast(s.Expr, result), eps, k)) 

124 value = coefficient.subs(actual, simultaneous=True) 

125 if value.has(s.nan, s.zoo, s.oo, -s.oo): 

126 raise UnsupportedExpressionError( 

127 "Degenerate contiguous slope limit was not resolved exactly" 

128 ) 

129 final += value * eps**k 

130 return reduce_known_hpl(final).xreplace({z: h.argument})