MathGroup Archive 2011

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

Search the Archive

Re: Replace & Rationalize

  • To: mathgroup at smc.vnet.net
  • Subject: [mg121538] Re: Replace & Rationalize
  • From: Heike Gramberg <heike.gramberg at gmail.com>
  • Date: Mon, 19 Sep 2011 07:04:12 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201109180810.EAA06285@smc.vnet.net>

This is because you're with Rule the expression on the right-hand side 
is evaluated
before the Rule is applied. In this case, Rationalize[x]  is evaluated 
first, returning x,
before the value for x is substituted, so effectively you're evaluating

{0., 1.} /. x_ /; IntegerQ[Rationalize[x]] -> x

which just returns the unrationalized values for x.

To solve this you should use RuleDelayed (:>) which works similar to 
SetDelayed in that
the rhs isn't evaluated until after the rule is applied, i.e.

{0., 1.} /. (x_ /; IntegerQ[Rationalize[x]]) :> Rationalize[x]

which returns

{0, 1}

Heike.

On 18 Sep 2011, at 10:10, Chris Degnen wrote:

> Further to a question on Stack Overflow I noticed a puzzling
> evaluation.  Why does the second expression generate reals?
>
> (x \[Function]
>   If[IntegerQ[Rationalize[x]], Rationalize[x], x]) /@ {0., 1.}
>
> {0., 1.} /. x_ /; IntegerQ[Rationalize[x]] -> Rationalize[x]
>
>
>





  • Prev by Date: Re: output forms
  • Next by Date: Re: How create new style sheet- Mathematica 8
  • Previous by thread: Re: Replace & Rationalize
  • Next by thread: Plotting 3D Inequality with conditions for variable