alternating series, sum and add (22.11.99)

John Kurtzke

I have found Maple adds terms in a series very quickly, but I have had problems with the alternating harmonic series, sum( (-1)^(n+1)/n ) It will sum this up to 1000 quickly, but with 10,000 or more, Maple gobbles up memory to the limit and locks things up.

Specifically:

evalf( sum( ((-1)^(n+1))/n, n=1..N) );

works well when N= 1000, but not at all for N = 10000.

I then broke things up to even & odd and Maple computed the sums in a flash:

odds := evalf( sum( 1/(2*n+1), n=1..N) ); 
evens := evalf( sum(1/(2*n), n=1..N) );

I used 10000 for N and Maple computed these two sums in the blink of an eye.

I suspect Maple is having trouble with the alternating nature of the original series, but I don’t know why.

This has happened using Maple V Release 5.0 on both my (old) Power Mac 8100/100 and on a Pentium III PC in one of our computer classrooms.

Bill Bauldry (23.11.99)

Consider using an inert Sum instead of sum. The sum attempts an antidifference, then goes to the evalf. The Sum just dumps to evalf. Here’s what I get on a Mac G3:

> restart; 
> S := N -> evalf( sum( ((-1)^(n+1))/n, n=1..N) ): 
> SI := N -> evalf( Sum( ((-1)^(n+1))/n, n=1..N) ): 
> time(): 
> evaln(S)[1000] = S(1000); 
> (time() - %%)*seconds; 
 
> time(): 
> evaln(SI)[1000] = SI(1000); 
> (time() - %%)*seconds; 
                        S[1000] = .6926474306 
                             .317 seconds 
 
                        SI[1000] = .6926474306 
                             .067 seconds 
> time(): 
> evaln(S)[10000] = S(10000); 
> (time() - %%)*seconds; 
 
> time(): 
> evaln(SI)[10000] = SI(10000); 
> (time() - %%)*seconds; 
                        S[10000] = .6930971831 
                            12.333 seconds 
 
                       SI[10000] = .6930971831 
                             .783 seconds

Robert Israel (23.11.99)

I don’t think that’s it at all. The ”sum” function is for symbolic summation. It finds sum((-1)^(n+1)/n, n=1..N) (with N left unassigned) as

                          (N + 1) 
          ln(2) + 1/2 (-1)        (Psi(1 + 1/2 N) - Psi(1/2 N + 1/2)) 
and it finds sum(1/n, n=1..N) as 
 
          Psi(N + 1) + gamma 
It finds sum(1/n, n=1..10000) as 
 
          Psi(10001) + gamma

(which ”evalf” can then handle, giving a numerical answer 9.787606026). However, for some reason (I don’t know exactly why) it doesn’t calculate the symbolic solution for sum((-1)^n/n, n=1..N) when N has a numerical value; instead it simply adds the terms of the sum. This is done with rational arithmetic, so the result will be a rational number with huge numerator and denominator: for N=10000 the numerator and denominator each have 4346 digits, and I expect that for N=10000 they will have about 4320 digits.

If you want a numerical answer obtained from adding up the individual terms, you can use

> evalf(Sum((-1)^(n+1)/n, n=1..10000));

Or if you want to evaluate the symbolic answer numerically, you can use

> evalf(eval(sum((-1)^(n+1)/n, n=1..N), N=10000));

Douglas B. Meade (24.11.99)

There are a number of issues involved with your observation. I am working with Release 5.1 under Solaris (UNIX) so some of the memory issues are a little different.

First, you need to be aware of the differences between the sum and add commands. The sum (and Sum) commands attempt to find explicit formulae for definite and indefinite sums; only add actually adds each term in the sum. (Well, sum will add the terms if all attempts to find an explicit formula fail.)

The following session illustrates some of these differences. Note how Maple automatically simplifies the sum when sum is used. This makes all evaluations extremely fast. The next section, which uses the intert summation, Sum, is similar.

It is somewhat disturbing that Maple could not make up its mind whether to return a difference of Psi functions or the LerchPhi function, but that is an issue for the developers (hint, hint). In the third section, the sum is computed using add.

Here we see a slowdown as N increases. In this demonstration the timings are slight superlinear; in other tests they were much more linear. The specifics of these timings are likely to be very dependent on the amount of memory, etc.

I hope you will find this response informative, even if it only indirectly addresses your question about even and odd sums.

> restart; 
> s := sum( (-1)^(n+1)/n, n=1..N ); 
 
                        (N + 1) 
   s := ln(2) + 1/2 (-1)        (Psi(1 + 1/2 N) - Psi(1/2 N + 1/2)) 
 
> st := time(): N := 10^3: evalf( s ); time()-st; 
 
                              .692647431    .010 
 
> st := time(): N := 10^4: evalf( s ); time()-st; 
 
                              .693097184     0 
 
> st := time(): N := 10^5: evalf( s ); time()-st; 
 
                              .693142181     0 
 
> st := time(): N := 10^3-1: evalf( s ); time()-st; 
 
                              .693647431     0 
 
> st := time(): N := 10^4-1: evalf( s ); time()-st; 
 
                              .693197184     0 
 
> st := time(): N := 10^5-1: evalf( s ); time()-st; 
 
                              .693152181     0 
 
 
> restart; 
> S := Sum( (-1)^(n+1)/n, n=1..N ); 
 
> st := time(): value( S ); time()-st; 
 
                       (N + 1) 
           ln(2) + (-1)        (-LerchPhi(-1, 1, N) + 1/N) 
 
                                 .210 
 
> st := time(): N := 10^3: evalf( S ); time()-st; 
 
                             .6926474306     .080 
 
> st := time(): N := 10^4: evalf( S ); time()-st; 
 
                             .6930971831    1.000 
 
> st := time(): N := 10^5: evalf( S ); time()-st; 
 
                             .6931421806   11.150 
 
> st := time(): N := 10^3-1: evalf( S ); time()-st; 
 
                             .6936474306     .080 
 
> st := time(): N := 10^4-1: evalf( S ); time()-st; 
 
                             .6931971831    1.020 
 
> st := time(): N := 10^5-1: evalf( S ); time()-st; 
 
                             .6931521806   11.150 
 
> restart; 
> N := 10^3: st := time(): evalf( add( (-1)^(n+1)/n, n=1..N )); time()-st; 
 
                             .6926474306     .220 
 
> N := 10^4: st := time(): evalf( add( (-1)^(n+1)/n, n=1..N ) ); time()-st; 
 
                             .6930971831   16.410 
 
> N := 10^4-1: st := time(): evalf( add( (-1)^(n+1)/n, n=1..N ) ); time()-st; 
 
                             .6931971831   16.540