Coverage for src/hypra/native/matched_half.py: 75.59%

87 statements  

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

1"""Matched half-integer parameter reductions in the rational variable sqrt(z).""" 

2 

3from __future__ import annotations 

4 

5import sympy as s 

6 

7from ..errors import UnsupportedExpressionError 

8from ..symbolic import reduce_known_hpl 

9from .integer import _truncate 

10from .nested_sums import _slope 

11from .planning import _exact_rational_valuation, laurent_coefficient 

12 

13 

14def half_parameters(h, eps): 

15 rows = [] 

16 for parameters in (h.ap, h.bq): 

17 row = [] 

18 for parameter in parameters: 

19 try: 

20 p = s.Poly(parameter, eps) 

21 except s.PolynomialError: 

22 return None 

23 base, slope = p.nth(0), p.nth(1) 

24 if p.degree() > 1 or not base.is_Rational or base.q not in (1, 2): 

25 return None 

26 try: 

27 slope = _slope(slope, eps) 

28 except (TypeError, ValueError): 

29 return None 

30 row.append((base, slope)) 

31 rows.append(row) 

32 return rows[0], rows[1] 

33 

34 

35def expand_matched_half(h, eps, order): 

36 """Expand matched upper/lower half counts, with arbitrary integer offsets.""" 

37 from ..hpl.algebra import integrate_from_zero 

38 from .colored_sums import canonical_colored_hyper_series 

39 

40 parsed = half_parameters(h, eps) 

41 if parsed is None: 41 ↛ 42line 41 didn't jump to line 42 because the condition on line 41 was never true

42 return None 

43 upper, lower = parsed 

44 nh = sum(base.q == 2 for base, _ in upper) 

45 if nh == 0 or nh != sum(base.q == 2 for base, _ in lower): 

46 return None 

47 if len(upper) != len(lower) + 1: 47 ↛ 48line 47 didn't jump to line 48 because the condition on line 47 was never true

48 raise UnsupportedExpressionError("Matched-half engine requires pF(p-1)") 

49 if any(base.is_Integer and base <= 0 and slope == 0 for base, slope in lower): 49 ↛ 50line 49 didn't jump to line 50 because the condition on line 49 was never true

50 raise UnsupportedExpressionError("Fixed singular lower parameter") 

51 poles = sum(1 for base, _ in lower if base.is_Integer and base <= 0) 

52 terminating = [ 

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

54 ] 

55 if terminating: 55 ↛ 56line 55 didn't jump to line 56 because the condition on line 55 was never true

56 value = sum( 

57 h.argument**n 

58 / s.factorial(n) 

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

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

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

62 ) 

63 return _truncate(value, eps, order, poles) 

64 if any(base < 0 for base, _ in upper): 64 ↛ 65line 64 didn't jump to line 65 because the condition on line 64 was never true

65 shift = int(s.floor(-min(base for base, _ in upper + lower))) + 1 

66 coefficient, prefix = s.S.One, s.S.Zero 

67 for n in range(shift): 

68 if n: 

69 coefficient *= s.prod(a + n - 1 for a in h.ap) / ( 

70 n * s.prod(b + n - 1 for b in h.bq) 

71 ) 

72 prefix += coefficient * h.argument**n 

73 coefficient *= s.prod(a + shift - 1 for a in h.ap) / ( 

74 shift * s.prod(b + shift - 1 for b in h.bq) 

75 ) 

76 valuation = _exact_rational_valuation(coefficient, eps, s.true) 

77 tail_h = s.hyper( 

78 [1, *(a + shift for a in h.ap)], 

79 [shift + 1, *(b + shift for b in h.bq)], 

80 h.argument, 

81 evaluate=False, 

82 ) 

83 tail = expand_matched_half(tail_h, eps, order + max(0, -valuation)) 

84 return _truncate(prefix + coefficient * h.argument**shift * tail, eps, order, poles) 

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

86 theta = lambda f: t * s.diff(f, t) / 2 

87 depth = order + poles 

88 base_upper = [ 

89 (s.Rational(1, 2) if base.q == 2 else (0 if base == 0 else 1), slope) 

90 for base, slope in upper 

91 ] 

92 base_lower = [(s.Rational(1, 2) if base.q == 2 else 1, slope) for base, slope in lower] 

93 result = canonical_colored_hyper_series(base_upper, base_lower, t**2, eps, depth) 

94 for (base, slope), (initial, _) in zip(upper, base_upper, strict=True): 

95 for step in range(int(base - initial)): 

96 a = initial + step + slope * eps 

97 result = _truncate((theta(result) + a * result) / a, eps, depth, poles) 

98 for (base, slope), (initial, _) in zip(lower, base_lower, strict=True): 

99 for step in range(int(initial - base)): 

100 new = initial - step - 1 

101 c = new + slope * eps 

102 if new == 0: 102 ↛ 103line 102 didn't jump to line 103 because the condition on line 102 was never true

103 depth -= 1 

104 result = _truncate((theta(result) + c * result) / c, eps, depth, poles) 

105 for (base, slope), (initial, _) in zip(lower, base_lower, strict=True): 

106 for step in range(int(base - initial)): 

107 current = initial + step 

108 expfactor = sum( 

109 (2 * slope * eps * s.log(t)) ** k / s.factorial(k) for k in range(depth + poles + 1) 

110 ) 

111 weighted = _truncate(t ** (2 * current - 1) * expfactor * result, eps, depth, poles) 

112 integrated = s.S.Zero 

113 for k in range(-poles, depth + 1): 

114 coeff = laurent_coefficient(weighted, eps, k) 

115 if coeff: 

116 integrated += eps**k * integrate_from_zero(coeff, t) 

117 inverse = sum( 

118 (-2 * slope * eps * s.log(t)) ** k / s.factorial(k) 

119 for k in range(depth + poles + 1) 

120 ) 

121 result = _truncate( 

122 2 * (current + slope * eps) * t ** (-2 * current) * inverse * integrated, 

123 eps, 

124 depth, 

125 poles, 

126 ) 

127 return reduce_known_hpl(_truncate(result, eps, order, poles)).xreplace({t: s.sqrt(h.argument)})