Coverage for src/hypra/hpl/known_functions.py: 87.91%

63 statements  

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

1"""Known-function HPL reductions derived from shuffle and path composition. 

2 

3Nielsen functions retain their exact HPL convention. No upstream expression 

4table is copied; weight-four binary words reduce through their three Lyndon 

5generators, all of which have Nielsen form. 

6""" 

7 

8from functools import lru_cache 

9from typing import cast 

10 

11import sympy as s 

12 

13from .._typing import Interval, as_integer_tuple 

14from ..errors import UnsupportedExpressionError 

15from ..nielsen import NielsenPolylog 

16from ..symbolic import HPL, reduce_known_hpl 

17from .classical import classical_hpl 

18from .notation import HPLPM, hpl_pm 

19from .reduction import lyndon_reduce 

20 

21 

22def _nielsen_word(word, argument): 

23 zeros = 0 

24 while zeros < len(word) and word[zeros] == 0: 

25 zeros += 1 

26 tail = word[zeros:] 

27 if zeros and tail and len(set(tail)) == 1 and tail[0] in (-1, 1): 

28 return tail[0] ** len(tail) * NielsenPolylog(zeros, len(tail), tail[0] * argument) 

29 return HPL(word, argument) 

30 

31 

32@lru_cache(maxsize=128) 

33def _binary_known(word, argument): 

34 """Shuffle-reduce a {0,1} word of weight at most four.""" 

35 if not word: 

36 return s.S.One 

37 reduced = lyndon_reduce(HPL(word, argument)) 

38 result = reduced.xreplace( 

39 {atom: _nielsen_word(tuple(atom.word), argument) for atom in reduced.atoms(HPL)} 

40 ) 

41 result = reduce_known_hpl(cast(s.Expr, result)) 

42 return result.xreplace( 

43 { 

44 atom: s.polylog(atom.n + 1, atom.argument) 

45 for atom in result.atoms(NielsenPolylog) 

46 if atom.p == 1 

47 } 

48 ) 

49 

50 

51def _negative_binary_known(word, argument): 

52 # Relabel the shuffle alphabet only. The zero letter remains log(argument), 

53 # avoiding an unjustified log(-argument) substitution across its cut. 

54 positive_word = tuple(-letter for letter in word) 

55 reduced = lyndon_reduce(HPL(positive_word, argument)) 

56 negative = reduced.xreplace( 

57 { 

58 atom: HPL(tuple(-letter for letter in as_integer_tuple(atom.word)), argument) 

59 for atom in reduced.atoms(HPL) 

60 } 

61 ) 

62 result = negative.xreplace( 

63 {atom: _nielsen_word(tuple(atom.word), argument) for atom in negative.atoms(HPL)} 

64 ) 

65 return reduce_known_hpl(cast(s.Expr, result)) 

66 

67 

68def _opposite_known(word, argument): 

69 # u=(1+z)/2 sends the {-1,+1} kernels to the {0,1} kernels, but moves the 

70 # basepoint from zero to 1/2. Chen composition with the inverse first path 

71 # gives sum H_prefix(u)*(-1)^len(suffix)*H_reverse(suffix)(1/2). 

72 mapped = tuple(0 if letter == -1 else 1 for letter in word) 

73 return s.Add( 

74 *( 

75 (-1) ** (len(word) - split) 

76 * _binary_known(mapped[:split], (1 + argument) / 2) 

77 * _binary_known(mapped[split:][::-1], s.S.Half) 

78 for split in range(len(word) + 1) 

79 ) 

80 ) 

81 

82 

83def reduce_hpl_to_known_functions(expression: s.Expr, z: s.Symbol, *, domain: Interval) -> s.Expr: 

84 """Reduce the HPL 2 known-function families on the real interval 0<z<1. 

85 

86 Covers all ternary words through weight three, every two-letter word 

87 through weight four, repeated letters, and signed Nielsen words 0^n(±1)^p. 

88 Higher mixed words remain HPLs. Outputs use logs, classical polylogarithms 

89 and NielsenPolylog with explicit principal continuations. Plus/minus HPLs 

90 are first expanded into their defining ordinary-letter linear combination. 

91 

92 Args: 

93 expression: Exact expression containing ordinary or plus/minus HPLs. 

94 z: Common symbolic argument of the HPLs being reduced. 

95 domain: Explicit declaration `"0<z<1"`. 

96 """ 

97 if not isinstance(expression, s.Expr) or not isinstance(z, s.Symbol): 97 ↛ 98line 97 didn't jump to line 98 because the condition on line 97 was never true

98 raise TypeError("Use a SymPy expression and symbolic argument") 

99 if domain != "0<z<1": 

100 raise UnsupportedExpressionError("Explicit domain='0<z<1' is required") 

101 if expression.has(s.Float, s.nan, s.zoo, s.oo, -s.oo): 101 ↛ 102line 101 didn't jump to line 102 because the condition on line 101 was never true

102 raise UnsupportedExpressionError("Known-function reduction requires exact finite input") 

103 ordinary = expression.xreplace( 

104 {atom: hpl_pm(atom.word, atom.argument) for atom in expression.atoms(HPLPM)} 

105 ) 

106 result = classical_hpl(cast(s.Expr, ordinary), z, domain=domain) 

107 changes = {} 

108 for atom in result.atoms(HPL): 

109 if atom.argument != z: 109 ↛ 110line 109 didn't jump to line 110 because the condition on line 109 was never true

110 if atom.argument.has(z): 

111 raise UnsupportedExpressionError("First transform HPLs to the common argument") 

112 continue 

113 word = as_integer_tuple(atom.word) 

114 direct = _nielsen_word(word, z) 

115 if direct != atom: 

116 changes[atom] = direct 

117 elif len(word) <= 4: 117 ↛ 108line 117 didn't jump to line 108 because the condition on line 117 was always true

118 letters = set(word) 

119 if letters <= {0, 1}: 

120 changes[atom] = _binary_known(word, z) 

121 elif letters <= {0, -1}: 

122 changes[atom] = _negative_binary_known(word, z) 

123 elif letters <= {-1, 1}: 

124 changes[atom] = _opposite_known(word, z) 

125 return cast(s.Expr, result.xreplace(changes))