1

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 Answers1

1

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.)

acer
  • 5,293
  • Thanks a lot, for your thorough explanation! In all honesty, I wasn't aware of Maple's evaluation scheme but of course it explains everything! – ScottHastings Jun 15 '22 at 16:32
  • You could also forcibly delay the evaluation of that first argument by placing single right-ticks (aka uneval quotes) around the word ifelse, while using product in your example. But that's not such a robust approach in general, and can get messy with nested products. Also, you are computing an exact result with multiplication of finitely many terms (and the end-points of the index are numeric). So this is not a "symbolic product", with an answer of a closed formula that might attain for some example for k=1..N with N unassigned. So mul really is the appropriate choice, not product. – acer Jun 15 '22 at 20:57