5.6. Forloop like commands
The three commands
seq
,
add
, and
mul
act very much like forloops. In a sense they are abbreviations of special purpose forloops. Let us look at a few examples of each of these commands
We have seen the
seq
command before. It is used to create expression sequences. Here is an example.
> 
seq( ifactor(n), n=1..10 );

Compare this last command to the following forloop.
> 
for n from 1 to 10 do ifactor( n ) od;

Both commands did roughly the same thing. An index variable
n
was incremented, in steps of 1, from an initial value of
1
to a final value of
10
and for each value of the index variable the procedure
ifactor(n)
was evaluated. The main difference between the two commands is that the
seq
command produced one result, an expression sequence, but the forloop produced 10 separate results. Here is a way to rewrite the forloop so that it produces an expression sequence.
> 
result := NULL: # Start with an empty exprseq.

> 
result := result, ifactor(n) # Append an operand to the exprseq.

> 
result; # Show the final exprseq.

Except for the fact that this execution group needed to use an extra variable, the execution group produces the same result as the above
seq
command.
Here is another example. This produces, more or less, one line of Pascal's triangle.
> 
seq( op(1,n), n=expand((a+b)^12) );

In this case, instead of the index variable counting from an initial value to a final value, the index variable steps through the operands of a data structure. The following forinloop produces the same expression sequence as the
seq
command.
> 
for n in expand( (a+b)^12 ) do

> 
result := result, op(1,n)

Notice how much more clear it is to use the
seq
command. Besides being easier to read and write, the
seq
command is also more computationally efficient than the equivalent forloop. The forloop produces a lot of intermediate results (which we hide by using a colon) but the
seq
command is implemented in a way that avoids all the intermediate expression sequences.
Now let us turn to the
add
command. The following command will add up the first ten squares.
This can also be done with a forloop.
The main difference between the two is that the forloop needed an extra variable and it produced a lot of intermediate results. The
add
command is also faster. Try adding up the first one million squares using first the
add
command and then the forloop.
Recall that earlier we said that forloops can be used to implement the sigma notation used in mathematics. The
add
command is a direct analogue in Maple to sigma notation. An
add
command of the form
add( f(n), n=a..b )
means exactly the same thing as
and their forloop equivalent is
result := 0:
for n from a to b do
result := result + f(n)
od:
result;
By the way, look at the interesting output from these nested "loop" commands. They sum up the first 10 squares, then the first 100 squares, then the first 1000 squares, etc. Can you explain the pattern?
The
mul
command is much like the
add
command, it just uses multiplication instead of addition. So the following command will find the product of the first ten squares.
Here is the equivalent forloop.
The
mul
command is a direct analogue in Maple to mathematical product notation. A
mul
command of the form
mul( f(n), n=a..b )
means exactly the same thing as the standard mathematical product notation
and their forloop equivalent is
result := 1:
for n from a to b do
result := result * f(n)
od:
result;
There are two Maple commands that are related to the
add
and
mul
command but they are not abbreviations for forloops. They are the
sum
and
product
commands. These two commands do
symbolic summation
and
symbolic multiplication
. For example, consider the next command, which will sum up the first
j
squares.
> 
sum( 'n^2', 'n'=1..j );

This command gave us a symbolic answer for the sum of the first
j
squares for
any
value of
j
. The result from the
sum
command can be simplified quite a bit.
The
sum
command can even sum up infinite series. Here is a geometric series.
> 
sum( 'r^n', 'n'=1..infinity );

The product command does much the same thing for products.
> 
product( 'n^2', 'n'=1..j );

The product of the first
j
squares is given symbolically in terms of a special function,
, called the gamma function. Let us test this result with
j
equal to 99.
The
product
command can also do infinite products. The following command uses the "inert" form of the
product
command ( i.e.
Product
) to display a typeset version of the product notation on the left hand side of an equal sign, and on the right of the equal sign is the regular
product
command to evaluate the infinite product.
> 
Product( 11/(4*'n'^2), 'n'=1..infinity )

> 
= product( 11/(4*'n'^2), 'n'=1..infinity );

Here is one tricky difference between
add
,
mul
on the one hand and
sum
,
product
on the other hand. Let us give the variable
i
a value (it does not matter what value).
Now use
i
as the index variable in an
add
and a
mul
command.
Let us check the value of
i
now.
It is still zero. The value of
i
did not affect, and was not affected by, the index
i
in the
add
or
mul
commands. The index variable in an
add
or a
mul
command is local to that command and does not have anything to do with the global variable with the same name (just like local variables in procedures). Now try the
sum
and
product
commands.
Error, (in sum) summation variable previously assigned, second argument evaluates to 0 = 1 .. 10
Error, (in product) product variable previously assigned, second argument evaluates to 0 = 1 .. 10
The index variable in the
sum
and
product
commands are global variables. Here is how to fix the last two commands.
> 
product( 'i', 'i'=1..10 );

Now check the value of
i
.
Strangely enough, even though the index variable in the
add
and
product
commands is the global variable
i
,
i
still retains its value from before the commands were executed.
The moral of this is that you need to be more careful when using
sum
and
product
than when you use
add
or
mul
. Always use
add
and
mul
if you do not need the extra abilities of
sum
and
product
.
Exercise
: Compare the way Maple handles the "index variable" in
add
,
mul
,
sum
, and
product
with the way Maple handles the index variable in a forloop.