Why do Replace and ReplaceAll give different results even when only one rule and one expression is used?

前端 未结 2 463
南旧
南旧 2021-02-08 02:06

Maybe someone can explain to me why Replace gives a different answer than ReplaceAll, even though I am using one rule, and, I think, I have one express

相关标签:
2条回答
  • 2021-02-08 02:36

    I think the relevant bit of the documentation for Replace is: "applies a rule or list of rules in an attempt to transform the entire expression expr. " i.e., to transform the entire expression. Thus,

    Replace[z, z -> Exp[I*w]]
    

    would transform z to Exp[I*w], but your example fails because the rule does not match the entire expression.

    Note that Replace takes an argument level spec; so to operate on the end leaves of your tree, try

    Replace[hz, z -> Exp[I*w], -1]
    

    giving

    E^(I w)/(0.5 -1.4 E^(I w)+E^(2 I w))
    
    0 讨论(0)
  • 2021-02-08 02:47

    In fact, Replace and ReplaceAll are not the same. You can think of Replace as a more precise version, since with Replace you can specify the levels in expression on which you want the replacements to happen. The difference here is somewhat similar to that between Map and MapAll, except the subtlety that ReplaceAll does not work depth-first (see below). Another difference, also rather subtle, is that Replace takes the Heads option, while ReplaceAll does not, which makes Replace yet more precise than ReplaceAll.

    By default, Replace works only at level 0, which is, entire expression. Your z is deeper however:

    In[220]:= Clear[z]
    hz = z/(0.5 - 1.4 z + z^2);
    
    
    Position[hz, z]
    
    Out[222]= {{1}, {2, 1, 2, 2}, {2, 1, 3, 1}}
    

    If you use the level specification for Replace, you can achieve an effect similar to, but not always the same as that of ReplaceAll:

    In[223]:= Replace[hz,z->Exp[I*w],{0,Infinity}]
    
    Out[223]= E^(I w)/(0.5\[VeryThinSpace]-1.4 E^(I w)+E^(2 I w))
    

    The difference between Replace with lev.spec {0,Infinity} and ReplaceAll is that the former acts depth-first, sub-expressions before expressions, while the latter works from larger expressions to their parts. It is discussed in more detail e.g. here. One example where this difference was used to one's advantage can be found in this post.

    Coming back to default behavior of Replace, which operates on entire expression: it is very useful when you want to transform only the entire expression, but none of its parts (which may accidentally match the pattern in your rule). One example of such application of Replace is here.

    0 讨论(0)
提交回复
热议问题