为什么即使只使用一个规则和一个表达式, Replace 和 ReplaceAll 也会给出不同的结果? [英] Why do Replace and ReplaceAll give different results even when only one rule and one expression is used?

查看:21
本文介绍了为什么即使只使用一个规则和一个表达式, Replace 和 ReplaceAll 也会给出不同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许有人可以向我解释为什么 Replace 给出的答案与 ReplaceAll 不同,即使我使用的是一种规则,而且我认为我有一种表达方式.

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.

根据文档:

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

所以,ReplaceAll 完成了我预期的工作,但没有完成Replace.我认为我在这里缺少的唯一一件事是使 Parts 成为表达式的原因.所以也许 hz 不是一个"表达?好的,很好,但它应该能够至少将规则应用于其中的一部分.但它没有在任何地方做任何替换

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]]

hz 不是一种表达吗?如果没有,它有多少部分?是TreeForm[hz]生成的图末尾的叶子数吗?如果是这样,那么我会在其中看到 3 个z",然后 Replace[] 应该替换其中之一

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

我一直都是用/.,想都没想,和ReplaceAll是一样的,但是现在用了Replace[],想的是与 /. 相同,我发现了这个并注意到了不同的功能.(要学习的功能太多,时间太少:)

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

推荐答案

事实上,ReplaceReplaceAll相同的.您可以将 Replace 视为更精确的版本,因为使用 Replace 您可以指定表达式中您希望发生替换的级别.这里的区别有点类似于 MapMapAll 之间的区别,除了 ReplaceAll 不是深度优先(见下文)的微妙之处.另一个同样相当微妙的区别是 Replace 接受 Heads 选项,而 ReplaceAll 没有,这使得 Replace> 比 ReplaceAll 更精确.

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.

默认情况下,Replace在 0 级工作,即整个表达式.但是你的 z 更深:

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

如果使用Replace的级别规范,可以达到类似ReplaceAll的效果,但不一定相同:

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

Replace 与 lev.spec {0,Infinity}ReplaceAll 的区别在于,前者行为深度优先,子表达式之前的表达式,而后者则是从较大的表达式到它们的部分.它被更详细地讨论,例如此处.可以在 这篇 帖子.

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.

回到 Replace 的默认行为,它对整个表达式进行操作:当您只想转换整个表达式,而不是它的任何部分(可能会意外匹配模式)时,它非常有用在您的规则中).Replace 这种应用的一个例子是 这里.

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.

这篇关于为什么即使只使用一个规则和一个表达式, Replace 和 ReplaceAll 也会给出不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆