You are right, the result is wrong. No need for Python, though:
Sum[(16 - 32 k -
4 (-1)^k (4 k - 3) (16 k^2 - 1))/((2 k - 1) (4 k - 3) (16 k^2 -
1)), {k, Infinity}]
% // FullSimplify
Ralph,
I would hesitate using the terms “wrong”, “bug”, etc. in the title of a post.
From the documentation on NSum:
NSumgives a numerical approximation to the sum
And in fact the result here is not “wrong”, it rather is a less good approximation. But this approximation can be improved by various options, e.g.:
two[n_] :=
NSum[(16 - 32 k -
4 (-1)^k (4 k - 3) (16 k^2 - 1))/((2 k - 1) (4 k - 3) (16 k^2 -
1)), {k, 1., n},
Method -> "AlternatingSigns", NSumTerms -> 100, WorkingPrecision -> 30]
two[Infinity]
(* Out: 2.00000889 *)
Does that help? Regards – Henrik
I am no expert at all about numerical algorithms, and I probably have unrealistic expectations.
The word “wrong” is used in the documentation, though: “You should realize that with sufficiently pathological summands, the algorithms used by NSum can give wrong answers.”
NSum was last updated in 2007. Maybe it is time for a revamp. For example, it could discover by itself that the series has alternating signs, and it could find reasonable error estimates.
As for Python, it may do something like this:
Plus @@ Table[
N[(16 - 32 k -
4 (-1)^k (4 k - 3) (16 k^2 - 1))/((2 k - 1) (4 k -
3) (16 k^2 - 1))], {k, 1, 1000000}] // FullForm
which is much more precise and much slower than NSum, which takes shortcuts.
The following gives acceptable approximations. You just have to learn to be a Mathematica whisperer.
Here we know the exact answer in advance. What if we didn’t know? I admit my ignorance in this field.
Sometimes you have to do a little analysis on simpler forms compared to the full expression and make a guess.
But come to find out none of this was necessary; see Gianluca Gorni’s reply.