Coverage for src/hypra/hpl/series_one.py: 93.55%

40 statements  

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

1"""Exact real near-one power/log expansions from endpoint constants and kernels.""" 

2 

3import sympy as s 

4 

5from .._typing import IntegerWord 

6from ..errors import UnsupportedExpressionError 

7from .numeric import _word 

8from .regularization import hpl_endpoint_polynomial 

9 

10 

11def hpl_series_at_one(word: IntegerWord, t: s.Symbol, *, through: int = 3) -> s.Expr: 

12 """Expand H_word(1-t) for real t->0+, including t**through. 

13 

14 Uses T=-log(t) with exact polynomial coefficients. The explicit remainder 

15 is O(t**(through+1)*log(t)**weight), NOT an unjustified plain power Order. 

16 The input t must be an ordinary commutative Symbol; its positive approach 

17 is part of this API's domain contract. No complex branch is inferred. 

18 Weight <=8, through <=24, and a coefficient-operation budget are enforced. 

19 

20 Args: 

21 word: Expanded ternary word of weight at most eight. 

22 t: Positive distance from the endpoint. 

23 through: Inclusive power of t, from zero to 24. 

24 """ 

25 if not isinstance(t, s.Symbol) or t.is_commutative is not True: 

26 raise TypeError("t must be an ordinary commutative SymPy Symbol") 

27 if type(through) is not int or not 0 <= through <= 24: 

28 raise UnsupportedExpressionError("Near-one order must be an integer from zero through 24") 

29 word = _word(word) 

30 if len(word) > 8: 

31 raise UnsupportedExpressionError("Near-one expansion supports weight at most eight") 

32 if not word: 

33 return s.S.One 

34 # A private ordinary Symbol is required by the endpoint API; its name is 

35 # distinct from t. All endpoint coefficients are parameter-free constants. 

36 T = s.Symbol("_near_one_log" if t.name != "_near_one_log" else "_near_one_log_") 

37 tail = [s.S.One] + [s.S.Zero] * through 

38 for offset in range(len(word) - 1, -1, -1): 

39 head = word[offset] 

40 coefficients = [hpl_endpoint_polynomial(word[offset:], T)] 

41 for n in range(1, through + 1): 

42 if head == 1: 

43 rhs = -tail[n] 

44 elif head == 0: 

45 rhs = -sum(tail[:n], s.S.Zero) 

46 else: 

47 rhs = -sum((tail[m] / s.Integer(2) ** (n - m) for m in range(n)), s.S.Zero) 

48 # Invert n-D_T on the finite polynomial space, without integration 

49 # constants or divisions by symbolic parameters. 

50 value, derivative, power = s.S.Zero, s.expand(rhs), 1 

51 while derivative != 0: 

52 value += derivative / s.Integer(n) ** power 

53 derivative = s.diff(derivative, T) 

54 power += 1 

55 if power > len(word) + 2: 55 ↛ 56line 55 didn't jump to line 56 because the condition on line 55 was never true

56 raise UnsupportedExpressionError( 

57 "Near-one coefficient is not a finite log polynomial" 

58 ) 

59 value = s.expand(value) 

60 if s.count_ops(value) > 12000: 60 ↛ 61line 60 didn't jump to line 61 because the condition on line 60 was never true

61 raise UnsupportedExpressionError("Near-one coefficient operation budget exceeded") 

62 coefficients.append(value) 

63 tail = coefficients 

64 expression = s.Add(*(coefficient * t**n for n, coefficient in enumerate(tail))) 

65 return s.expand(expression.subs(T, -s.log(t))) + s.Order( 

66 t ** (through + 1) * s.log(t) ** len(word), (t, 0) 

67 )