Maple: Comparing an index variable to an integer in the context of product

39 Views Asked by At

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!

1

There are 1 best solutions below

2
On BEST ANSWER

The product command 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 ifelse gets evaluated before product sees it. What the product command receives is this result:

ifelse(k <> 3, x - k, 1);

             x - k

You can see this via trace.

trace(product):
product(ifelse(k <> 3, x - k, 1), k = 1 .. 5);
  {--> enter product, args = x-k, k = 1 .. 5
  ....

In contrast, the mul command has so-called special evaluation rules, in which its first argument is not evaluated until the index name k actually attains its numeric values.

mul(ifelse(k <> 3, x - k, 1), k = 1 .. 5);

      (x - 1) (x - 2) (x - 4) (x - 5)

This is a common mistake. (It's seen more often with sum versus add.)