Error, (in sum) summation variable previously assigned

1.3k Views Asked by At

Assume that I have a function like this

func1 := proc (a)
    return sum(i, i=1..a);
end proc;

This function works as long as I don't define a global variable $i$. If I do that, every subsequent call to func1 returns this error:

Error, (in sum) summation variable previously assigned, second argument evaluates to ...

I understand that Maple doesn't like the $i$ in the summation, as it thinks it needs to use the global $i$. Can I somehow tell Maple that the $i$ in the summation is a temporary variable and not to be confused with the global $i$?

P.S. In the real package, I have dozens of methods using summation and multiplication with indexes like $i$, $j$, etc. Renaming them all to something weird is not a solution, since you never know which variables the user has defined in his files.
P.P.S. If you do the same in Mathematica:

i = 4;
Sum[i, {i,1,k}];

this works without a hitch, as Mathematica recognizes the $i$ in the curly brackets as a temporary parameter. I can't imagine Maple can't do the same.

Thanks!

1

There are 1 best solutions below

0
On BEST ANSWER

You simply need to make the summation index a local variable:

func1:= proc(a)
     local i;
     sum(i, i= 1..a)
end proc;