Skip to content

Increase working precision in polylog_general() for negative s#898

Merged
skirpichev merged 7 commits into
mpmath:masterfrom
Riccardo231:fix_polylog
Jan 22, 2025
Merged

Increase working precision in polylog_general() for negative s#898
skirpichev merged 7 commits into
mpmath:masterfrom
Riccardo231:fix_polylog

Conversation

@Riccardo231

@Riccardo231 Riccardo231 commented Jan 18, 2025

Copy link
Copy Markdown
Contributor

Closes #634

Tries to address error in polylog precision when abs(z) > 1 and s is close to an integer
@Riccardo231 Riccardo231 marked this pull request as draft January 18, 2025 15:39
@Riccardo231

Copy link
Copy Markdown
Contributor Author

I still have to fix the issues rising from these tests, please do not check. Thanks

@skirpichev

Copy link
Copy Markdown
Collaborator

I'm afraid, but you need more arguments than "empirical testing".

No additional tests have been implemented; please advise if they are required.

At least you could add tests from the issue.

@fredrik-johansson

fredrik-johansson commented Jan 19, 2025

Copy link
Copy Markdown
Collaborator

Probably the precision in polylog should be increased by max(0, -nint_distance(s)[1]) in some of the algorithms.

Edit: specifically, this is required in polylog_general where one cancels out two poles of magnitude $\approx 1/(s-nint(s))$.

To make polylog even more reliable (but potentially much slower), sum_accurately could be used.

@Riccardo231

Copy link
Copy Markdown
Contributor Author

Tomorrow I'll work on it. Thanks for the suggestion

@Riccardo231

Copy link
Copy Markdown
Contributor Author

Hello @fredrik-johansson, I tried this approach but it doesn't seem to work:

return ctx.gamma(1-s)*(-u)**(s-1) + v + max(0, -(round(s)-s))

I think I am missing something because, of course, the output does not differ of that value. Can you help me? Thank you

@skirpichev

Copy link
Copy Markdown
Collaborator

@ricor07, I guess you changed last line of the polylog_general(). Could you explain what means your code?

Increasing precision seems to be working for me:

diff --git a/mpmath/functions/zeta.py b/mpmath/functions/zeta.py
index 3e3b542..8944666 100644
--- a/mpmath/functions/zeta.py
+++ b/mpmath/functions/zeta.py
@@ -456,6 +456,8 @@ def polylog_general(ctx, s, z):
         return ctx.gamma(v)*(j**v*ctx.zeta(v,0.5+y) + j**-v*ctx.zeta(v,0.5-y))/(2*ctx.pi)**v
     t = 1
     k = 0
+    prec = ctx.prec
+    ctx.prec += max(0, -ctx.nint_distance(s)[1])
     while 1:
         term = ctx.zeta(s-k) * t
         if not abs(term) >= ctx.eps:
@@ -464,7 +466,9 @@ def polylog_general(ctx, s, z):
         k += 1
         t *= u
         t /= k
-    return ctx.gamma(1-s)*(-u)**(s-1) + v
+    r = ctx.gamma(1-s)*(-u)**(s-1) + v
+    ctx.prec = prec
+    return r
 
 @defun_wrapped
 def polylog(ctx, s, z):

On OP example:

>>> import mpmath as mp
... def check(s, z):
...     pl1 = mp.polylog(s, z)
...     print(pl1.real)
...     with mp.workdps(32):
...         pl2 = mp.polylog(s, z)
...         print(pl2.real)
...         
>>> check(1+1e-15, -2)  # that one was bad
-1.09861228866811
-1.0986122886681101262592914356806
>>> check(1, -2 + 1e-8j)
-1.09861228866811
-1.0986122886681096969508007924781
>>> check(1+1e-15, -0.5 + 1e-8j)
-0.405465108108164
-0.40546510810816446598993214576279

@Riccardo231

Copy link
Copy Markdown
Contributor Author

Hello, thanks to your suggestion i could fix the issue. I added a control for inf and nan values because your implementation couldn't fit test_issue_505 in test_function2. The tests fit. Let me know what you think

@skirpichev skirpichev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also should add tests.

Comment thread mpmath/functions/zeta.py Outdated
Comment thread mpmath/functions/zeta.py Outdated
Comment thread mpmath/functions/zeta.py Outdated
Comment thread mpmath/functions/zeta.py Outdated
@skirpichev skirpichev self-requested a review January 22, 2025 10:59
@skirpichev skirpichev changed the title Address #634 Increase working precision in polylog_general() for negative s Jan 22, 2025
@skirpichev skirpichev marked this pull request as ready for review January 22, 2025 11:01
@skirpichev skirpichev merged commit fa51d4c into mpmath:master Jan 22, 2025
@skirpichev

Copy link
Copy Markdown
Collaborator

Thanks.

@Riccardo231 Riccardo231 deleted the fix_polylog branch January 22, 2025 13:53
@skirpichev skirpichev added this to the 1.4 milestone May 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Polylog inaccurate for s very close to an integer and z outside of the unit circle

3 participants