0

My goal is to replace 1+n with α+β. But the results for code1 and code2 are different as shown below. I don't know why? (I checked FullForm and it's the same)

f[n_, k_, x_] := 
 n! /((k - 1)! *(n - k)!) F[x]^(k - 1) (1 - F[x])^(n - k) f[x]

(Code1)

f[n, k, x] /. {(n - k) :> \[Beta] - 1, k :> \[Alpha]}  // 
 FullSimplify   /. (1 + n) :> \[Alpha] + \[Beta]       (*Code1*)

(Code2)

exp = f[n, k, x] /. {(n - k) :> \[Beta] - 1, k :> \[Alpha]}  // 
   FullSimplify ;
exp /. (1 + n) :> \[Alpha] + \[Beta]         (*Code2*)

※Screen Capture enter image description here

Soon
  • 648
  • 3
  • 8
  • 1
    You've forgotten about precedence: (f[n, k, x] /. {(n - k) :> \[Beta] - 1, k :> \[Alpha]} // FullSimplify) /. (1 + n) :> \[Alpha] + \[Beta] When you're not sure about precedence, try N-click the code, or press Ctrl + . to check the precedence: https://mathematica.stackexchange.com/a/249596/1871 – xzczd Jan 21 '24 at 04:30

1 Answers1

1

This is interesting example. All replacements after FullSimplify are not applied. That is why.

Lets look at very simple example to better see this.

ClearAll["Global`*"]
expr = (n - k) + m;
expr /. {(n - k) :> b} // Simplify /. m :> e

The above says to replace (n - k) by b, which will give b+m and then to replace m by e which should result in b+e. But it does not. It results in b+m. The whole thing after // Simplify is simply not used (Trace confirms this).

Mathematica graphics

You can either do one of the following two options. The first as suggested by xzczd in comment above

ClearAll["Global`*"]
expr = (n - k) + m;
(expr /. {(n - k) :> b} // Simplify) /. m :> e

Mathematica graphics

Or move // Simplify to the very end!

ClearAll["Global`*"]
expr=(n-k)+m;
expr/.{(n-k):> b}/.m:>e//Simplify

Mathematica graphics

Basically, adding //Simplify in the middle has the effect of short circuiting the computation. So rule of thumb, always add // at very end of the command and not in the middle. (I never add // command in middle of commands myself because I simply do not like to chain too many commands into one long one and prefer to write many smaller commands one after the other). The code might becomes longer but for me it is more clear.

To see this, just do Trace and you will see that everything after //Simplify is not applied.

enter image description here

Nasser
  • 143,286
  • 11
  • 154
  • 359
  • Thank you for your answer! Without explicit precedence, it seems that Simplify function "eat"s the rule, like myF[x_]:=x+2; Plus[3,a]//myF/. 2->3 (2 in the myF replaced by 3) – Soon Jan 22 '24 at 03:01