Expression summation

We have just looked at adding numbers, but how does this differ from summation?

The Maple sum(...) function behaves more like diff(...) and int(...): the function looks at the expression and attempts to determine the solution without explicitly calculating the sum. For example, in secondary school, you saw that:

$\sum_{k = 1}^n k = \frac{n(n + 1)}{2}$

Thus, when you call

[> sum( k, k = 5..20 );

the sum(...) function looks at the first argument k and from this deduces that the correct formula is $\frac{n(n + 1)}{2}$ and that consequently, the answer is:

$\frac{n (n + 1)}{2}\Big |_{n = 20} - \frac{n (n + 1)}{2}\Big |_{n = 4} = 200$

Indeed, very often, you can provide an unknown lower and upper bound:

[> sum( k, k = a..b );

$\frac{(b + 1)^2}{2} - \frac{b}{2} - \frac{1}{2} - \frac{a^2}{2} + \frac{a}{2}$

Now, in general, we sum from $0$ or $1$ to an unknown $n$:

[> sum( k^2, k = 1..n );

$\frac{(n + 1)^3}{3} - \frac{(n + 1)^2}{2} + \frac{n}{6} + \frac{1}{6}$

Maple is aware of other interesting functions:

[> sum( 1/k, k = 1..n );

$\Psi(n + 1) + \gamma$

Here, $\Psi$ is the digamma function. If you asked what the infinite sum was, Maple returns infinity:

[> sum( 1/k, k = 1..infinity );

$\infty$

Now, this is a little mor interesting:

[> sum( 1/k^2, k = 1..n );

$-\Psi(1, n + 1) + \frac{\pi^2}{6}$

The two-argument implementation of this function has the derivative as the first argument, and in this case, the infinite sum results in just the constant:

[> sum( 1/k^2, k = 1..infinity );

$\frac{\pi^2}{6}$

Maple is also useful for calculating geometric series:

[> sum( z^k, k = 0..n );

$\frac{z^{n + 1}}{z - 1} - \frac{1}{z - 1}$

Consequently, you may be able to consider an infinite sum:

[> sum( z^k, k = 0..infinity );

$\sum_{k = 0}^\infty z^k$

Maple returned unevaluated, as no general solution exists for an arbitrary $z$; instead, a solution only exists if $|z| < 1$, so we must let Maple know that it should make this assumption when determining the sum:

[> sum( z^k, k = 0..infinity ) assuming abs( z ) < 1;

$-\frac{1}{z - 1}$

Now, suppose you have one signal $(1, z, z^2, z^3, \cdots)$ and a second signal $(1, w, w^2, w^3, \cdots)$, and you'd like to take the inner product of these two signals:

[> sum( w^k * z^k, k = 0..infinity ) assuming abs( z ) < 1 and abs( w ) < 1;

$-\frac{1}{wz - 1}$

Similarly, you could calculate the 2-norm of a signal:

[> sqrt( sum( (z^k)^2, k = 0..infinity ) ) assuming abs( z ) < 1;

$\sqrt{-\frac{1}{z^2 - 1}}$

In both cases, Maple can determine that if $|z| < 1$ and $|w| < 1$, then it must also be true that $|z^2| < 1$ and $|wz| < 1$.

In determining the run time of specific algorithms, Maple is very helpful in finding solutions to sums not normally found in secondary school or undergraduate text books. For example, in determining the run time of a heap, one may, depending on your analysis, end up calculating the following where $N$ is the height of a perfect binary tree:

[> rt := sum( k*2^k, k = 0..N );

$rt := (N + 1)2^{N + 1} - 2 2^{N + 1} + 2$

[> rt := simplify( rt, 'size' );

$rt := 2 + (N - 1)2^{N + 1}$

Now, $N = log_2(n + 1) - 1$ where $n$ is the number of items in the perfect tree, so we now look at:

[> rt := eval( rt, N = log[2](n + 1) - 1 );

$rt := 2 + \left( \frac{\ln(n + 1)}{\ln(2)} - 2\right) 2^\frac{\ln(n + 1)}{\ln(2)}$

You recall from secondary school that $\log_k(x) = \frac{\ln(x)}{\ln(k)}$, so you suspect this expression can be simplified, because $2^{\log_2(x)} = x$:

[> rt := simplify( rt );

$rt := 2 + \left( \frac{\ln(n + 1)}{\ln(2)} - 2 \right) (n + 1)$

and you know that this is $2 + (\log_2(n + 1) - 2)(n + 1) = O(n\ln(n))$, and you can even get the asymptotic behavior from Maple:

[> asympt( rt, n );

$\left( \frac{\ln(n)}{\ln(2)} - 2 \right)n + \frac{\ln(n)}{\ln(2)} + \frac{1}{\ln(2)} + \frac{1}{2 \ln(2) n} - \frac{1}{6 \ln(2)n^2} + \frac{1}{12 \ln(2)n^3} - \frac{1}{20 \ln(2)n^4} + O\left(\frac{1}{n^5}\right)$

Remember: the sum(...) function does a lot more work, and attempts to find a solution algebraically. If your only goal is to add a number of values together, use add(...).