Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #5068 > unrolled thread

Re: Replace & Rationalize

Started byHeike Gramberg <heike.gramberg@gmail.com>
First post2011-09-19 11:04 +0000
Last post2011-09-19 11:04 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Replace & Rationalize Heike Gramberg <heike.gramberg@gmail.com> - 2011-09-19 11:04 +0000

#5068 — Re: Replace & Rationalize

FromHeike Gramberg <heike.gramberg@gmail.com>
Date2011-09-19 11:04 +0000
SubjectRe: Replace & Rationalize
Message-ID<j577k4$j2h$1@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]
>
>
>

[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web