MathGroup Archive 1999

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

Search the Archive

Re: Newbie question about rules

  • To: mathgroup at
  • Subject: [mg21367] Re: [mg21363] Newbie question about rules
  • From: Ken Levasseur <Kenneth_Levasseur at>
  • Date: Wed, 29 Dec 1999 23:18:41 -0500 (EST)
  • Organization: UMass Lowell Mathematical Sciences
  • References: <>
  • Sender: owner-wri-mathgroup at


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

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

  • Prev by Date: Re: Newbie question about rules
  • Next by Date: Problem with Display and ImageSize
  • Previous by thread: Newbie question about rules
  • Next by thread: Re: Newbie question about rules