Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #5068
| From | Heike Gramberg <heike.gramberg@gmail.com> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Replace & Rationalize |
| Date | 2011-09-19 11:04 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <j577k4$j2h$1@smc.vnet.net> (permalink) |
| 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]
>
>
>
Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread
Re: Replace & Rationalize Heike Gramberg <heike.gramberg@gmail.com> - 2011-09-19 11:04 +0000
csiph-web