Re: N[] does not work inside Replace[] ?
- To: mathgroup at smc.vnet.net
- Subject: [mg53189] Re: N[] does not work inside Replace[] ?
- From: David Bailey <dave at Remove_Thisdbailey.co.uk>
- Date: Tue, 28 Dec 2004 23:12:05 -0500 (EST)
- References: <200412241059.FAA05903@smc.vnet.net> <200412271141.GAA12681@smc.vnet.net> <cqrh2u$qi5$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
yehuda ben-shimol wrote: > Hi, > I just found a small but annoying problem with operating N[] inside a > replace rule > s was actually part of an answer I got from Solve[] and I wanted to > leave the Rule, but change the right hand side to a machine real number. > A reduced form of the problem is given below. I'm using version 5.0.0 > (ver 5.1 is on the way ... ) > > s = Rule[a, 10/3] > and the output is > a->10/3 > > case 1: > now use a replace rule with N[] > s /. Rule[x_, y_] -> Rule[x, N[y]] > and the result is still a->10/3 > > case 2: > using some other header in place of N[] > s /. Rule[x_, y_] -> Rule[x, g[y]] > and the results is as expected > a->g[10/3] > > Trying to convert the second argument to a machine real number in > different way > > case 3: > s /. Rule[x_, y_] -> Rule[x, 1. y] > results with > a -> 3.33333 > as expected. > > > Using Trace[] for the three cases reveals that N[y] is replaced by y > very early in the evaluation process, so it explains this unexpected > behavior. > > I tried then a simpler example > b=Range[5]/3 > and then tried the same replacement idea > b/.x_Rational->N[x] > and still the results leave the rational numbers unchanged > > but with the b/.x_Rational->g[x] or b/.x_Rational->1.0 * x Mathematica > works as expected. > > So, again, is this a bug? If it is so in version 5.0.0, does it still > exists in version 5.1? > > Thanks in advanced > yehuda > Hi, There is no bug here - if you use -> (Rule) an evaluation happens before the rule is applied - so N gets applied to y and leaves y. Obviously, an arbitrary function g without a definition stays as g[y]. You should use :> (RuleDelayed) {x -> 10/3} /. Rule[a_,b_] :> Rule[a,N[b]] {x -> 3.33333} Although there are a few obscure bugs in Mathematica, it is wise to assume the fault is yours until you know the system very well! David Bailey dbaileyconsultancy.co.uk
- References:
- Mathematica language issues
- From: Maxim <ab_def@prontomail.com>
- Re: Mathematica language issues
- From: DrBob <drbob@bigfoot.com>
- Mathematica language issues