Coverage for src/hypra/native/atom.py: 68.40%
166 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-26 22:12 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-26 22:12 +0000
1"""Dispatch a single hypergeometric atom to an exact native kernel."""
3import sympy as s
4from sympy.logic.boolalg import Boolean
6from ..errors import UnsupportedExpressionError
7from ..symbolic import HPL, reduce_known_hpl
8from .planning import _positive, plan_expansion
11def _inside_interval(z, assumptions):
12 return _positive(z, assumptions) and _positive(1 - z, assumptions)
15def _linear(expr, eps, base):
16 """Return rational slope if expr=base+slope*eps, else None."""
17 slope = s.expand(expr).coeff(eps)
18 if slope is not None and slope.is_Rational and s.expand(expr - base - slope * eps) == 0:
19 return slope
20 return None
23def _zero_base_twof1(b, c, z, eps, order):
24 # f1=b H_1; fk= -c H_(0,word)+(b-c) H_(1,word).
25 result = s.S.One
26 words = {(1,): b}
27 for k in range(1, order + 1):
28 result += eps**k * sum(coef * HPL(word, z) for word, coef in words.items())
29 following = {}
30 for word, coef in words.items():
31 for letter, factor in ((0, -c), (1, b - c)):
32 value = coef * factor
33 if value:
34 following[(letter, *word)] = value
35 words = following
36 return reduce_known_hpl(result)
39def _li(k, z):
40 return -s.log(1 - z) if k == 1 else s.polylog(k, z)
43def _half_telescoping(b, z, eps, order):
44 t = s.sqrt(z)
46 def odd_sum(k):
47 return s.Integer(2) ** (k - 1) * (_li(k, t) - _li(k, -t)) / t
49 result = (s.log(1 + t) - s.log(1 - t)) / (2 * t)
50 for k in range(1, order + 1):
51 result += (-1) ** (k - 1) * b**k * (odd_sum(k) - odd_sum(k + 1) / 2) * eps**k
52 return result
55def _half_even(a, z, eps, order):
56 t = s.sqrt(z)
57 return sum(
58 ((-2 * a) ** k / (2 * s.factorial(k))) * (s.log(1 - t) ** k + s.log(1 + t) ** k) * eps**k
59 for k in range(order + 1)
60 )
63def expand_atom(h, eps, order, assumptions: Boolean = s.true, argument_domain="real-interval"):
64 """Expand an origin-regular germ on the selected argument domain."""
65 if h.argument == 0: 65 ↛ 66line 65 didn't jump to line 66 because the condition on line 65 was never true
66 return s.S.One
67 z = h.argument
68 if z == 1:
69 from .endpoints import convergent_unit
70 from .planning import laurent_coefficient
71 from .unit_limit import unit_coefficient_limit
73 if not convergent_unit(h, eps): 73 ↛ 74line 73 didn't jump to line 74 because the condition on line 73 was never true
74 raise UnsupportedExpressionError("Unit endpoint requires strictly positive base excess")
75 if order == 0:
76 from .endpoints import rewrite_endpoints
78 base = h.subs(eps, 0)
79 if not any(b.is_Integer and b <= 0 for b in base.bq) and any( 79 ↛ 82line 79 didn't jump to line 82 because the condition on line 79 was never true
80 a.is_Integer and a <= 0 for a in base.ap
81 ):
82 return rewrite_endpoints(base, eps, assumptions)
83 regular_z = s.Dummy("unit_argument", positive=True)
84 regular_h = s.hyper(h.ap, h.bq, regular_z, evaluate=False)
85 regular = expand_atom(
86 regular_h, eps, order, s.And(assumptions, regular_z < 1), "real-interval"
87 )
88 poles = plan_expansion(h, eps, order, assumptions).pole_bound
89 return s.Add(
90 *(
91 eps**k * unit_coefficient_limit(laurent_coefficient(regular, eps, k), regular_z)
92 for k in range(-poles, order + 1)
93 )
94 )
95 if argument_domain == "principal-disk" and (
96 z.free_symbols or not _positive(1 - s.Abs(z), s.true) or z.is_zero is not False
97 ):
98 raise UnsupportedExpressionError(
99 "Principal-disk expansion requires an exact numeric argument with 0 < |z| < 1"
100 )
101 if argument_domain == "principal-cut-plane" and (
102 z.free_symbols
103 or z.is_finite is not True
104 or not (z.is_real is False or _positive(1 - z, s.true))
105 ):
106 raise UnsupportedExpressionError(
107 "Principal-cut-plane expansion requires an exact finite numeric point off [1,infinity)"
108 )
109 if z.is_negative or (z.is_negative is None and _positive(-z, assumptions)):
110 return _expand_negative(h, eps, order, assumptions)
111 if (argument_domain == "real-interval" or z.is_real is not False) and not _inside_interval(
112 z, assumptions
113 ):
114 raise UnsupportedExpressionError(
115 "Native hypergeometric expansion requires argument < 0 or 0 < argument < 1"
116 )
117 return _expand_interior(h, eps, order, assumptions)
120def _expand_negative(h, eps, order, assumptions):
121 """Continue from above to z=-x (x>0) using the kernels' exact pullbacks."""
122 from ..hpl.maps import fractional_hpl
123 from ..hpl.transforms import negate_hpl
125 t = s.Dummy("interior_argument", positive=True)
126 x = s.Dummy("magnitude", positive=True)
127 germ = _expand_interior(s.hyper(h.ap, h.bq, t, evaluate=False), eps, order, assumptions)
128 root = s.sqrt(1 + x)
129 negative_coordinates = (-s.sqrt(x) / root, (1 - root) / (1 + root))
130 changes = {}
131 for atom in germ.atoms(HPL):
132 if atom.argument == t:
133 # Integer kernel: z=y/(y-1), where 0<y=x/(1+x)<1 even for x>=1.
134 mapped = fractional_hpl(atom.word, t, boundary="upper", domain="0<z<1")
135 changes[atom] = mapped.xreplace({t: x / (1 + x)})
136 else:
137 at_negative = atom.argument.xreplace({t: -x})
138 # The matched-half coordinate +i*sqrt(x) is already off-cut.
139 if at_negative == s.I * s.sqrt(x):
140 continue
141 # The A/D and B coordinates approach (-1,0) from above. Algebraic
142 # expansion in the contiguous engine can distribute their quotients.
143 for coordinate in negative_coordinates: 143 ↛ 149line 143 didn't jump to line 149 because the loop on line 143 didn't complete
144 if s.cancel(at_negative - coordinate) == 0:
145 mapped = negate_hpl(atom.word, t, domain="0<z<1")
146 changes[atom] = mapped.xreplace({t: -coordinate})
147 break
148 else:
149 raise UnsupportedExpressionError(
150 "Unrecognized HPL coordinate on the negative real axis"
151 )
152 result = germ.xreplace(changes).xreplace({t: -x})
153 return reduce_known_hpl(result).xreplace({x: -h.argument})
156def _expand_interior(h, eps, order, assumptions):
157 z = h.argument
158 if len(h.ap) == 1 and not h.bq:
159 return s.series((1 - z) ** (-h.ap[0]), eps, 0, order + 1).removeO()
160 # Preserve compact classical forms for two common half-integer subfamilies.
161 if len(h.ap) == 2 and len(h.bq) == 1:
162 upper, lower = tuple(h.ap), h.bq[0]
163 if s.S.One in upper:
164 other = upper[1] if upper[0] == 1 else upper[0]
165 b = _linear(other, eps, s.Rational(1, 2))
166 c = _linear(lower, eps, s.Rational(3, 2))
167 if b is not None and b == c:
168 return _half_telescoping(b, z, eps, order)
169 if lower == s.Rational(1, 2):
170 for first, second in (upper, upper[::-1]):
171 a = _linear(first, eps, s.S.Zero)
172 b = _linear(second, eps, s.Rational(1, 2))
173 if a is not None and a == b:
174 return _half_even(a, z, eps, order)
175 from .canonical import build_canonical, canonical_bases, use_contiguous
176 from .contiguous import expand_contiguous
177 from .matched_half import half_parameters
179 parsed = half_parameters(h, eps)
180 if parsed is not None and use_contiguous(*parsed):
181 ap, bq = parsed
182 ca, cb = canonical_bases(ap, bq)
183 coordinate = s.Dummy("argument", positive=True)
184 result = expand_contiguous(
185 ap, bq, ca, cb, coordinate, eps, order, build_canonical, assumptions=assumptions
186 )
187 return reduce_known_hpl(result).xreplace({coordinate: z})
188 from .integer import expand_integer_hyper
190 integer_result = expand_integer_hyper(h, eps, order)
191 if integer_result is not None:
192 return integer_result
193 from .matched_half import expand_matched_half
195 matched_result = expand_matched_half(h, eps, order)
196 if matched_result is not None:
197 return matched_result
198 if len(h.ap) != 2 or len(h.bq) != 1: 198 ↛ 217line 198 didn't jump to line 217 because the condition on line 198 was always true
199 from .half_system import canonical_half_series
200 from .matched_half import half_parameters
202 parsed = half_parameters(h, eps)
203 if parsed is not None: 203 ↛ 216line 203 didn't jump to line 216 because the condition on line 203 was always true
204 ap, bq = parsed
205 result = canonical_half_series(
206 [a for a, _ in ap],
207 [a for _, a in ap],
208 [b for b, _ in bq],
209 [b for _, b in bq],
210 z,
211 eps,
212 order,
213 )
214 if result is not None: 214 ↛ 215line 214 didn't jump to line 215 because the condition on line 214 was never true
215 return result
216 raise UnsupportedExpressionError("Unsupported noninteger parameter pattern or shifts")
217 upper, lower = tuple(h.ap), h.bq[0]
218 if s.S.One in upper:
219 other = upper[1] if upper[0] == 1 else upper[0]
220 b, c = _linear(other, eps, s.S.Zero), _linear(lower, eps, s.S.One)
221 if b is not None and c is not None:
222 return _zero_base_twof1(b, c, z, eps, order)
223 b = _linear(other, eps, s.Rational(1, 2))
224 c = _linear(lower, eps, s.Rational(3, 2))
225 if b is not None and b == c:
226 return _half_telescoping(b, z, eps, order)
227 if lower == s.Rational(1, 2):
228 for first, second in (upper, upper[::-1]):
229 a = _linear(first, eps, s.S.Zero)
230 b = _linear(second, eps, s.Rational(1, 2))
231 if a is not None and a == b:
232 return _half_even(a, z, eps, order)
233 from .twof1_shift import expand_shifted_twof1
235 shifted = expand_shifted_twof1(h, eps, order)
236 if shifted is not None:
237 return shifted
238 from .half_system import canonical_half_series
239 from .matched_half import half_parameters
241 parsed = half_parameters(h, eps)
242 if parsed is not None:
243 ap, bq = parsed
244 canonical = canonical_half_series(
245 [a for a, _ in ap],
246 [a for _, a in ap],
247 [b for b, _ in bq],
248 [b for _, b in bq],
249 z,
250 eps,
251 order,
252 )
253 if canonical is not None:
254 return reduce_known_hpl(canonical)
255 raise UnsupportedExpressionError(
256 "Native port does not yet support this parameter family; no external fallback was used"
257 )