Prev: Remote kernel only works with "raw mathlink connection" selected
Next: FindRoot and Bose-Einstein distribution
From: magma on 12 Oct 2007 03:13 I do not understand what Mathematica 6 is doing here: a=5 a/.a->3 gives 3 But... a+2/. a->3 gives 7 Why is that? Thank you for your help
From: Szabolcs Horvát on 13 Oct 2007 03:50 magma wrote: > I do not understand what Mathematica 6 is doing here: > > a=5 > a/.a->3 > > gives 3 > > But... > > a+2/. a->3 > > gives 7 > > Why is that? > > Thank you for your help > Hi, Use Trance[a /. a->3] or On[] and Off[] to find out how Mathematica evaluates an expression. The following output should explain what happens: In[1]:= On[] During evaluation of In[1]:= On::trace: On[] --> Null. >> In[2]:= a=5 During evaluation of In[2]:= Set::trace: a=5 --> 5. >> Out[2]= 5 In[3]:= a/.a->3 During evaluation of In[3]:= a::trace: a --> 5. >> During evaluation of In[3]:= a::trace: a --> 5. >> During evaluation of In[3]:= Rule::trace: a->3 --> 5->3. >> During evaluation of In[3]:= Rule::trace: 5->3 --> 5->3. >> During evaluation of In[3]:= ReplaceAll::trace: a/.a->3 --> 5/.5->3. >> During evaluation of In[3]:= ReplaceAll::trace: 5/.5->3 --> 3. >> Out[3]= 3 In[4]:= (a+2)/.a->3 During evaluation of In[4]:= a::trace: a --> 5. >> During evaluation of In[4]:= Plus::trace: a+2 --> 5+2. >> During evaluation of In[4]:= Plus::trace: 5+2 --> 7. >> During evaluation of In[4]:= a::trace: a --> 5. >> During evaluation of In[4]:= Rule::trace: a->3 --> 5->3. >> During evaluation of In[4]:= Rule::trace: 5->3 --> 5->3. >> During evaluation of In[4]:= ReplaceAll::trace: a+2/.a->3 --> 7/.5->3. >> During evaluation of In[4]:= ReplaceAll::trace: 7/.5->3 --> 7. >> Out[4]= 7 In[5]:= Off[] Szabolcs P.S. This is why it is a bad idea to do things like sqrt[a_?NumericQ] := x /. FindRoot[x^2 == a, {x, 1}] (Even though this function works correctly almost all the time -- one must come up with a really tricky definition for 'x' to make it fail, like x = Sequence[1]) So use Module[] to localise 'x': sqrt[a_?NumericQ] := Module[{x}, x /. FindRoot[x^2 == a, {x, 1}]]
From: adriano.pascoletti on 13 Oct 2007 04:08 > I do not understand what Mathematica 6 is doing here: > > a=5 > a/.a->3 > > gives 3 > > But... > > a+2/. a->3 > > gives 7 > > Why is that? > > Thank you for your help > Being a=5 the statement a+2/. a->3 becomes 7/.5->3. You can check it with Trace: In[5]:= Trace[a + 2 /.a -> 3] Out[5]={{{a,5},5+2,7},{{a,5},5->3,5->3},7/.5->3,7} Adriano Pascoletti
From: Bill Rowe on 13 Oct 2007 04:17 On 10/12/07 at 2:59 AM, maderri2(a)gmail.com (magma) wrote: >I do not understand what Mathematica 6 is doing here: >a=5 >a/.a->3 >gives 3 >But... >a+2/. a->3 >gives 7 >Why is that? What is happening in each case is evaluation occurs before replacement. =46or a/.a-> evaluation causes this to become 5/.5->3 then the replacement occurs to give the result 3. =46or a+2/.a->3 evaluation gives 5+2/.5->3 Arithmetic operators such as + or - have higher precedence than patter replacement. So, this becomes 7/.5->3. And finally since there is no 5 to replace, the result is 7. You can easily verify this is the way things work by looking at the output from Trace[a/.a->3] and Trace[a+2/.a->3] -- To reply via email subtract one hundred and four
From: Szabolcs Horvát on 14 Oct 2007 04:18
magma wrote: > I do not understand what Mathematica 6 is doing here: > > a=5 > a/.a->3 > > gives 3 > > But... > > a+2/. a->3 > > gives 7 > > Why is that? > > Thank you for your help > Hi, Use Trance[a /. a->3] or On[] and Off[] to find out how Mathematica evaluates an expression. The following output should explain what happens: In[1]:= On[] During evaluation of In[1]:= On::trace: On[] --> Null. >> In[2]:= a=5 During evaluation of In[2]:= Set::trace: a=5 --> 5. >> Out[2]= 5 In[3]:= a/.a->3 During evaluation of In[3]:= a::trace: a --> 5. >> During evaluation of In[3]:= a::trace: a --> 5. >> During evaluation of In[3]:= Rule::trace: a->3 --> 5->3. >> During evaluation of In[3]:= Rule::trace: 5->3 --> 5->3. >> During evaluation of In[3]:= ReplaceAll::trace: a/.a->3 --> 5/.5->3. >> During evaluation of In[3]:= ReplaceAll::trace: 5/.5->3 --> 3. >> Out[3]= 3 In[4]:= (a+2)/.a->3 During evaluation of In[4]:= a::trace: a --> 5. >> During evaluation of In[4]:= Plus::trace: a+2 --> 5+2. >> During evaluation of In[4]:= Plus::trace: 5+2 --> 7. >> During evaluation of In[4]:= a::trace: a --> 5. >> During evaluation of In[4]:= Rule::trace: a->3 --> 5->3. >> During evaluation of In[4]:= Rule::trace: 5->3 --> 5->3. >> During evaluation of In[4]:= ReplaceAll::trace: a+2/.a->3 --> 7/.5->3. >> During evaluation of In[4]:= ReplaceAll::trace: 7/.5->3 --> 7. >> Out[4]= 7 In[5]:= Off[] Szabolcs P.S. This is why it is a bad idea to do things like sqrt[a_?NumericQ] := x /. FindRoot[x^2 == a, {x, 1}] (Even though this function works correctly almost all the time -- one must come up with a really tricky definition for 'x' to make it fail, like x = Sequence[1]) So use Module[] to localise 'x': sqrt[a_?NumericQ] := Module[{x}, x /. FindRoot[x^2 == a, {x, 1}]] |