Re: replace by value in expr
- To: mathgroup at yoda.ncsa.uiuc.edu (Mathematica)
- Subject: Re: replace by value in expr
- From: jacobson at cello.hpl.hp.com
- Date: Tue, 18 Dec 90 11:35:57 PST
Charles Herrick writes questioning the operation of substitution. The problem is that whenever x is defined *all* the instances of x, incuding those on the left of "->" get replaced by the value of x. I've annotated his list by what (I think) is happening at the time substitution takes place In[n]:= x = .; In[n+1]:= 1 + x^2 /. x -> 1+a (1+x^2)/.x->(1+a) Out[n+1]:= 1 + (1 + a)^2 In[n+2]:= x = 3; In[n+3]:= 1 + x^2 /. x -> 1+a 10/.3->(1+a) Out[n+3]:= 10 In[n+4]:= x = 5; In[n+5]:= 5 + x /. x -> 3 10/.(5->3) Out[n+5]:= 10 In[n+6]:= 5 + (x /. x -> 3) 5 + (5/.5->3) Out[n+6]:= 8 In[n+7]:= x = 3; In[n+8]:= 1 + (x^2 /. x -> 1+a) 1+(9/.3->1+a) Out[n+8]:= 10 So, its a feature. If you want the x on the left of "->" to not be replaced by the value of x *before substition* use ":>" instead. (Actually an x on the right won't get evaluated either, until after the substition.) -- David Jacobson