Coverage for src/hypra/hpl/limits.py: 85.42%
66 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"""Combined finite z->1- limits using exact HPL reflection or power/log series.
3This is not termwise endpoint evaluation: rational/logarithmic singularities
4are retained until all coefficients at the reflected origin have combined.
5"""
7import sympy as s
9from ..errors import UnsupportedExpressionError
10from ..symbolic import HPL
11from .algebra import _as_hpl, _expansion_size, _origin_constant
12from .transforms import reflect_hpl
15def hpl_limit_at_one(expression: s.Expr, z: s.Symbol) -> s.Expr:
16 """Prove the combined finite real endpoint, or raise on unsupported data.
18 Coefficients must be rational in z, with ordinary HPLs/logs/polylogs at z.
19 Reflection supports binary words of weight<=8. Ternary words use exact
20 near-one series (individual weight<=8, total polynomial weight<=12,
21 rational pole order<=16, numeric rational-function coefficients). No complex branch choice or
22 divergent regularization is inferred. A failure is not an infinite value.
24 Args:
25 expression: Exact rational-HPL expression, with singular terms combined.
26 z: Argument approaching one from below.
28 Raises:
29 UnsupportedExpressionError: The limit is outside the supported scope or
30 finiteness could not be established.
31 """
32 if not isinstance(expression, s.Expr) or not isinstance(z, s.Symbol): 32 ↛ 33line 32 didn't jump to line 33 because the condition on line 32 was never true
33 raise TypeError("Expected a SymPy expression and symbolic argument")
34 if expression.has(s.Float, s.nan, s.zoo, s.oo, -s.oo): 34 ↛ 35line 34 didn't jump to line 35 because the condition on line 34 was never true
35 raise UnsupportedExpressionError("Endpoint expression must be exact and finite")
36 transformed = _as_hpl(expression, z)
37 if any(-1 in h.word for h in transformed.atoms(HPL)):
38 return _ternary_limit(transformed, z)
39 t = s.Dummy("endpoint_distance", positive=True)
40 replacements = {h: reflect_hpl(h.word, t, domain="0<z<1") for h in transformed.atoms(HPL)}
41 reflected = transformed.xreplace(replacements).subs(z, 1 - t)
42 return _origin_constant(reflected, t)
45def _ternary_limit(expression, z):
46 """Combine exact near-one Laurent/log coefficients before testing finiteness."""
47 from .series_one import hpl_series_at_one
49 if _expansion_size(expression) > 4096: 49 ↛ 50line 49 didn't jump to line 50 because the condition on line 49 was never true
50 raise UnsupportedExpressionError("Endpoint expression exceeds the expansion budget")
51 atoms = sorted(expression.atoms(HPL), key=s.default_sort_key)
52 generators = [s.Dummy("endpoint_hpl") for _ in atoms]
53 replaced = expression.xreplace(dict(zip(atoms, generators, strict=True)))
54 try:
55 polynomial = s.Poly(replaced, *generators)
56 except s.PolynomialError as exc:
57 raise UnsupportedExpressionError(
58 "Ternary endpoint limit requires polynomial HPL dependence"
59 ) from exc
60 t, T = s.Dummy("endpoint_distance", positive=True), s.Dummy("endpoint_log")
61 terms, pole = [], 0
62 for powers, coefficient in polynomial.terms():
63 if sum(p * len(h.word) for p, h in zip(powers, atoms, strict=True)) > 12: 63 ↛ 64line 63 didn't jump to line 64 because the condition on line 63 was never true
64 raise UnsupportedExpressionError("Endpoint expression exceeds total HPL weight twelve")
65 if coefficient.free_symbols - {z} or not coefficient.is_rational_function(z):
66 raise UnsupportedExpressionError(
67 "Ternary endpoint coefficients must be numeric rational functions of z"
68 )
69 coefficient = s.cancel(coefficient.subs(z, 1 - t))
70 numerator, denominator = coefficient.as_numer_denom()
71 try:
72 num, den = s.Poly(numerator, t), s.Poly(denominator, t)
73 except s.PolynomialError as exc:
74 raise UnsupportedExpressionError("Unresolved rational endpoint coefficient") from exc
75 if num.is_zero: 75 ↛ 76line 75 didn't jump to line 76 because the condition on line 75 was never true
76 continue
77 nmin, dmin = min(m[0] for m in num.monoms()), min(m[0] for m in den.monoms())
78 if den.nth(dmin).is_zero is not False: 78 ↛ 79line 78 didn't jump to line 79 because the condition on line 78 was never true
79 raise UnsupportedExpressionError("Could not prove the endpoint denominator nonzero")
80 pole = max(pole, dmin - nmin)
81 terms.append((powers, coefficient))
82 if pole > 16:
83 raise UnsupportedExpressionError("Endpoint rational pole order exceeds sixteen")
84 expansions = [
85 hpl_series_at_one(h.word, t, through=pole).removeO().subs(s.log(t), -T) for h in atoms
86 ]
87 result = s.S.Zero
88 for powers, coefficient in terms:
89 result += s.series(coefficient, t, 0, 1).removeO() * s.prod(
90 value**power for value, power in zip(expansions, powers, strict=True)
91 )
92 _expansion_size(result)
93 # Multiply by a sufficient t power to inspect an ordinary polynomial.
94 combined = s.Poly(s.expand(result * t**pole), t, T)
95 constant = s.S.Zero
96 for (degree, logarithms), coefficient in combined.terms():
97 if degree > pole:
98 continue
99 coefficient = s.simplify(coefficient)
100 if degree < pole or logarithms:
101 if coefficient != 0:
102 raise UnsupportedExpressionError("Endpoint diverges or cancellation is not proved")
103 else:
104 constant += coefficient
105 return s.simplify(constant)