Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: N[] does not work inside Replace[] ?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg53196] Re: [mg53185] N[] does not work inside Replace[] ?
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Tue, 28 Dec 2004 23:12:14 -0500 (EST)
  • Reply-to: hanlonr at cox.net
  • Sender: owner-wri-mathgroup at wolfram.com

Use RuleDelayed

s=Rule[a,10/3];

s//N

a -> 3.3333333333333335

s /. Rule[x_,y_] :> Rule[x,N[y]]

a -> 3.3333333333333335

b = Range[5]/3

{1/3, 2/3, 1, 4/3, 5/3}

b // N

{0.333333,0.666667,1.,1.33333,1.66667}

b /. x_Rational :> N[x]

{0.3333333333333333, 0.6666666666666666, 1, 
  1.3333333333333333, 1.6666666666666667}


Bob Hanlon

> 
> From: yehuda ben-shimol <benshimo at bgu.ac.il>
To: mathgroup at smc.vnet.net
> Date: 2004/12/28 Tue AM 06:30:32 EST
> To: mathgroup at smc.vnet.net
> Subject: [mg53196] [mg53185] N[] does not work inside Replace[] ?
> 
> 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
> 
> 


  • Prev by Date: Re: N[] does not work inside Replace[] ?
  • Next by Date: Re: shuffling 10^8 numbers
  • Previous by thread: Re: N[] does not work inside Replace[] ?
  • Next by thread: RealTime3D