# Numerical evaluation of a sum

I am trying to compute numerically NSum[(-1)^n/n^3, {n, 1, Infinity}]. Of course, using first Sum would work here, but often it's not an option (no closed-form). I would like to compute the sum with an arbitrary number of digits.

So far, I've found that

NSum[(-1)^n/n^3, {n, 1, Infinity},
WorkingPrecision -> 24,
AccuracyGoal -> 24, PrecisionGoal -> Infinity]


Will give only 16 digits (-0.9015426773696957), whatever the value of WorkingPrecision and AccuracyGoal.

However, if I add the option Method -> "AlternatingSigns", I get as many digits as I want, and it's really controled by WorkingPrecision.

Among the other options, Method -> "EulerMaclaurin" gives a wrong answer. Not all digits are correct, and there is an imaginary part: -0.901542677369695714049804 + 15.503138340149910087738157 I. And Method -> "WynnEpsilon" also fails, giving -0.9015426773696957140, even if I ask for more digits.

So, is there something I'm doing wrong? Is there a reason it does not work if I don't give a specific method? Is there a more straightforward way to compute a sum (or any numerical result, for that matter)?

And, since I'm quite interested by numerical computations with Mathematica and I'm a beginner (with Mathematica, not with numerical analysis :-)), is there a good guide out there ?

As an aside, I know Maxima better, and I'm used to the option fpprec, that controls "bigfloat" evaluation in every computations. Is there something similar in Mathematica? I think it has a better control of accuracy, so maybe the question does not make sense (maybe there is no "global" setting).

To answer rasher's comment, since there is not much space in comments:

Actually, if I use larger values of the option NSumTerms, I get more and more digits, but they don't agree with what I get with Method -> "AlternatingSigns":

NSum[(-1)^n/n^3, {n, 1, Infinity}, WorkingPrecision -> 50,
AccuracyGoal -> 50, PrecisionGoal -> Infinity, NSumTerms -> 5000]


-0.90154267736969571404980362113358749307373971926

NSum[(-1)^n/n^3, {n, 1, Infinity}, WorkingPrecision -> 50,
AccuracyGoal -> 50, PrecisionGoal -> Infinity,
Method -> "AlternatingSigns"]


-0.90154267736969378050491347983360150530995076960417

After 14 digits, they are different numbers.

And here it's possible to check:

Sum[(-1)^n/n^3, {n, 1, Infinity}] // N[#, 50] &


-0.90154267736969571404980362113358749307373971925537

So, the answer with Method -> "AlternatingSigns" is also wrong!

And it's not better if I combine Method and NSumTerms options:

NSum[(-1)^n/n^3, {n, 1, Infinity}, WorkingPrecision -> 50,
AccuracyGoal -> 50, PrecisionGoal -> Infinity,
Method -> "AlternatingSigns", NSumTerms -> 5000]


-0.90154267736969571404546474460914681065800480608588

So the results differ after 20 digits.

Now I'm even more puzzled :-) I can see that your comment is right, since I get the right answer, but I absolutely don't understand why, and why the other methods fail without notification.

Also, it's not a very satisfying approach, since I can't set the number of accurate digits in the output (it depends on NSumTerms in a non obvious way).

-
NSumTerms is your friend... –  rasher Jun 7 at 9:00
@rasher, I edited the post to answer your comment, since I lacked space. –  Jean-Claude Arbaut Jun 7 at 9:13
@Jean-ClaudeArbaut - Sum and NSum with the NSumTerms-option deliver the same result (write N[#, 47]&) –  eldo Jun 7 at 9:30
@eldo, yes, but I had to choose a large enough value of NSumTerms (1000 is not enough for 50 digits, 5000 is almost enough). Is there a way to simply tell Mathematica "I want 50 digits, choose the options as you wish"? Maybe I'm asking for too much, but I find rather strange that many options give wrong results, and one option give the right answer, but with the correct parameter. Not easy to do that automatically. How am I supposed to know I've found the right option, if I can't check with closed form? –  Jean-Claude Arbaut Jun 7 at 9:43
Note that N[Unevaluated[Sum[(-1)^n/n^3, {n, 1, Infinity}]], 20] gives the message that $MaxExtraPrecision will be reached, so here we at least get an error if the answer is incorrect. Using Sum and N together like this will cause a call to NSum, as can be seen from MemberQ[Trace[N[Unevaluated[Sum[(-1)^n/n^3, {n, 1, Infinity}]]], TraceInternal -> True], NSum, Infinity, Heads -> True] evaluating to True. However, the approach can also lead to wrong results, like in N[Unevaluated[Sum[1/n^2, {n, 1, Infinity}]], 100], even when setting $MaxExtraPrecision and \$MinPrecision. –  Jacob Akkerboom Jun 7 at 10:11

x = N[Sum[(-1)^n/n^3, {n, 1, Infinity}], 47]

y = NSum[(-1)^n/n^3, {n, 1, Infinity}, WorkingPrecision -> 47,
AccuracyGoal -> 47, PrecisionGoal -> 47, NSumTerms -> 10000]

z = NSum[(-1)^n/n^3, {n, 1, Infinity}, Method -> "AlternatingSigns",
WorkingPrecision -> 47, AccuracyGoal -> 47, PrecisionGoal -> 47]

x == y == z


True (-0.90154267736969571404980362113358749307373971926)

Using the simplest form:

e = NSum[(-1)^n/n^3, {n, 1, Infinity}]


we get with

e // FullForm


-0.9015426773696957

which is clearly less precise. But!

x === y === z === e


True

Strange ...

-
Interesting, it seems my PrecisionGoal -> Infinty was causing some trouble. I see now that AccuracyGoal -> Infinity, PrecisionGoal -> 47 is better than AccuracyGoal -> 47, PrecisionGoal -> Infinty. –  Jean-Claude Arbaut Jun 7 at 9:58
@Jean-ClaudeArbaut - It is, of course, unsatisfactory, that you only find the "correct" number for NSumTerms (approx. 10000) by trial and error. Thanks for this interesting question. –  eldo Jun 7 at 10:01
This doesn't directly answer the original question, but why not use the fact that Mathematica can evaluate the exact sum symbolically: Sum[(-1)^n/n^3, {n, 1, Infinity}] gives (-3*Zeta[3])/4. You may then use N, with a 2nd argument to specify the desired precision. –  murray Jun 7 at 15:23
@murray - for me thats THE answer if you can evaluate symbolically. –  eldo Jun 7 at 15:30
@eldo: OK, I made my comment into an answer. –  murray Jun 7 at 19:40

If you look at the FullForm or InputForm of the NSum result without using "AlternatingSigns" you will see that more digits are available; however, display is limited by the determined precision which is less than 17 digits. The determined precision which is implied by the shortened result also can be seen using Precision.

approx1 = NSum[(-1)^n/n^3, {n, 1, Infinity}, WorkingPrecision -> 24, AccuracyGoal -> 24, PrecisionGoal -> Infinity]


-0.9015426773696957

#[approx1] & /@ {Precision, FullForm, InputForm}


{16.774,-0.9015426773696956967432522482628641510116.773986512910163,-0.9015426773696956967432522482628641510116.773986512910163}

If the precision of the result is less than the requested precision then you should start investigating other methods.

approx2 = NSum[(-1)^n/n^3, {n, 1, Infinity}, WorkingPrecision -> 24, AccuracyGoal -> 24, PrecisionGoal -> Infinity, Method -> "AlternatingSigns"]


-0.901542677369693780504913

#[approx2] & /@ {Precision, FullForm, InputForm}


{24.1487,-0.90154267736969378050491347983360150530995076960472861836724.148671096657576,-0.90154267736969378050491347983360150530995076960472861836724.148671096657576}

-
Thanks. So that means you can get a result with lower precision than requested? (it's what I understand by playing around with this problem) I hoped there was a way to "just" get the requested precision, and Mma do all the stuff, but from links in comments above, I'm just starting to understand Mma numerics are much more complex than just "N" with number of digits. :-) –  Jean-Claude Arbaut Jun 7 at 14:46

This doesn't directly answer the original question, but why not use the fact that Mathematica can evaluate the exact sum symbolically:

    s = Sum[(-1)^n/n^3, {n, 1, Infinity}]
(-3*Zeta[3])/4 *)


Then you may use N, with a 2nd argument to specify the desired precision, e.g.:

    N[s, 50]
(* -0.90154267736969571404980362113358749307373971925537 *)


(Of course such an approach will only work with those special series whose sums Mathematica already knows about somehow.)

-
May I suggest that you split your answer into two lines: x = Sum[(-1)^n/n^3, {n, 1, Infinity}] // InputForm. Then: N[x, 100]` (would make it clearer for the occasional reader). –  eldo Jun 7 at 20:02
For a sum with alternating signs, be it numeric or symbolic, it is often worthwile to try summing the odd and even terms separately. In this case, both NSum calculate to the desired precision, and as a last step, just subtract. –  Wouter Jun 9 at 20:51
And there is a well-known ("Calculus II") Alternating Series Test whose proof implies that, for an alternating series whose terms have absolute values that decrease and approach 0, the (mathematical) truncation error in using the *n*th partial sum does not exceed the magnitude of the next term. –  murray Jun 10 at 0:22