I'm running into a strange problem. I want to implement a product in Maple with skipping some factors. For example (a very simplified one):
$$\prod_{\stackrel{k=1}{k \not= 3}}^{5} (x-k) = (x-1)(x-2)(x-4)(x-5).$$
In Maple I wrote
product(ifelse(k <> 3, x - k, 1), k = 1 .. 5)
But I get
(x - 1)*(x - 2)*(x - 3)*(x - 4)*(x - 5).
In other words, the ifthen seems to evaluate to true all the time.
I tested a little further and got those results:
product(i = 1, i = 1 .. 1);
1 = 1
product(evalb(i = 1), i = 1 .. 1);
false
So even when I force the evaluation with evalb I never get a true. The type of the index variable k is an ordinary symbol and outside of the product-context, comparing a symbol does work:
k := 1;
k := 1
k = 1;
1 = 1
evalb(k = 1);
true
What am I doing wrong here?
Best regards!
The
productcommand follows Maple's usual model of evaluation, in which the arguments passed to procedure calls are evaluated up front.In other words, that call to
ifelsegets evaluated beforeproductsees it. What theproductcommand receives is this result:You can see this via
trace.In contrast, the
mulcommand has so-called special evaluation rules, in which its first argument is not evaluated until the index namekactually attains its numeric values.This is a common mistake. (It's seen more often with
sumversusadd.)