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 expression.
According to the documentation:
ReplaceAll looks at each part of expr, tries all the rules on it, and then goes on to
the next part of expr. The first rule that applies to a particular part is used; no
further rules are tried on that part, or on any of its subparts
and for Replace
A list of rules can be given. The rules are tried in order. The result of the first
one that applies is returned. If none of the rules apply, the original expr is
returned.
I have this expression:
z/(0.5 -1.4 z+z^2)
and this one rule
z -> Exp[I*w]
The following input
Clear[z]
hz = z/(0.5 - 1.4 z + z^2);
Replace[hz, z -> Exp[I*w]]
ReplaceAll[hz, z -> Exp[I*w]]
yields this result:
z/(0.5 -1.4 z+z^2) (*from Replace*)
E^(I w)/(0.5 -1.4 E^(I w)+E^(2 I w)) (*from ReplaceAll*)
So, ReplaceAll
did the job I expected, but not Replace
. The only thing I think I am missing here, is what makes Parts of an expression. So maybe hz is not 'one' expression? Ok, fine, but it should then have been able to apply the rule at least to one part of it. But it did not do any replacement anywhere
FullForm[hz]
Times[z,Power[Plus[0.5`,Times[-1.4`,z],Power[z,2]],-1]]
Isn't hz one expression? If not, how many parts does it have? Is it the number of leaves at the end of the graph generated by TreeForm[hz]
? If so, then I see 3 'z' in there, and Replace[]
should then have replaced one of them
I always used /.
without thinking about it, which is the same as ReplaceAll
, but now I used Replace[]
thinking is was the same as /.
, I found this and noticed the different functions.
(so many functions to learn, so little time :)