# Expression involving If and /. gives different result from direct evaluation

In[1]:= Table[Product[If[j == k, 1, (n x - j)/(k - j)], {j, 0, n}] /. n -> 3, {k, 0, 3}]

Out[1]= {1/6 (1 - 3 x) (2 - 3 x) (3 - 3 x), ( 3 (1 - 3 x) (2 - 3 x) (3 - 3 x) x)/(2 - 6 x), 3/2 x (-1 + 3 x),  1/2 x (-2 + 3 x) (-1 + 3 x)}

In[2]:= Table[Product[If[j == k, 1, (3 x - j)/(k - j)], {j, 0, 3}], {k, 0, 3}]

Out[2]= {1/6 (1 - 3 x) (2 - 3 x) (3 - 3 x), 3/2 (2 - 3 x) (3 - 3 x) x, 3/2 (3 - 3 x) x (-1 + 3 x), 1/2 x (-2 + 3 x) (-1 + 3 x)}

In[3]:= FullSimplify[% - %%]

Out[3]= {0, 0, -(3/2) x (2 + 9 (-1 + x) x), 0}


As far as I can tell, these should give the same result ... so what's going on here?

Try this:

Table[Inactivate@Product[If[j == k, 1, (n x - j)/(k - j)], {j, 0, n}] /. n -> 3, {k, 0, 3}] // Activate

Table[Product[If[j == k, 1, (3 x - j)/(k - j)], {j, 0, 3}], {k, 0, 3}]


Check that the outputs are the same:

% === %%
(*True*)


Note the differences in the use of your replacement rule for $$n$$:

• Interesting .. but why should the original input give a different answer?
– gmvh
Aug 16, 2022 at 18:23