1
$\begingroup$

In the following code, I assume that my variables are strictly positive. However, mathematica doesn't take this in consideration when it evaluates the If : the If is still in a non simplified form.

Why ?

In[99]:= Clear[PXMini, PXini, PM, lambda00, lambda00Bis, lambda, \
lambdaBis]

In[100]:= PXMini = Table[0, {i, 1, 8}, {j, 1, 8}];

In[101]:= PXMini[[1, 1]] = lambda00;

In[102]:= PXMini[[2, 1]] = lambda00Bis;

In[103]:= {PXMini[[3, 2]], PXMini[[5, 3]], PXMini[[7, 4]]} = {lambda, 
   lambda, lambda};

In[104]:= {PXMini[[4, 2]], PXMini[[6, 3]], 
   PXMini[[8, 4]]} = {lambdaBis, lambdaBis, lambdaBis};

In[105]:= PXini[x_] := Sum[PXMini[[x, k]], {k, 1, 8}];

In[106]:= PM[m_] := Sum[PXMini[[k, m]], {k, 1, 8}];

In[107]:= Infoi = 
 Assuming[lambda00 > 0 && lambda00Bis > 0 && lambda > 0 && 
   lambdaBis > 0, 
  Sum[If[PXMini[[x, m]] != 0, 
    PXMini[x, m]*Log[PXMini[x, m]/(PXini[x]*PM[m])], 0], {x, 1, 
    8}, {m, 1, 8}]]

Out[107]= 
3 If[lambda != 0, PXMini[x, m] Log[PXMini[x, m]/(PXini[x] PM[m])], 
   0] + If[lambda00 != 0, 
  PXMini[x, m] Log[PXMini[x, m]/(PXini[x] PM[m])], 0] + 
 If[lambda00Bis != 0, PXMini[x, m] Log[PXMini[x, m]/(PXini[x] PM[m])],
   0] + 3 If[lambdaBis != 0, 
   PXMini[x, m] Log[PXMini[x, m]/(PXini[x] PM[m])], 0]
$\endgroup$
  • $\begingroup$ It's easier to copy-paste if there aren't all those In[] labels. Consider SetOptions[$FrontEnd, ExportMultipleCellsOptions -> {"IncludeCellLabels" -> False}], if you want to make it easier for others to help you. $\endgroup$ – Michael E2 Dec 27 '18 at 23:04
3
$\begingroup$

First of all, on such a small number of terms, Sum uses the "Procedural" method, which does not need any assumptions. Second, Piecewise[] operates more like a function than If[]:

Incorrect result!:

Assuming[x == 0,
 Sum[If[x != 0, 1/n^2, 1/n^4], {n, 3}]
 ]
(*  3 If[x != 0, 1/n^2, 1/n^4]  *)

Correct result, but did not apply assumptions:

Assuming[x == 0,
 Sum[Piecewise[{{1/n^2, x != 0}}, 1/n^4], {n, 3}]
 ]

Mathematica graphics

Use Simplify[] inside Assuming[] to apply assumptions [update notice: I originally had Simplify[] outside sum, but since the summand depends on an external parameter, it probably makes more sense to apply Simplify[] to the summand directly]:

Assuming[x == 0,
 Sum[Simplify@Piecewise[{{1/n^2, x != 0}}, 1/n^4], {n, 3}]
 ]
(*  1393/1296  *)

The significant difference between If[] and Piecewise[] is that while both have the attribute HoldAll, Piecewise does evaluate its arguments:

Block[{n = 2},
 If[x != 0, 1/n^2, 1/n^4]
 ]
(*  If[x != 0, 1/n^2, 1/n^4]  *)

Block[{n = 2},
 Piecewise[{{1/n^2, x != 0}}, 1/n^4]
 ]
(*  Piecewise[{{1/4, x != 0}}, 1/16]  *)

Another workaround: Make the sum indefinite so that assumptions will be applied:

Assuming[x == 0 && k == 3,
 Sum[If[x != 0, 1/n^2, 1/n^4], {n, k}]
 ]
(*  1393/1296  *)
$\endgroup$
  • $\begingroup$ What do you mean exactly by If and Piecewise have the HoldAll attribute.I tried to find the exact definition on the documentation but it is written nowhere (at least for the If). Because for me saying that Piecewise has the holdAll attribute BUT evaluate its argument doesn't mean anything. (If he has a holdAll by definition it wouldn't evaluate its argument). However I can see that with your example the If doesn't evaluate the second & third arguments whereas the Piecewise does but I still don't understand what you precisely meant. $\endgroup$ – StarBucK Dec 28 '18 at 1:41
  • $\begingroup$ (I know the basic definition of HoldAll, for me what we put inside a HoldAll stays uncomputed, so HoldAll[1+1]=1+1 for example) $\endgroup$ – StarBucK Dec 28 '18 at 1:42
  • $\begingroup$ @StarBucK (1) You can find attributes with Attributes[If], Attributes[Piecewise], etc. You will see that HoldAll is among them. (2) HoldAll means that the arguments are passed to the function unevaluated, that is, in the form supplied in the function call. So the expression passed to f in f[1 + 2], if f is HoldAll, would be 1 + 2 and not 3. However, a function may evaluate its arguments or not. $\endgroup$ – Michael E2 Dec 28 '18 at 1:56
  • $\begingroup$ Now, If evaluates its first argument. This can be seen by executing Clear[x]; If[Print[x]; x == 0, 1, 2]. It does not evaluate its 2nd or 3rd argument unless the first argument evaluates to True or False. Piecewise likewise will evaluate its arguments as can be seen in one of the my examples. $\endgroup$ – Michael E2 Dec 28 '18 at 1:58
  • 1
    $\begingroup$ @StarBucK You have to use a function that uses $Assumptions for Assuming to have any effect. Since Sum does not in this case, you need something like Simplify. Since Sum sometimes applies $Assumptions, I can see that it's both confusing and irritating. Note that If[] does not look at $Assumptions at all. Neither does Solve nor Reduce, which is perhaps more surprising. $\endgroup$ – Michael E2 Dec 28 '18 at 4:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.