也许有人可以向我解释为什么Replace
给出了不同的答案ReplaceAll
,即使我使用一条规则,并且我认为我有一种表达方式。
根据文档:
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
和替换
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.
我有这样的表达:
z/(0.5 -1.4 z+z^2)
这一条规则
z -> Exp[I*w]
以下输入
Clear[z]
hz = z/(0.5 - 1.4 z + z^2);
Replace[hz, z -> Exp[I*w]]
ReplaceAll[hz, z -> Exp[I*w]]
产生这个结果:
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
做了我期望的工作,但没有Replace
。我认为我在这里唯一缺少的就是表达式的 Parts 的组成部分。那么也许 hz 不是“一”表达式?好的,很好,但是它应该能够至少将规则应用到其中的一部分。但它没有在任何地方进行任何替换
FullForm[hz]
Times[z,Power[Plus[0.5`,Times[-1.4`,z],Power[z,2]],-1]]
hz 不是一种表达方式吗?如果不是的话,它有多少个零件?是生成的图末尾的叶子数吗TreeForm[hz]
?如果是这样,那么我会在那里看到 3 个“z”,并且Replace[]
那么应该更换其中之一
我一直用/.
不假思索,这与ReplaceAll
,但现在我用了Replace[]
想法是一样的/.
,我发现了这个并注意到了不同的功能。
(需要学习的功能太多,时间却很少:)