mathematica 如何确定在替换中首先使用哪个规则
我想知道如果给定多个替换规则,mma 如何确定在发生碰撞时首先应用哪个规则。一个例子是:
x^3 + x^2*s + x^3*s^2 + s x /. {x -> 0, x^_?OddQ -> 2}
谢谢。
I am wondering if given multiple substitution rules, how does mma determine which to apply first in case of collision. An example is:
x^3 + x^2*s + x^3*s^2 + s x /. {x -> 0, x^_?OddQ -> 2}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Mathematica 有一种机制能够在简单情况下确定规则的相对通用性,例如,它理解
___
(BlankNullSequence) 比__
(BlankSequence) 更通用。因此,如果可以的话,它会根据它重新排序全局定义。但重要的是要认识到,这种分析必然主要是句法分析。因此,虽然有时可以分析PatternTest (?)
和Condition (/;)
以及一些简单的内置谓词(如EvenQ
),但使用它们使用用户定义的谓词必然会使类似定义的规则无法进行此类重新排序,因此 Mathematica 将按照输入的顺序保留这些规则。这是因为,PatternTest
和Condition
强制模式匹配器调用评估器来确定匹配的事实,这使得无法回答定义时的规则。即使对于纯粹的句法规则,也并不总是能够确定它们的相对通用性。因此,当无法做到这一点或 Mathematica 无法做到这一点时,它会按照输入的顺序保留规则。这一切都与全局规则有关,由
Set
或SetDelayed
或其他赋值运算符创建。对于本地规则,就像您的示例一样,没有任何重新排序,它们按照规则列表中的顺序应用。对于该子表达式和特定的规则应用过程,规则列表中除了应用于给定(子)表达式的第一个规则之外的所有规则都将被忽略 - (子)表达式根据第一个匹配规则进行重写,并且然后规则应用过程继续处理其他子表达式。即使重写(子)表达式的新形式与规则列表中更靠下的某些规则相匹配,它们也不会在此规则应用过程中应用。换句话说,对于单个规则应用过程,对于任何特定的(子)表达式,要么不应用规则,要么只应用一个规则。但这里也有一些微妙之处。例如,ReplaceAll (/.)
将较大表达式的规则应用于子表达式,而具有显式级别规范的Replace
则以相反的方式执行此操作。在这样的情况下这可能很重要:我在书中的几个地方提到了规则重新排序:这里、此处和此处。在极少数情况下,当 Mathematica 以不令人满意的方式重新排序规则时,您可以通过直接操作
DownValues
(或其他...值)来手动更改顺序,例如DownValues [f] = 反向[DownValues[f]]
。这种情况有时确实会发生,但很少发生,如果发生,请确保有充分的理由保留现有设计并进行手动规则重新排序。Mathematica has a mechanism which is able to determine the relative generality of the rules in simple cases, for example it understands that
___
(BlankNullSequence) is more general than__
(BlankSequence). So, when it can, it reorders global definitions according to it. It is important to realize though that such analysis is necessarily mostly syntactic. Therefore, whilePatternTest (?)
andCondition (/;)
with some simple built-in predicates likeEvenQ
can sometimes be analyzed, using them with user-defined predicates will necessarily make such reordering impossible with respect to similarly defined rules, so that Mathematica will keep such rules in the order they were entered. This is because,PatternTest
andCondition
force the pattern-matcher to call evaluator to determine the fact of the match, and this makes it impossible to answer the question of relative generality of rules at definition - time. Even for purely syntactic rules it is not always possible to determine their relative generality. So, when this can not be done, or Mathematica can not do it, it keeps the rules in the order they were entered.This all was about global rules, created by
Set
orSetDelayed
or other assignment operators. For local rules, like in your example, there is no reordering whatsoever, they are applied in the order they have in the list of rules. All the rules in the list of rules beyond the first one that applied to a given (sub)expression, are ignored for that subexpression and that particular rule-application process - the (sub)expression gets rewritten according to the first matching rule, and then the rule-application process continues with other sub-expressions. Even if the new form of the rewritten (sub) expression matches some rules further down the rule list, they are not applied in this rule-application process. In other words, for a single rule-application process, for any particular (sub) expression, either no rule or just one rule is applied. But here also there are a few subtleties. For example,ReplaceAll (/.)
applies rules from larger expressions to sub-expressions, whileReplace
with explicit level specification does it in the opposite way. This may matter in cases like this:I mentioned rule reordering in a few places in my book:here, here, and here. In rare cases when Mathematica does reorder rules in a way that is not satisfactory, you can change the order manually by direct manipulations with
DownValues
(or other ...Values), for example likeDownValues[f] = Reverse[DownValues[f]]
. Such cases do occur sometimes, but rather rarely, and if they happen, make sure there is a good reason to keep the existing design and go for manual rule reordering.