Coverage for src/hypra/hpl/endpoints.py: 91.72%
111 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"""Convergent real HPL endpoints at one and exact multiple-zeta identities.
3The endpoint is the limit from 0<z<1. No divergent value is assigned. Trailing
4zeros are removed by the shuffle identity with H_0(z)=log(z); its product with
5any {0,1} HPL tends to zero at z=1 because HPL endpoint growth is at most a
6power of log(1-z). Thus the reduction preserves the complete finite limit.
8For admissible words (begin 0, end 1), each run 0^(s-1)1 gives an MZV index s.
9MZV duality follows from t -> 1-t and reversing the integration simplex.
10Weight-four reductions follow by comparing
11 zeta(2)^2 = 2 zeta(2,2) + zeta(4) (strict stuffle)
12and
13 zeta(2)^2 = 2 zeta(2,2) + 4 zeta(3,1) (HPL shuffle),
14with zeta(2)^2 = (5/2) zeta(4). These are identities, not fitted tables.
15"""
17from __future__ import annotations
19from functools import lru_cache
20from typing import cast
22import sympy as s
24from .._typing import ExactScalar, IntegerWord, as_integer_tuple
25from ..errors import UnsupportedExpressionError
26from ..symbolic import MZV
27from .algebra import shuffle_words
29_MAX_ENDPOINT_WEIGHT = 24
32def _word(word):
33 if not isinstance(word, (tuple, list, s.Tuple)):
34 raise TypeError("Endpoint input must be an HPL word, not an arbitrary expression")
35 if any(
36 isinstance(letter, bool) or not isinstance(letter, (int, s.Integer)) or letter not in (0, 1)
37 for letter in word
38 ):
39 raise UnsupportedExpressionError("This endpoint evaluator supports only the {0,1} alphabet")
40 if len(word) > _MAX_ENDPOINT_WEIGHT: 40 ↛ 41line 40 didn't jump to line 41 because the condition on line 40 was never true
41 raise UnsupportedExpressionError(f"Endpoint weight exceeds {_MAX_ENDPOINT_WEIGHT}")
42 return as_integer_tuple(word)
45def _indices_from_word(word):
46 if not word or word[0] != 0 or word[-1] != 1: 46 ↛ 47line 46 didn't jump to line 47 because the condition on line 46 was never true
47 raise UnsupportedExpressionError("An MZV endpoint word must begin with 0 and end with 1")
48 indices, zeros = [], 0
49 for letter in word:
50 if letter == 0:
51 zeros += 1
52 else:
53 indices.append(zeros + 1)
54 zeros = 0
55 return tuple(indices)
58def dual_indices(indices):
59 """Convergent MZV duality, derived by reversing and complementing its word."""
60 value = MZV(indices)
61 if not isinstance(value, MZV): 61 ↛ 62line 61 didn't jump to line 62 because the condition on line 61 was never true
62 return ()
63 indices = as_integer_tuple(value.indices)
64 if sum(indices) > _MAX_ENDPOINT_WEIGHT: 64 ↛ 65line 64 didn't jump to line 65 because the condition on line 64 was never true
65 raise UnsupportedExpressionError(f"MZV duality weight exceeds {_MAX_ENDPOINT_WEIGHT}")
66 word = tuple(letter for index in indices for letter in (*([0] * (index - 1)), 1))
67 return _indices_from_word(tuple(1 - letter for letter in reversed(word)))
70@lru_cache(maxsize=256)
71def _height_one(first, depth):
72 """Coefficient of x^(first-1)y^depth in 1-Gamma(1-x)Gamma(1-y)/Gamma(1-x-y).
74 The convergent height-one MZV generating identity follows from the Euler
75 beta integral. Sparse bivariate truncation is exact; every logarithm term
76 contains both x and y, so only min(first-1, depth) powers can contribute.
77 """
78 nx, ny = first - 1, depth
79 logarithm = {
80 (i, j): -s.binomial(i + j, i) * s.zeta(i + j) / (i + j)
81 for i in range(1, nx + 1)
82 for j in range(1, ny + 1)
83 }
84 power = {(0, 0): s.S.One}
85 coefficient = s.S.Zero
86 for k in range(1, min(nx, ny) + 1):
87 product = {}
88 for (i, j), left in power.items():
89 for (a, b), right in logarithm.items():
90 if i + a <= nx and j + b <= ny:
91 key = (i + a, j + b)
92 product[key] = product.get(key, s.S.Zero) + left * right
93 power = product
94 coefficient -= power.get((nx, ny), s.S.Zero) / s.factorial(k)
95 return s.expand(coefficient)
98@lru_cache(maxsize=64)
99def _repeated_index(index, depth):
100 """Newton identities for elementary sums in x_n=1/n**index."""
101 elementary: list[s.Expr] = [s.S.One]
102 for degree in range(1, depth + 1):
103 elementary.append(
104 s.expand(
105 sum(
106 (-1) ** (k - 1) * elementary[degree - k] * s.zeta(k * index)
107 for k in range(1, degree + 1)
108 )
109 / degree
110 )
111 )
112 return elementary[-1]
115def reduce_mzv(expression: ExactScalar) -> s.Expr:
116 """Apply proved duality, height-one and bounded double-shuffle identities.
118 Double-shuffle reduction spans all convergent MZVs through weight eight
119 using ordinary zeta products and zeta(5,3). Height-one beta-generating
120 reductions extend through weight twelve. Repeated indices and Euler's
121 zeta(n,1), Zagier's (3,1)^n and the prefixed (2,(1,3)^n) identities extend
122 through the endpoint weight limit of 24.
123 Higher MZVs remain exact objects.
124 Duality selects the
125 representative with smaller depth (then lexicographic order); it does not
126 make unproved basis-completeness or transcendence claims.
128 Args:
129 expression: Exact symbolic expression containing multiple zeta values.
130 """
131 if type(expression) is int: 131 ↛ 132line 131 didn't jump to line 132 because the condition on line 131 was never true
132 expression = s.Integer(expression)
133 if not isinstance(expression, s.Basic): 133 ↛ 134line 133 didn't jump to line 134 because the condition on line 133 was never true
134 raise TypeError("expression must be a SymPy expression")
136 def reduce_one(atom):
137 indices = as_integer_tuple(atom.indices)
138 if len(indices) == 1:
139 return s.zeta(indices[0])
140 if sum(indices) > _MAX_ENDPOINT_WEIGHT:
141 return atom
142 dual = dual_indices(indices)
143 indices = min((indices, dual), key=lambda word: (len(word), word))
144 if len(indices) == 1:
145 return s.zeta(indices[0])
146 # Borwein--Bradley--Broadhurst--Lisonek, arXiv:math/9910045,
147 # Example 2.2 / Section 11 proves the Zagier family by a product of
148 # Gauss hypergeometric generating functions.
149 if len(indices) % 2 == 0 and indices == (3, 1) * (len(indices) // 2):
150 repeats = len(indices) // 2
151 return 2 * s.pi ** (4 * repeats) / s.factorial(4 * repeats + 2)
152 # Example 2.3 (also 6.4) of the same paper records the proved
153 # Bowman--Bradley prefixed family. Repeated-four factors below are
154 # generated by Newton identities, independently of any source table.
155 if len(indices) % 2 and indices == (2,) + (1, 3) * (len(indices) // 2):
156 repeats = len(indices) // 2
157 return s.expand(
158 s.Rational(1, 4) ** repeats
159 * sum(
160 (-1) ** k
161 * _repeated_index(4, repeats - k)
162 * (
163 (4 * k + 1) * s.zeta(4 * k + 2)
164 - 4
165 * sum(
166 s.zeta(4 * j - 1) * s.zeta(4 * k - 4 * j + 3) for j in range(1, k + 1)
167 )
168 )
169 for k in range(repeats + 1)
170 )
171 )
172 if indices and len(set(indices)) == 1:
173 return _repeated_index(indices[0], len(indices))
174 if len(indices) == 2 and indices[1] == 1:
175 first = indices[0]
176 return s.expand(
177 first * s.zeta(first + 1) / 2
178 - sum(s.zeta(first - k) * s.zeta(k + 1) for k in range(1, first - 1)) / 2
179 )
180 if indices and sum(indices) <= 12 and all(index == 1 for index in indices[1:]):
181 return _height_one(indices[0], len(indices))
182 if sum(indices) <= 8:
183 from .mzv_reduction import mzv_weight_reductions
185 return mzv_weight_reductions(sum(indices))[indices]
186 return MZV(indices)
188 return cast(s.Expr, expression.replace(lambda node: isinstance(node, MZV), reduce_one))
191@lru_cache(maxsize=2048)
192def _endpoint(word):
193 if not word:
194 return s.S.One
195 if all(letter == 0 for letter in word):
196 return s.S.Zero
197 # For a leading 1, any further 1 leaves a nonzero (or divergent) tail at
198 # one and produces a logarithmic divergence. A pure-zero tail instead
199 # vanishes and is integrable: H(1,0;1)=-zeta(2), for example.
200 if word[0] == 1 and (len(word) == 1 or 1 in word[1:]):
201 raise UnsupportedExpressionError(
202 "Divergent HPL endpoint at one; no regularized value is assigned"
203 )
204 if word[-1] == 1:
205 return MZV(_indices_from_word(word))
206 prefix = word[:-1]
207 shuffled = shuffle_words(prefix, (0,))
208 multiplicity = shuffled.pop(word)
209 result = -sum((count * _endpoint(other) for other, count in shuffled.items()), s.S.Zero)
210 return s.expand(result / multiplicity)
213def hpl_at_one(word: IntegerWord, *, reduced: bool = True) -> s.Expr:
214 """Evaluate a convergent {0,1} HPL word at z=1 from the real interval.
216 The input is the discrete word, e.g. ``hpl_at_one((0,1)) == pi**2/6``.
217 Use reduced=False to retain MZV objects without applying proven reductions.
218 Empty words give one, nonempty all-zero words give zero. Other alphabets
219 and genuinely divergent endpoints raise UnsupportedExpressionError.
220 This is not a limit engine for arbitrary rational-HPL combinations.
222 Args:
223 word: Expanded binary word with a convergent endpoint.
224 reduced: Apply supported exact MZV reductions.
225 """
226 if type(reduced) is not bool: 226 ↛ 227line 226 didn't jump to line 227 because the condition on line 226 was never true
227 raise TypeError("reduced must be a Boolean")
228 result = _endpoint(_word(word))
229 return reduce_mzv(result) if reduced else result