Mathematica 执行时错误:符号名称 [英] Mathematica execution-time bug: symbol names

查看:44
本文介绍了Mathematica 执行时错误:符号名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mathematica 中有一个奇怪的错误已经存在多年,至少从 5.1 版开始,并且一直持续到 7 版.

There is a strange bug that has been in Mathematica for years, at least since version 5.1, and persisting through version 7.

Module[{f, L}, L = f[];
  Do[L = f[L, i], {i, 10^4}]] // Timing

  {0.015, Null}

Module[{weirdness, L}, L = weirdness[];
  Do[L = weirdness[L, i], {i, 10^4}]] // Timing

  {2.266, Null}

<小时>

  • 这是什么原因?是散列问题吗?


    • What causes this? Is it a hashing problem?

      它在第 8 版中修复了吗?

      Is it fixed in Version 8?

      除了测试之外,有没有办法知道哪些符号名称会导致速度变慢?

      Is there a way to know what symbol names cause a slowdown, other than testing?

      推荐答案

      这是什么原因造成的?是哈希问题吗?

      What causes this? Is it a hashing problem?

      是的,或多或少.

      它在第 8 版中修复了吗?

      Is it fixed in Version 8?

      是的(也或多或少).也就是说,不可能在任何完整"意义上进行修复.但最常见的情况处理得更好.

      Yes (also more or less). That is to say, it is not possible to fix in any "complete" sense. But the most common cases are much better handled.

      除了测试之外,有没有办法知道哪些符号名称会导致速度变慢?

      Is there a way to know what symbol names cause a slowdown, other than testing?

      我不知道.

      在第 7 版中,有一个与第 8 版中的修复类似的早期修复.默认情况下它是关闭的(我们在发布时没有足够的时间来测试它,并且它没有被打开7.0.1 版).可以通过以下方式访问.

      In version 7 there is an earlier fix of a similar nature to the one in version 8. It was off by default (we'd not had adequate time to test it when we shipped, and it did not get turned on for version 7.0.1). It can be accessed as follows.

      SetSystemOptions["NeedNotReevaluateOptions"->{"UseSymbolLists"->True}];
      

      这让你的例子回到了合理的领域.

      This brings your example back to the realm of the reasonable.

      Module[{weirdness, L}, L = weirdness[];
        Do[L = weirdness[L, i], {i, 10^4}]] // Timing
      

      输出[8]= {0.020997,空}

      Out[8]= {0.020997, Null}

      ---编辑---

      我可以更详细地解释这里涉及的优化.首先回想一下 Mathematica 模拟无限求值",即表达式不断求值直到它们不再改变.这可能代价高昂,因此需要仔细优化短路以在可能的情况下阻止它.

      I can explain the optimization involved here in slightly more detail. First recall that Mathematica emulates "infinite evaluation", that is, expressions keep evaluating until they no longer change. This can be costly and hence requires careful short circuit optimizations to forestall it when possible.

      我们使用的一种机制是散列的一种变体,用于指示表达式可能依赖的符号未更改,因此该表达式未更改.在这里可能会发生碰撞,因此需要做更多的工作.

      A mechanism we use is a variant of hashing, that serves to indicate that symbols on which an expression might depend are unchanged and hence that expression is unchanged. It is here that collisions might occur, thus necessitating more work.

      在糟糕的情况下,Mathematica 内核可能需要遍历整个表达式,以确定它没有改变.这种步行可能与重新评估一样昂贵.版本 7 的新优化(如上所述)是为某些类型的表达式明确记录它所依赖的那些符号.然后可以通过简单地检查自上次评估表达式以来这些符号都没有更改来缩短重新评估检查.

      In a bad case, the Mathematica kernel might need to walk the entire expression in order to determine that it is unchanged. This walk can be as costly as reevaluation. An optimization, new to version 7 (noted above), is to record explicitly, for some types of expression, those symbols upon which it depends. Then the reevaluation check can be shortened by simply checking that none of these symbols has been changed since the last time the expression was evaluated.

      实现细节有点复杂(也有点专有,虽然可能不那么难猜).但简而言之,这就是幕后发生的事情.早期版本有时会进行重要的表达式遍历,只是为了发现表达式不需要重新评估.这种情况仍然可能发生,但现在这种情况更为罕见.

      The implementation details are a bit involved (and also a bit proprietary, though perhaps not so hard to guess). But that, in brief, is what is going on under the hood. Earlier versions sometimes did significant expression traversal just to discover that the expression needed no reevaluation. This can still happen, but it is a much more rare event now.

      ---结束编辑---

      丹尼尔·利希布劳Wolfram 研究

      Daniel Lichtblau Wolfram Research

      这篇关于Mathematica 执行时错误:符号名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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