Coverage for src/hypra/hpl/multiple_polylog.py: 78.38%

54 statements  

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

1"""Multiple polylogarithm constants defined by absolutely convergent sums.""" 

2 

3from collections.abc import Iterable 

4from functools import lru_cache 

5from typing import cast 

6 

7import mpmath as mp 

8import sympy as s 

9from sympy.core.evalf import prec_to_dps 

10 

11 

12@lru_cache(maxsize=512) 

13def _evaluate(indices, arguments, digits): 

14 ctx = mp.mp.clone() 

15 ctx.dps = digits + 16 

16 values = [ 

17 ctx.mpc(*(str(part.evalf(ctx.dps)) for part in arg.as_real_imag())) for arg in arguments 

18 ] 

19 # Every cumulative argument product has modulus <= 1/2. For outer 

20 # index n the summands total at most n**(depth-1) * 2**(-n). 

21 depth = len(indices) 

22 cutoff = max(4 * depth, 4 * ctx.dps) 

23 while 4 * cutoff ** (depth - 1) * ctx.power(2, -cutoff) > ctx.power(10, -digits - 8): 23 ↛ 24line 23 didn't jump to line 24 because the condition on line 23 was never true

24 cutoff *= 2 

25 

26 sums = [ctx.zero] * depth + [ctx.one] 

27 powers = [ctx.one] * depth 

28 for n in range(1, cutoff + 1): 

29 # Ascending j uses the previous cutoff for the inner strict sum. 

30 for j, (index, value) in enumerate(zip(indices, values, strict=True)): 

31 powers[j] *= value 

32 sums[j] += powers[j] * sums[j + 1] / n**index 

33 return ctx.nstr(sums[0].real, digits), ctx.nstr(sums[0].imag, digits) 

34 

35 

36class MultiplePolylog(s.Function): 

37 r"""Strict multiple polylogarithm Li_(indices)(arguments). 

38 

39 The arguments here are exact Gaussian rationals with each cumulative 

40 product of modulus at most 1/2. Rewrite as Sum for its defining series. 

41 """ 

42 

43 nargs = 2 

44 

45 @classmethod 

46 def eval(cls, indices, arguments): # pyright: ignore[reportIncompatibleMethodOverride] 

47 if not isinstance(indices, s.Tuple) or not isinstance(arguments, s.Tuple): 47 ↛ 48line 47 didn't jump to line 48 because the condition on line 47 was never true

48 raise TypeError("Use SymPy tuples for multiple polylogarithm indices and arguments") 

49 if ( 49 ↛ 54line 49 didn't jump to line 54 because the condition on line 49 was never true

50 not indices 

51 or len(indices) != len(arguments) 

52 or any(not isinstance(index, s.Integer) or index < 1 for index in indices) 

53 ): 

54 raise ValueError("Use one positive integer index per argument") 

55 cumulative = s.S.One 

56 for arg in arguments: 

57 if not isinstance(arg, s.Expr): 57 ↛ 58line 57 didn't jump to line 58 because the condition on line 57 was never true

58 raise TypeError("Multiple polylogarithm arguments must be Gaussian rationals") 

59 re, im = arg.as_real_imag() 

60 if not isinstance(re, s.Rational) or not isinstance(im, s.Rational): 60 ↛ 61line 60 didn't jump to line 61 because the condition on line 60 was never true

61 raise TypeError("Multiple polylogarithm arguments must be Gaussian rationals") 

62 cumulative = s.expand(cumulative * arg) 

63 re, im = cumulative.as_real_imag() 

64 if re**2 + im**2 > s.Rational(1, 4): 64 ↛ 65line 64 didn't jump to line 65 because the condition on line 64 was never true

65 raise ValueError("Cumulative argument products must have modulus at most 1/2") 

66 if len(indices) == 1: 

67 return s.polylog(indices[0], arguments[0]) 

68 

69 def _eval_rewrite_as_Sum(self, indices, arguments, **hints): 

70 variables = [s.Dummy("n", integer=True, positive=True) for _ in indices] 

71 terms = s.prod( 

72 arg**n / n**power for n, power, arg in zip(variables, indices, arguments, strict=True) 

73 ) 

74 limits = [(variables[j], 1, variables[j - 1] - 1) for j in range(len(indices) - 1, 0, -1)] 

75 return s.Sum(terms, *limits, (variables[0], 1, s.oo)) 

76 

77 def _eval_evalf(self, prec): 

78 digits = prec_to_dps(prec) + 8 

79 indices = cast(Iterable[s.Integer], self.args[0]) 

80 arguments = cast(Iterable[s.Expr], self.args[1]) 

81 real, imag = _evaluate(tuple(map(int, indices)), tuple(arguments), digits) 

82 return s.Float(real, precision=prec) + s.I * s.Float(imag, precision=prec)