Re: Replace and ReplaceAll -- simple application
- To: mathgroup at smc.vnet.net
- Subject: [mg105959] Re: Replace and ReplaceAll -- simple application
- From: AES <siegman at stanford.edu>
- Date: Mon, 28 Dec 2009 04:55:56 -0500 (EST)
- Organization: Stanford University
- References: <26882784.1261873319266.JavaMail.root@n11> <hh72e4$kuj$1@smc.vnet.net>
In article <hh72e4$kuj$1 at smc.vnet.net>, "David Park" <djmpark at comcast.net> wrote: > I don't know how you arrived at the initial expression, but if possible you > could try to enter only exact expressions (avoiding numbers with decimal > points, called approximate numbers). Enter approximate numbers only when you > are absolutely forced to. Try to keep all equations symbolic, free of > approximate numbers. Also keep them free of units. Then once you have solved > the equations (if you can do that) then substitute data values with > approximate numbers and units. After pondering this for a while, I'd agree that sounds like good advice. But, there are still lots of opportunities for confusion with "/." and rules. The definition of "/." says: ReplaceAll (Built-in Mathematica Symbol) expr /. rules applies a rule or list of rules in an attempt to transform each subpart of an expression expr. ReplaceAll looks at each part of expr, tries all the rules on it, and then goes on to the next part of expr. The first rule that applies to a particular part is used; no further rules are tried on that part, or on any of its subparts. So try In[1]:= 1./.{1.->1} Out[1]= 1 In[2]:= -1./.{1.->1} Out[2]= -1. In[3]:= a/.{a->b} Out[3]= b In[4]:= -a/.{a->b} Out[4]= -b Hmmm -- apparently "-1." is _one_ part (or subpart), but "-a" is _two_ subparts. I suppose the point is that "-1." is a number (and hence a single thing, or single part, that has to be matched), while "-a" is (somehow!) two things or two parts -- an operator, and a symbol? -- that are tested separately. Interestingly, a further test shows that "- 1." (note space) is also a single part in an expression -- and is in fact matched by "-1." (no space) in a rule. And "-(1.)" is also matched in the same way by "-1." (which seems an unfortunate behavior; writing it this way should clearly separate the operator "-" from the approximate number). These behaviors clearly account for my difficulties with rules involving I and -I. But if there's a formal or rigorous definition of "part" or "subpart" anywhere in the Mathematica documentation, I have yet to find it.