Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReplaceAll not working as expected

Still early days with Mathematica so please forgive what is probably a very obvious question. I am trying to generate some parametric plots. I have:

ParametricPlot[{
    (a + b) Cos[t] - h Cos[(a + b)/b t],
    (a + b) Sin[t] - h Sin[(a + b)/b t]},
    {t, 0, 2 \[Pi]}, PlotRange -> All] /. {a -> 2, b -> 1, h -> 1}

No joy: the replacement rules are not applied and a, b and h remain undefined.

If I instead do:

Hold@ParametricPlot[{
    (a + b) Cos[t] - h Cos[(a + b)/b t],
    (a + b) Sin[t] - h Sin[(a + b)/b t]},
    {t, 0, 2 \[Pi]}, PlotRange -> All] /. {a -> 2, b -> 1, h -> 1}

it looks like the rules ARE working, as confirmed by the output:

Hold[ParametricPlot[{(2 + 1) Cos[t] - 
1 Cos[(2 + 1) t], (2 + 1) Sin[t] - 1 Sin[(2 + 1) t]}, {t, 0, 
2 \[Pi]}, PlotRange -> All]]

Which is what I'd expect. Take the Hold off, though, and the ParametricPlot doesn't work. There's nothing wrong with the equations or the ParametricPlot itself, though, because I tried setting values for a, b and h in a separate expression (a=2; b=1; h=1) and I get my pretty double cardoid out as expected.

So, what am I doing wrong with ReplaceAll and why are the transformation rules not working? This is another fundamentally important aspect of MMA that my OOP-ruined brain isn't understanding.

I tried reading up on ReplaceAll and ParametricPlot and the closest clue I found was that "ParametricPlot has attribute HoldAll and evaluates f only after assigning specific numerical values to variables" which didn't help much or I wouldn't be here.

Thanks.

like image 221
Tim Avatar asked Dec 22 '22 19:12

Tim


1 Answers

Mathematica evaluates each head without holding attributes by first evaluating head of each subexpression. Since ReplaceAll doesn't have holding attributes, ParametricPlot becomes Graphics before replacement

To see the expression tree, do

ParametricPlot[{(a + b) Cos[t] - h Cos[(a + b)/b t], (a + b) Sin[t] - 
      h Sin[(a + b)/b t]}, {t, 0, 2 \[Pi]}, 
    PlotRange -> All] /. {a -> 2, b -> 1, h -> 1} // Hold // TreeForm

From that tree you can see that your command is the same as doing

temp1=ParametricPlot[{(a + b) Cos[t] - h Cos[(a + b)/b t], (a + b) Sin[t] - 
          h Sin[(a + b)/b t]}, {t, 0, 2 \[Pi]}, 
        PlotRange -> All]
temp2={a -> 2, b -> 1, h -> 1} 
temp1/.temp2

Look at FullForm[temp1] to confirm that there's no a or b in that expression.

If you set ReplaceAll to HoldFirst, that prevents ParametricPlot from being evaluated before ReplaceAll, and result is what you expected. In this case, ReplaceAll evaluates to expression with head ParametricPlot, and only at that point ParametricPlot is evaluated. Make sure to reset the attributes back because changing behavior of built-in commands can have unexpected side-effects.

SetAttributes[ReplaceAll, HoldFirst]; 
ParametricPlot[{(a + b) Cos[t] - h Cos[(a + b)/b t], (a + b) Sin[t] - 
    h Sin[(a + b)/b t]}, {t, 0, 2 \[Pi]}, 
  PlotRange -> All] /. {a -> 2, b -> 1, h -> 1}
ClearAttributes[ReplaceAll, HoldFirst]

A useful trick when needing to evaluate arguments passed to function with HoldAll is to do operations on an expression with List head, and substitute ParametricPlot in the end, for instance

ParametricPlot @@ ({{(a + b) Cos[t] - 
      h Cos[(a + b)/b t], (a + b) Sin[t] - h Sin[(a + b)/b t]}, {t, 0,
      2 \[Pi]}, PlotRange -> All} /. {a -> 2, b -> 1, h -> 1})
like image 87
Yaroslav Bulatov Avatar answered Dec 29 '22 07:12

Yaroslav Bulatov