Coverage for src/hypra/hpl/taylor.py: 86.05%

29 statements  

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

1"""Exact Taylor coefficients at nonsingular explicitly branched base points.""" 

2 

3from typing import Literal 

4 

5import sympy as s 

6 

7from .._typing import ExactScalar, IntegerWord 

8from ..errors import UnsupportedExpressionError 

9from ..symbolic import HPL 

10from .algebra import _word 

11 

12 

13def hpl_taylor( 

14 word: IntegerWord, 

15 center: ExactScalar, 

16 variable: s.Symbol, 

17 *, 

18 through: int = 3, 

19 domain: Literal["real", "principal-cut-plane"] = "real", 

20) -> s.Expr: 

21 """Expand HPL(word, variable) about an exact nonsingular center. 

22 

23 Coefficients retain exact HPL constants at center. The kernel ODE yields a 

24 finite convolution for every coefficient; no numerical differentiation is 

25 used. The default center is real with 0<center<1; exact algebraic and 

26 transcendental numbers are permitted. Explicit principal-cut-plane mode 

27 also permits exact nonreal centers. The Taylor series continues the local 

28 germ at center and converges inside the disk excluding 0, +/-1; it does 

29 not reset the branch when that disk crosses an artificial logarithm cut. 

30 Includes degree ``through`` and an Order remainder. Weight <=24 and 

31 degree <=64 are supported. 

32 

33 Args: 

34 word: Expanded ternary word of weight at most 24. 

35 center: Exact finite numerical expansion point in the chosen domain. 

36 variable: Symbolic argument of the Taylor series. 

37 through: Inclusive Taylor degree, from zero to 64. 

38 domain: Real interior by default, or explicit principal cut plane. 

39 """ 

40 word = _word(word) 

41 if not isinstance(variable, s.Symbol) or variable.is_commutative is not True: 41 ↛ 42line 41 didn't jump to line 42 because the condition on line 41 was never true

42 raise TypeError("variable must be a commutative SymPy Symbol") 

43 if type(center) is int: 

44 center = s.Integer(center) 

45 if domain not in ("real", "principal-cut-plane"): 45 ↛ 46line 45 didn't jump to line 46 because the condition on line 45 was never true

46 raise UnsupportedExpressionError("Taylor domain must be real or principal-cut-plane") 

47 if ( 

48 not isinstance(center, s.Expr) 

49 or center.free_symbols 

50 or center.has(s.Float) 

51 or center.is_finite is not True 

52 ): 

53 raise UnsupportedExpressionError("Taylor center must be an exact finite number") 

54 interior = ( 

55 center.is_real is True and center.is_positive is True and (1 - center).is_positive is True 

56 ) 

57 if not interior and not (domain == "principal-cut-plane" and center.is_real is False): 

58 raise UnsupportedExpressionError( 

59 "Taylor center must be interior real or explicitly off-cut" 

60 ) 

61 if type(through) is not int or not 0 <= through <= 64: 61 ↛ 62line 61 didn't jump to line 62 because the condition on line 61 was never true

62 raise UnsupportedExpressionError("Taylor degree must be an integer from zero through 64") 

63 coefficients = [s.S.One] + [s.S.Zero] * through 

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

65 letter = word[offset] 

66 kernel = [ 

67 (1 if letter == 1 else (-1) ** j) 

68 / ((1 - center if letter == 1 else center if letter == 0 else 1 + center) ** (j + 1)) 

69 for j in range(through) 

70 ] 

71 coefficients = [HPL(word[offset:], center)] + [ 

72 s.expand(sum(kernel[j] * coefficients[n - 1 - j] for j in range(n)) / n) 

73 for n in range(1, through + 1) 

74 ] 

75 delta = variable - center 

76 polynomial = sum(coefficient * delta**n for n, coefficient in enumerate(coefficients)) 

77 return polynomial + s.Order(delta ** (through + 1), (variable, center))