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{tex2html_preform}\begin{verbatim}(C40) sum((1+i)/(1+i^4),i...

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

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{tex2html_preform}\begin{verbatim}(C84) simpsum;
(D84) FALS...

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

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{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{tex2html_preform}\begin{verbatim}> value(%);
...497> evalf(%);

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{tex2html_preform}\begin{verbatim}In[17]:= Sum[(1+i)/(1+i^4...

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

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

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{tex2html_preform}\begin{verbatim}(C1) sum(1/k^2, k, 1, inf...
(D2) ----

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
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
                          1/6 Pi


And Mathematica knows all that stuff too:

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

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{tex2html_preform}\begin{verbatim}(C10) product(((i^2+3*i-1...
! ! I + 3
I = 0

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{tex2html_preform}\begin{verbatim}> product(((i^2+3*i-11)/(...
...00> evalf(%);

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{tex2html_preform}\begin{verbatim}In[17]:= P...
...(i + 3), {i, 0, 10}]In[20]:=\end{verbatim}\end{tex2html_preform}}

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