Re: Newbie question about rules
- To: mathgroup at smc.vnet.net
- Subject: [mg21367] Re: [mg21363] Newbie question about rules
- From: Ken Levasseur <Kenneth_Levasseur at uml.edu>
- Date: Wed, 29 Dec 1999 23:18:41 -0500 (EST)
- Organization: UMass Lowell Mathematical Sciences
- References: <199912291915.OAA03067@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Doug:
What is happening is that exp is evaluated to produce a graphics object
and so the rule doesn't do anything.
You might think that this would work:
Plot[Sin[x] /. Sin -> Cos, {x, 0, 2 Pi}]
but it doesn't for a different reason. Plot has the HoldFirst attribute
and so (Sin[x] /. Sin -> Cos) is left as is until a number is
substituted for x in the process of plotting, then for example if x= 0,
the expression turns into (Sin[0] /. Sin -> Cos) which becomes (0 /. Sin
-> Cos) or 0. So you still get a plot of Sin[x]/
What you had in mind could be done this way, overriding the HoldFirst
attribute with Evaluate:
Plot[Evaluate[Sin[x] /. Sin -> Cos], {x, 0, 2 Pi}]
Another way would be to go back to your original expession but wrap the
Plot expression with Hold to get the rule to be applied
ReleaseHold[Hold[Plot[Sin[x], {x, 0, 2 Pi}]] /. Sin -> Cos]
However, you can't "name" your Plot expression and accomplish the same
effect since exp itself is held. So this won't work:
ReleaseHold[Hold[exp] /. Sin -> Cos]
Hope this helps.
Ken Levasseur
UMass Lowell
http://www.uml.edu/Dept/Math/LevasseuK.html
Doug Nelson wrote:
>
> Hello All,
>
> I just received my copy of Mathematica 4.0 and I've been reading Mastering
> Mathematica.
>
> I'm trying to apply a rule that changes "Sin" to "Cos" in an expression. It
> seems to work fine with normal expressions, but not with a Plot function.
>
> This works:
> exp2 := Abcfunc[Sin[x]]
> exp2 /. Sin -> Cos
> out = Abcfunc[Cos[x]]
>
> However, the following gives me a sine wave plot in both cases:
>
> exp := Plot[Sin[x], {x,0,2 Pi}]
> exp
> out = < a sine wave plot >
>
> exp /. Sin -> Cos
> out = < another sine wave plot ! >
>
> Why don't I get a cosine plot?
>
> Thanks in advance,
> Doug
- References:
- Newbie question about rules
- From: dougn_NOspam@gte.net (Doug Nelson)
- Newbie question about rules