系数功能慢 [英] Coefficient function is slow
问题描述
请考虑:
Clear[x]
expr = Sum[x^i, {i, 15}]^30;
CoefficientList[expr, x]; // Timing
Coefficient[Expand@expr, x, 234]; // Timing
Coefficient[expr, x, 234]; // Timing
{0.047, Null}
{0.047, Null}
{4.93, Null}
帮助状态:
不管是否以扩展形式显式给出expr,
Coefficient
都可以工作.
Coefficient
works whether or not expr is explicitly given in expanded form.
为什么在最后一种情况下Coefficient
需要这么慢?
Is there a reason why Coefficient
needs to be so slow in the last case?
推荐答案
Coefficient
除非绝对必要,否则不会扩展.这确实可以避免内存爆炸.我相信从版本3开始就采用这种方式(我想我是在1995年左右开始研究它的.)
Coefficient
will not expand unless it deems it absolutely necessary to do so. This does indeed avoid memory explosions. I believe it has been this way since version 3 (I think I was working on it around 1995 or so).
也可以更快地避免扩展.这是一个简单的例子.
It can also be faster to avoid expansion. Here is a simple example.
In[28]:= expr = Sum[x^i + y^j + z^k, {i, 15}, {j, 10}, {k, 20}]^20;
In[29]:= Coefficient[expr, x, 234]; // Timing
Out[29]= {0.81, Null}
但是下一个似乎挂在版本8中,并且在开发Mathematica(更改了Expand
的地方)中至少花费了半分钟.
But this next appears to hang in version 8, and takes at least a half minute in the development Mathematica (where Expand
was changed).
Coefficient[Expand[expr], x, 234]; // Timing
可能应该添加一些试探法来寻找不会爆炸的单变量.不过,这似乎不是一个高优先级的项目.
Possibly some heuristics should be added to look for univariates that will not explode. Does not seem like a high priority item though.
丹尼尔·里奇布劳
这篇关于系数功能慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!