upiterbarg / mpmath

Automatically exported from code.google.com/p/mpmath
Other
0 stars 0 forks source link

nsum convergence criteria #236

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,

I have tried to use nsum() for some cosine series, like: cos(nx)/(n^2)
however it doesn't work for x=pi*0.05. All methods produce wrong result with 
fixed error.
The reason is wrong stopping criteria for direct summation method 
(which is also used for all other methods). The direct method stops if 
the last term after an update() is smaller then tolerance. In my case
in term for n=10 is cos(nx)=1e-17 and ever 20th term is nearly zero.
So if the method do not stop after the first 10 terms it will stop sooner then 
desired.

extrapolation.py, function

adaptive_extrapolation
lines 507-509 in 
https://github.com/sympy/sympy/blob/master/sympy/mpmath/calculus/extrapolation.p
y

            # Check direct error
            best = partial[-1]
            error = abs(best - partial[-2])

The problem is that error is equal to absolute value of the last term only 
which can be very close to zero.
Rather one should use something like:

            error = abs(partial[-1] - partial[last_index])

where "last_index" is index before last update() call, similarly to the error 
computed for extrapolation methods.

What version of the product are you using? On what operating system?

mpmath 0.17-1 on KUbuntu 12.04 

Jan Brezina

Original issue reported on code.google.com by jbrezm...@gmail.com on 21 Mar 2013 at 10:52

GoogleCodeExporter commented 9 years ago
Hi Jan, thanks for the bug report.

I'm not sure if the proposed fix is sufficient, and it would slow down 
convergence of the direct method for well-behaved series.

I think there is a fairly easy workaround: explicitly add several terms at a 
time, say three: instead of

nsum(f, [1,inf])

do

nsum(lambda n: f(3*n-2) + f(3*n-1) + f(3*n), [1,inf])

Original comment by fredrik....@gmail.com on 21 Mar 2013 at 11:10

GoogleCodeExporter commented 9 years ago
Hi Fredrik,

Thank you for the workaround. However, there is still problem, that 
tolerance has different meaning in direct method and in extrapolation 
method. In the first case you measure only magnitude of terms in the 
second case you measure difference between individual steps.

If you woldn't touch convergence speed of direct method what about 
average over updated terms, this has straightforward counterpart in
extrapolation methods. The average has advantage that the meaning of 
tolerance is "independent" of the value of steps.

Jan

Original comment by jbrezm...@gmail.com on 21 Mar 2013 at 1:34