% Copyright The Numerical Algorithms Group Limited 1991.
% Certain derivative-work portions Copyright (C) 1988 by Leslie Lamport.
% All rights reserved

% Title: Summations

% Address comments and questions to the
%   Computer Algebra Group, Mathematical Sciences Department
%   IBM Thomas J. Watson Research Center, Box 218
%   Yorktown Heights, New York  10598  USA

%  Author: Clifton J. Williamson
%  Date created: 2 November 1989
%  Date last updated: 2 November 1989

\begin{page}{ExSumListEntriesI}{Summing the Entries of a List I}
\beginscroll
In \Language{}, you can create lists of consecutive integers by giving the
first and last entries of the list.
Here's how you create a list of the integers between {\em 1} and {\em 15}:
\spadpaste{[i for i in 1..15]}
To sum the entries of a list, simply put {\em +/} in front of the list.
For example, the following command will sum the integers from 1 to 15:
\spadpaste{reduce(+,[i for i in 1..15])}
\endscroll
\autobuttons\end{page}

\begin{page}{ExSumListEntriesII}{Summing the Entries of a List II}
\beginscroll
In \Language{}, you can also create lists whose elements are some expression
{\em f(n)} as the parameter n ranges between two integers.
For example, the following command will create a list of the squares of
the integers between {\em 5} and {\em 20}:
\spadpaste{[n^2 for n in 5..20]}
You can also compute the sum of the entries of this list:
\spadpaste{reduce(+,[n^2 for n in 5..20])}
\endscroll
\autobuttons\end{page}

\begin{page}{ExSumApproximateE}{Approximating e}
\beginscroll
You can obtain a numerical approximation of the number {\em e} by summing the
entries of the following list:
\spadpaste{reduce(+,[1.0/factorial(n) for n in 0..20])}
\endscroll
\autobuttons\end{page}

\begin{page}{ExSumClosedForm}{Closed Form Summations}
\beginscroll
In a previous example, we found the sum of the squares of the integers
between {\em 5} and {\em 20}.
We can also use \Language{} to find a formula for the sum of the squares of
the integers between {\em a} and {\em b}, where {\em a} and {\em b}
are integers which will remain
unspecified:
\spadpaste{s := sum(k^2,k = a..b) \bound{s}}
{\em sum(k^2,k = a..b)} returns the sum of {\em k^2} as the index {\em k}
runs from {\em a} to {\em b}.
Let's check our answer in one particular case by substituting specific values
for {\em a} and {\em b} in our formula:
% Warning: syntax for polynomial evaluation will probably change.
\spadpaste{eval(s,[a,b],[1,25]) \free{s}}
\spadpaste{reduce(+,[i^2 for i in 1..25])}
\endscroll
\autobuttons\end{page}

\begin{page}{ExSumCubes}{Sums of Cubes}
\beginscroll
Here's a cute example.
First compute the sum of the cubes from {\em 1} to {\em n}:
\spadpaste{sum(k^3,k = 1..n)}
Then compute the square of the sum of the integers from {\em 1} to {\em n}:
\spadpaste{sum(k,k = 1..n) ^ 2}
The answers are the same.
\endscroll
\autobuttons\end{page}

\begin{page}{ExSumPolynomial}{Sums of Polynomials}
\beginscroll
\Language{} can compute {\em sum(f,k = a..b)}
when {\em f} is any polynomial in {\em k}, even
one with parameters.
\spadpaste{sum(3*k^2/(c^2 + 1) + 12*k/d,k = (3*a)..(4*b))}
\endscroll
\autobuttons\end{page}

%\begin{page}{ExSumRationalFunction}{Sums of Rational Functions}
%\beginscroll
%\Language{} can compute {\em sum(f,k = a..b)} for some rational functions (quotients
%of polynomials) in {\em k}.
%\spadpaste{sum(1/(k * (k + 2)),k = 1..n)}
%However, the method used (Gosper's method) does not guarantee an answer
%in every case.
%Here's an example of a sum that the method cannot compute:
%\spadpaste{sum(1/(k^2 + 1),k = 1..n)}
%\endscroll
%\autobuttons\end{page}

\begin{page}{ExSumGeneralFunction}{Sums of General Functions}
\beginscroll
Gosper's method can also be used to compute {\em sum(f,k = a..b)}
for some functions
f which are not rational functions in {\em k}.
Here's an example:
\spadpaste{sum(k * x^k,k = 1..n)}
\endscroll
\autobuttons\end{page}

% provide a package for infinite sums
\begin{page}{ExSumInfinite}{Infinite Sums}
\beginscroll
In a few cases, we can compute infinite sums by taking limits of finite
sums.
For instance, you can compute the sum of {\em 1/(k * (k + 2))} as {\em k}
ranges from
{\em 1} to {\em infinity}.
Use {\em \%plusInfinity} to denote `plus infinity'.
\spadpaste{limit( sum(1/(k * (k + 2)),k = 1..n) ,n = \%plusInfinity)}
\endscroll
\autobuttons\end{page}
