next up previous index
Next: Complex Numbers Up: Numbers Previous: Forcing Floating Point Evaluation

Sums and Products

You can perform summations in Maxima in various ways. Function sum will return a numerical, although not necessarily a floating point, value if it can. This is how you would sum  $\sum_{i=1}^{10}\left(1+i\right)/\left(1+i^4\right)$:

\begin{figure}
\begin{tex2html_preform}\begin{verbatim}(C40) sum((1+i)/(1+i^4),i...
...-
40626648938819200088497
(C41)\end{verbatim}\end{tex2html_preform}\end{figure}

Another variant of this function, called 'sum delays  the evaluation:
\begin{figure}
\begin{tex2html_preform}\begin{verbatim}(C42) 'sum((1+i)/(1+i^4),...
...--
/ 4
==== I + 1
I = 1
(C43)\end{verbatim}\end{tex2html_preform}\end{figure}

We can force the evaluation in two ways. The first one is to declare a variable simpsum to be true and then call 'sum again:
\begin{figure}
\begin{tex2html_preform}\begin{verbatim}(C84) simpsum;
(D84) FALS...
...-
40626648938819200088497
(C88)\end{verbatim}\end{tex2html_preform}\end{figure}

The other way is to invoke ev on 'sum with the simpsum flag raised:
\begin{figure}
\begin{tex2html_preform}\begin{verbatim}(C88) simpsum:false;
(D88...
...;
(D91) 1.2678391664831168
(C92)\end{verbatim}\end{tex2html_preform}\end{figure}

The simpsum flag has no effect on sum, which always tries to evaluate the sum as far as it can.

We will see that delaying the evaluation is a technique frequently used in all three environments. The ability to control evaluation with flags is also very important.

In Maple the function sum is the same as sum in Maxima, and in place of Maxima's 'sum Maple has Sum:

\begin{figure}
\begin{tex2html_preform}\begin{verbatim}> sum((1+i)/(1+i^4), i=1....
...----
/ 4
----- 1 + i
i = 1>\end{verbatim}\end{tex2html_preform}\end{figure}

You can force the evaluation of Sum by saying 
\begin{figure}
\begin{tex2html_preform}\begin{verbatim}> value(%);
515080567275...
...497> evalf(%);
1.267839166>\end{verbatim}\end{tex2html_preform}\end{figure}

As you must have already guessed, we switch from Maxima/Maple to Mathematica by replacing sum with Sum and replacing the range i=1..10 with {i,1,10}:

\begin{figure}
\begin{tex2html_preform}\begin{verbatim}In[17]:= Sum[(1+i)/(1+i^4...
...0626648938819200088497In[18]:=\end{verbatim}\end{tex2html_preform}\end{figure}

Delaying evaluation in Mathematica is done quite differently though. Observe the following:
\begin{figure}
\begin{tex2html_preform}\begin{verbatim}In[8]:= mysum = Sum[(1+i)...
...32913722/40626648938819200088497\end{verbatim}\end{tex2html_preform}\end{figure}

Now we define yoursum, but this time we use := instead  of =:
\begin{figure}
\begin{tex2html_preform}\begin{verbatim}In[10]:= yoursum := Sum[(...
...0626648938819200088497In[13]:=\end{verbatim}\end{tex2html_preform}\end{figure}

When yoursum is requested in In[12] it is automatically evaluated, but it is stored as Sum[(1+i)/(1+i^4), {i,1,10}].

Working with infinite sums can be quite tricky. Consider the following sum: $\sum_{k=1}^{\infty} 1/k^2$. The result is $\pi^2/6$. Maxima can figure this out but you have to switch to the simpsum mode. 

\begin{figure}
\begin{tex2html_preform}\begin{verbatim}(C1) sum(1/k^2, k, 1, inf...
...mpsum;
2
%PI
(D2) ----
6
(C3)\end{verbatim}\end{tex2html_preform}\end{figure}

To check if this is correct, we can replace inf, which, by the way, Maxima, understands to be $+\infty$, with a very large integer and compare it to $\pi^2/6$:
(C8) sum(1/k^2,k,1,1000),numer;
(D8)                          1.6439345666815615
(C9) %pi^2/6,numer;
(D9)                          1.6449340668482264
(C10)
This is close enough for government work. Observe that you can force floating point evaluation by appending ,numer to the command, and that $\pi$ is represented in Maxima by %pi.

Maple is just as clever when it comes to this sum:

> sum(1/k^2, k=1..infinity);
bytes used=1004340, alloc=786288, time=0.44
                                2
                          1/6 Pi

>

And Mathematica knows all that stuff too:

\begin{figure}
\begin{tex2html_preform}\begin{verbatim}In[16]:= Sum[1/k^2, {k, 1...
... 2
Pi
Out[16]= ---
6In[17]:=\end{verbatim}\end{tex2html_preform}\end{figure}

The way Maxima, Maple and Mathematica go about it is to recognise one thing. Namely that the series is an infinite one. There is a relatively small number of infinite series about which a great deal is known. So the first thing that all three have to do is to check if the series in question relates to any of the ones that they know about. If it does then by the means of some additional transformations an exact mapping and thus an exact result may be found.

You can work with products in a way that is very similar to  sums. In Maxima we again have two forms to deal, say, with $\prod_{i=0}^{10} \left( i^2 +3 i - 11 \right) / \left( i + 3 \right)$:

\begin{figure}
\begin{tex2html_preform}\begin{verbatim}(C10) product(((i^2+3*i-1...
...--------
! ! I + 3
I = 0
(C12)\end{verbatim}\end{tex2html_preform}\end{figure}

However, the simpsum flag equivalent is not implemented, so you can't ev the 'product expression with that flag on and get a number.

In Maple we also have two forms: product and Product, where  Maxima's notation for range, i,0,10 is replaced with Maple's i=0..10:

\begin{figure}
\begin{tex2html_preform}\begin{verbatim}> product(((i^2+3*i-11)/(...
...00> evalf(%);
-192448.8197>\end{verbatim}\end{tex2html_preform}\end{figure}

In Mathematica products are produced with, yes you've guessed it, Product, which has its syntax similar to Maxima's and Maple's product:

\begin{figure}
{\footnotesize
\begin{tex2html_preform}\begin{verbatim}In[17]:= P...
...(i + 3), {i, 0, 10}]In[20]:=\end{verbatim}\end{tex2html_preform}}
\end{figure}


next up previous index
Next: Complex Numbers Up: Numbers Previous: Forcing Floating Point Evaluation
Zdzislaw Meglicki
2001-02-26