Search code examples
wolfram-mathematica

Why is a finite sum calculated so long?


I'm trying to compute the next sum: enter image description here

It is calculated instantly. So I raise the number of points to 24^3 and it still works fast: enter image description here

But when the number of points is 25^3 it's almost impossible to await the result! Moreover, there is a warning: enter image description here

Why is it so time-consuming to calculate a finite sum? How can I get a precise answer?


Solution

  • Try

    max=24;
    Timing[N[
      Sum[1/(E^((i^2+j^2+k^2-3)/500)-1),{i,2,max},{j,1,max},{k,1,max}]+
      Sum[1/(E^((i^2+j^2+k^2-3)/500)-1),{i,1,1},{j,2,max},{k,1,max}]+
      Sum[1/(E^((i^2+j^2+k^2-3)/500)-1),{i,1,1},{j,1,1},{k,2,max}]]]
    

    which quickly returns

    {0.143978,14330.9}
    

    and

    max=25;
    Timing[N[
      Sum[1/(E^((i^2+j^2+k^2-3)/500)-1),{i,2,max},{j,1,max},{k,1,max}]+
      Sum[1/(E^((i^2+j^2+k^2-3)/500)-1),{i,1,1},{j,2,max},{k,1,max}]+
      Sum[1/(E^((i^2+j^2+k^2-3)/500)-1),{i,1,1},{j,1,1},{k,2,max}]]]
    

    which quickly returns

    {0.156976,14636.6}
    

    and even

    max=50;
    Timing[N[
      Sum[1/(E^((i^2+j^2+k^2-3)/500)-1),{i,2,max},{j,1,max},{k,1,max}]+
      Sum[1/(E^((i^2+j^2+k^2-3)/500)-1),{i,1,1},{j,2,max},{k,1,max}]+
      Sum[1/(E^((i^2+j^2+k^2-3)/500)-1),{i,1,1},{j,1,1},{k,2,max}]]]
    

    which quickly returns

    {1.36679,16932.5}
    

    Changing your code in this way avoids doing hundreds or thousands of If tests that will almost always result in True. And it potentially uses symbolic algorithms to find those results instead of needing to add up each one of the individual values.

    Compare those results and times if you replace Sum with NSum and if you replace /500 with *.002

    To try to guess why the times you see suddenly change as you increment the bound, other people have noticed in the past that it appears there are some hard coded bounds inside some of the numerical algorithms and when a range is small enough Mathematica will use one algorithm, but when the range is just large enough to exceed that bound then it will switch to another and potentially slower algorithm. It is difficult or impossible to know exactly why you see this change without being able to inspect the decisions being made inside the algorithms and nobody outside Wolfram gets to see that information.

    To get a more precise numerical value you can change N[...] to N[...,64] or N[...,256] or eliminate the N entirely and get a large complicated exact numeric result.

    Be cautious with this, check the results carefully to make certain that I have not made any mistakes. And some of this is just guesswork on my part.