开发者

ReplaceAll not working as expected

开发者 https://www.devze.com 2023-02-01 12:41 出处:网络
Still early days with Mathematica so please forgive what is probably a very obvious question. I am trying to generate some parametric pl开发者_如何学Pythonots. I have:

Still early days with Mathematica so please forgive what is probably a very obvious question. I am trying to generate some parametric pl开发者_如何学Pythonots. 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.


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})


The best way for using local variables in Mathematica is Module[]:

Module[{a = 2, b = 1, h = 1}, 
   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]]

This way a, b, and h do not get assigned values in the Global context but only inside the Module. If you still want to use replacement rules you just have to ReleaseHold after you have done the replacement:

ReleaseHold[
   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}]

EDIT: As to why this happens. The way I understand it, HoldAll prevents the arguments of the function from being modified by any rules (internal or explicit). What your Hold does, is place the entire function on hold (not just the arguments), and the replacement rule gets applied after the function has gone through evaluation (which it didn't so there is still something there to replace) and HoldAll is no longer valid.

In[1]  := Hold[a /. a -> 5]
Out[1] := Hold[a /. a -> 5]
In[2]  := Hold[a] /. a -> 5
Out[2] := Hold[5]

Of course, Hold also has HoldAll as an attribute, so this doen't explain why ParametricPlot's HoldAll is different. :-(

EDIT2: I used Trace to look at what happens, and it seems like ReplaceAll gets applied only at the very end, when ParametricPlot has already turned into a graphical object (and does not contain a, b, or h anymore). In the case of Hold[a] /. a -> 5 the hold evaluates to Hold[a] and the replacement rule can then be successfully applied.


That is the way ReplaceAll always work.

See for example:

In[10]:= (a/a) /. a -> 0

Out[10]= 1  

Clearly the replacement is done AFTER the evaluation, because if you do:

In[11]:= a = 0; a/a

During evaluation of In[11]:= Power::infy: Infinite expression 1/0 encountered. >>

During evaluation of In[11]:= Infinity::indet: Indeterminate expression 0 ComplexInfinity encountered. >>

Out[12]= Indeterminate  

Now, is a matter of inserting the replacement at the level you want it to operate. As the result of a Plot is basically an Image with the numeric coordinates already "solved", you want to put those coordinates in before the plot is calculated. In your case:

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

 PlotRange -> All
]

ReplaceAll not working as expected


This is not an answer as such, just a comment on using Module with Plot.

If I proceed as follows

f[t_] := {(a + b) Cos[t] - h Cos[(a + b)/b t], (a + b) Sin[t] - 
   h Sin[(a + b)/b t]}

The following will NOT work

Method 1:

Module[{a = 2, b = 1, h = 1}, 
 ParametricPlot[f[t], {t, 0, 2 \[Pi]}, PlotRange -> All]]

Method 2:

Module[{a = 2, b = 1, h = 1}, 
 ParametricPlot[Evaluate[f[t]], {t, 0, 2 \[Pi]}, PlotRange -> All]]

The following does work (Method 3)

ParametricPlot[
 Module[{a = 2, b = 1, h = 1}, Evaluate[f[t]]], {t, 0, 2 \[Pi]}, 
 PlotRange -> All]

as does the method described above (method 4)

Module[{a = 2, b = 1, h = 1}, 
   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]]

Can anyone explain why method 4 works but method 2 doesn't? (The same applies to With, which I find more intuitive to Module).

For what its worth, I would generate the original parametric plot using replacement rules as follows:

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

EDIT

f[x_] := (a x)/(b + x);
With[{a = 10, b = 100}, Plot[Evaluate[f[x]], {x, 0, 100}]]
With[{a = 10, b = 100}, Plot[(a x)/(b + x), {x, 0, 100}]]
Plot[With[{a = 10, b = 100}, Evaluate[f[x]]], {x, 0, 100}]
Plot[Evaluate[f[x]] /. {a -> 10, b -> 100}, {x, 0, 100}]

Method 1 (of Edit) does not work (because 'Plot' treats the variable x as local, effectively using Block'?)

It seems to me that it is absolutely clear to anyone, even those with a rudimentary knowledge of Mathematica, what is going on with Method 2, showing the power and ease-of-use of Mathematica. When the equations become more complex, is it advantageous to define them separately. It is now not so clear that Method 3 must be used instead of Method 1. (Method 4, of course, is probably the best of all.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号