mathematica 如何确定在替换中首先使用哪个规则 [英] how does mathematica determine which rule to use first in substitution

查看:27
本文介绍了mathematica 如何确定在替换中首先使用哪个规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如果给定多个替换规则,mma 如何确定在发生碰撞时首先应用哪个.一个例子是:

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}

谢谢.

推荐答案

Mathematica 有一种机制可以确定规则在简单情况下的相对通用性,例如它理解 ___ (BlankNullSequence) 比 __ (BlankSequence) 更通用.因此,如果可以,它会根据它重新排序全局定义.重要的是要认识到,这种分析必然主要是句法.因此,虽然 PatternTest (?)Condition (/;) 带有一些简单的内置谓词,例如 EvenQ 有时可以分析,使用它们使用用户定义的谓词将必然使这种重新排序对于类似定义的规则变得不可能,因此 Mathematica 将按照它们输入的顺序保留这些规则.这是因为,PatternTestCondition 强制模式匹配器调用评估器来确定匹配的事实,这使得无法回答定义时的规则 - 时间.即使对于纯粹的句法规则,也不总是能够确定它们的相对普遍性.因此,当这无法完成,或者 Mathematica 无法完成时,它会按照输入的顺序保留规则.

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, while PatternTest (?) and Condition (/;) with some simple built-in predicates like EvenQ 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 and Condition 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.

这都是关于全局规则的,由 SetSetDelayed 或其他赋值运算符创建.对于本地规则,就像在您的示例中一样,没有任何重新排序,它们按照它们在规则列表中的顺序应用.除了应用于给定(子)表达式的第一个规则之外的规则列表中的所有规则,对于该子表达式和特定的规则应用过程都将被忽略——(子)表达式根据第一个匹配规则被重写,并且然后规则应用过程继续与其他子表达式.即使重写(子)表达式的新形式与规则列表更下方的某些规则相匹配,它们也不会应用于此规则应用过程.换句话说,对于单个规则应用过程,对于任何特定(子)表达式,要么不应用任何规则,要么只应用一个规则.但这里也有一些微妙之处.例如,ReplaceAll (/.) 将规则从较大的表达式应用于子表达式,而具有显式级别规范的 Replace 则以相反的方式进行.在这种情况下,这可能很重要:

This all was about global rules, created by Set or SetDelayed 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, while Replace with explicit level specification does it in the opposite way. This may matter in cases like this:

In[1]:= h[f[x, y]] /. {h[x_f] :> a, f[args__] :> b}

Out[0]= a

In[2]:= Replace[h[f[x, y]], {h[x_f] :> a, f[args__] :> b}, {0, Infinity}]

Out[2]= h[b]

我在书中的几个地方提到了规则重新排序:这里这里此处.在极少数情况下,当 Mathematica 以不令人满意的方式重新排序规则时,您可以通过使用 DownValues(或其他 ...Values)直接操作手动更改顺序,例如像 DownValues[f] = 反向[DownValues[f]].这种情况有时会发生,但很少发生,如果发生,请确保有充分的理由保留现有设计并进行手动规则重新排序.

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 like DownValues[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.

这篇关于mathematica 如何确定在替换中首先使用哪个规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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