Coverage for src/hypra/hpl/minimal.py: 18.39%

61 statements  

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

1"""Minimal HPL bases generated from shuffle relations.""" 

2 

3from fractions import Fraction 

4from functools import lru_cache 

5from itertools import product 

6 

7import sympy as s 

8 

9from ..errors import UnsupportedExpressionError 

10from ..symbolic import HPL 

11from .algebra import _word, shuffle_words 

12from .notation import HPLPM, _pm_indices 

13 

14_ALPHABET = (-1, 0, 1) 

15_ARGUMENT = s.Dummy("argument") 

16 

17 

18def _pivot_key(word): 

19 # Extract endpoint logarithms first. In the plus/minus alphabet, the same 

20 # ordering eliminates leading plus and trailing zero. 

21 return word[0] != 1 and word[-1] != 0, word 

22 

23 

24@lru_cache(maxsize=5) 

25def _shuffle_pivots(weight): 

26 """Solve each homogeneous weight over products of lower-weight words.""" 

27 pivots = {} 

28 for split in range(1, weight // 2 + 1): 

29 left_words = product(_ALPHABET, repeat=split) 

30 right_words = product(_ALPHABET, repeat=weight - split) 

31 for left, right in product(left_words, right_words): 

32 row = {word: Fraction(count) for word, count in shuffle_words(left, right).items()} 

33 rhs = HPL(left, _ARGUMENT) * HPL(right, _ARGUMENT) 

34 while row: 

35 pivot = min(row, key=_pivot_key) 

36 scale = row.pop(pivot) 

37 if pivot not in pivots: 

38 pivots[pivot] = ( 

39 {word: count / scale for word, count in row.items()}, 

40 rhs / s.Rational(scale), 

41 ) 

42 break 

43 terms, replacement = pivots[pivot] 

44 rhs -= s.Rational(scale) * replacement 

45 for word, count in terms.items(): 

46 remaining = row.get(word, 0) - scale * count 

47 if remaining: 

48 row[word] = remaining 

49 else: 

50 row.pop(word, None) 

51 return pivots 

52 

53 

54@lru_cache(maxsize=1092) 

55def _minimal_word(word): 

56 if not word: 

57 return s.S.One 

58 if len(word) == 1: 

59 return HPL(word, _ARGUMENT) 

60 pivots = _shuffle_pivots(len(word)) 

61 if word not in pivots: 

62 return HPL(word, _ARGUMENT) 

63 terms, rhs = pivots[word] 

64 lower = rhs.xreplace({atom: _minimal_word(_word(atom.word)) for atom in rhs.atoms(HPL)}) 

65 return s.expand( 

66 lower - sum(s.Rational(count) * _minimal_word(other) for other, count in terms.items()) 

67 ) 

68 

69 

70def reduce_hpl_to_minimal_set(expression: s.Expr) -> s.Expr: 

71 """Reduce ordinary and plus/minus HPLs to the HPL 2.0 shuffle bases. 

72 

73 Supports weights zero through six. Use HPLPM to retain plus/minus letters 

74 in the returned basis. The two alphabets are reduced independently. 

75 """ 

76 if not isinstance(expression, s.Expr): 

77 raise TypeError("expression must be a SymPy expression") 

78 if expression.has(s.Float, s.nan, s.zoo, s.oo, -s.oo): 

79 raise UnsupportedExpressionError("Minimal-set reduction requires exact finite input") 

80 replacements = {} 

81 for atom in expression.atoms(HPL, HPLPM): 

82 if len(atom.word) > 6: 

83 raise UnsupportedExpressionError("Minimal-set reduction supports weight at most six") 

84 plus_minus = isinstance(atom, HPLPM) 

85 template = _minimal_word(_pm_indices(atom.word) if plus_minus else _word(atom.word)) 

86 constructor = HPLPM.from_indices if plus_minus else HPL 

87 replacements[atom] = template.xreplace( 

88 { 

89 component: constructor(_word(component.word), atom.argument) 

90 for component in template.atoms(HPL) 

91 } 

92 ) 

93 return s.expand(expression.xreplace(replacements))