Mathematica:Grokking"NestWhileList"的"最大数量的评估"参数

Jan*_*nus 7 wolfram-mathematica

我在使用NestWhileList的情况下经常达到"最大评估次数".在得到一些古玩结果之后,我仔细研究了如何NestWhileList对指定的最大结果数作出反应:

Table[{nmax,
   Length@NestWhileList[
     (* f: nesting function *) Identity,
     (* initial state *) 1,
     (* test function *) False &,
     (* m: of arguments for test *) 1,
     (* nmax: max # applications of f *) nmax,
     (* n: extra evaluations *) 1]}, {nmax, 0, 2}];
ToString[TableForm[%, 
  TableHeadings -> {None, {"nmax", "output length"}}]]
Run Code Online (Sandbox Code Playgroud)

令人惊讶的部分是nmax=1单挑:这里f应用2次,而对于所有其他值,它只应用一次:

 nmax   output length
 0      2
 1      3
 2      2
Run Code Online (Sandbox Code Playgroud)

"额外评估"似乎是问题的一部分.离开该选项会产生更合理的结果:

Table[{nmax,
  Length@NestWhileList[
    (* f: nesting function *) Identity,
    (* initial state *) 1,
    (* test function *) False&,
    (* m: of arguments for test *) 1,
    (* max: max # applications of f *) nmax]},{nmax,0,2}];
ToString[TableForm[%,TableHeadings->{None, {"nmax","output length"}}]]

Out[123]=    
   nmax   output length
   0      1
   1      1
   2      1
Run Code Online (Sandbox Code Playgroud)

我的问题:这有什么意义,还是只是一个错误?

Mic*_*lat 4

这没有意义,而且我相当确定这只是一个错误。NestWhile也受到同样的困扰:

In[53]:= NestWhileList[# + 1 &, 1, False &, 1, 1, 1]

Out[53]= {1, 2, 3}

In[54]:= NestWhile[# + 1 &, 1, False &, 1, 1, 1]

Out[54]= 3
Run Code Online (Sandbox Code Playgroud)

这是一个解决方法函数NestWhileList

myNestWhileList[f_, expr_, test_, m_, max_, n_] :=
 Module[{nwl},
  nwl = NestWhileList[f, expr, test, m, max];
  Join[nwl, Rest[NestList[f, Last[nwl], n]]]
  ]

In[75]:= myNestWhileList[# + 1 &, 1, False &, 1, 1, 1]

Out[75]= {1, 2}
Run Code Online (Sandbox Code Playgroud)

显然,它并不是 的完全通用替代品NestWhileList,但如果需要的话,它应该很容易泛化。

我已经提交了错误报告。