English 中文(简体)
利用Fold计算线性复发的结果,依赖以往的多重价值
原标题:Using Fold to calculate the result of linear recurrence relying on multiple previous values

我有一个直线复发的问题,即下一个因素不仅仅取决于先前的价值,例如菲博托切合顺序。 计算Nth要素的一种方法就是通过功能电话加以界定,例如。

Fibonacci[0] = 0; Fibonacci[1] = 1;
Fibonacci[n_Integer?Positive] := Fibonacci[n] + Fibonacci[n - 1]

就我工作的顺序而言,这正是我的工作。 (定义载于<代码>Module,因此我不污染Global'。) 然而,我将用2个<>10- 213点来利用这一系统,因此,我担心,如果我刚才需要最后任期,我就没有事先的内容。 我愿使用<代码>Fold来做这项工作,但<代码>Fold只读到眼前的结局,这意味着它不能直接用于处理一般线性重复问题。

我与取代<代码>Fold 和FoldList的职能一样,通过特定数量的事先顺序要素,即职能。

In[1] := MultiFoldList[f, {1,2}, {3,4,5}] (* for lack of a better name *)
Out[1]:= {1, 2, f[3,2,1], f[4,f[3,2,1],2], f[5,f[4,f[3,2,1],2],f[3,2,1]]}

我这样做了,但我在保存该笔记本之前就关闭了该笔记本。 因此,如果我自己改写,我就把它放在后面。

www.un.org/chinese/ga/president 我的具体问题是:Im 履行http://books.google.com/books?id=kfNQAAMAAJ&q=inauthor%3a%22 George+Allen+Baker 2022&dq=inauthor%3a%22 George+Allen+Baker%22&hl=en&ei=lJl9TezYO0lQeFifHBQ&sa=Xlow&;i=subo=Q_res&d 创建近似值的一部分是产生一套系数,即:,即另一种关系,然后纳入最终关系。

A[n+1]== A[n] + (z - z[[n]]) a[[n+1]] A[n-1]

至少我可以看到的是<代码>RSolve或MatrixPower不能使用。

最佳回答

rel=“nofollow”>RecurrenceTable执行这项任务?

根据前两个价值观重新确定第1 000任期:

In[1]:= RecurrenceTable[{a[n] == a[n - 1] + a[n - 2], 
  a[1] == a[2] == 1}, a, 
   {n, {1000}}]

Out[1]= {4346655768693745643568852767504062580256466051737178040248172
9089536555417949051890403879840079255169295922593080322634775209689623
2398733224711616429964409065331879382989696499285160037044761377951668
49228875}

Edit: If your recurrence is defined by a function f[m, n] that doesn t like to get evaluated for non-numeric m and n, then you could use Condition:

In[2]:= f[m_, n_] /; IntegerQ[m] && IntegerQ[n] := m + n

<代码>f的复读表:

In[3]:= RecurrenceTable[{a[n] == f[a[n - 1], a[n - 2]], 
  a[1] == a[2] == 1}, a, {n, {1000}}]

Out[3]= {4346655768693745643568852767504062580256466051737178040248172
9089536555417949051890403879840079255169295922593080322634775209689623
2398733224711616429964409065331879382989696499285160037044761377951668
49228875}
问题回答

A multiple foldlist can be useful but it would not be an efficient way to get linear recurrences evaluated for large inputs. A couple of alternatives are to use RSolve or matrix powers times a vector of initial values.

此处采用这些方法,例如,如果其任期等于一至二分之二。

f[n_] =  f[n] /. RSolve[{f[n] == f[n - 1] + 2*f[n - 2], f[1] == 1, f[2] == 1},
  f[n], n][[1]]

Out [67]= 1/3 (-(-1)^n + 2^n)

f2[n_Integer] := Last[MatrixPower[{{0, 1}, {2, 1}}, n - 2].{1, 1}]

{f[11], f2[11]}

Out [79]={683, 683}

Daniel Lichtblau Wolfram Research

几乎是vol,但你可以使用<代码>的副作用。 Nest/代码

fibo[n_] := 
  Module[{i = 1, s = 1}, 
   NestWhileList[ s &, 1, (s = Total[{##}]; ++i < n) &, 2]];  

业绩不佳:

In[153]:= First@Timing@fibo[10000]
Out[153]= 0.235  

通过将最后的<代码>2改为任何分类,您可将最后的千分之结果传递给您(此处为“总数”)。

<代码>Linear Recurrence和RecurrenceTable非常有用。

对于小型油轮来说,丹尼尔提出的<条码>Matrix Power方法最快。

对于某些问题,这些问题可能无法适用,而且你可能需要自行推出。

我将使用 Nest公司,因为我认为这个问题是适当的,但与Fold公司也可以使用类似的建筑。

一个具体的例子,即Fibonacci序列。 这可能不是最清洁的,但我认为,在我继续的时候,你会看到这种效用。

fib[n_] :=
  First@Nest[{##2, # + #2} & @@ # &, {1, 1}, n - 1]

fib[15]

Fibonacci[15]

在此,我使用<代码>Apply(@),以便我能够用<代码>#、#2等,而不是#[l]等处理内容。 I use SlotSequence to reduce the first elements from the original list, and 序号同时列入新名单。

如果你将一劳永逸地在整个名单上运作,那么就有一个简单的<代码>。 适用[Rest@#, ......可能更好。 这两种方法都很容易普及。 例如,简单的线性重复实施就是

 lr[a_, b_, n_Integer] := First@Nest[Append[Rest@#, a.#] &, b, n - 1]

 lr[{1,1}, {1,1}, 15]

(斜体与<代码>中建筑的反向。) Linear Recurrence





相关问题
Solving vector equations in Mathematica

I m trying to figure out how to use Mathematica to solve systems of equations where some of the variables and coefficients are vectors. A simple example would be something like where I know A, V, and ...

Introspection of messages generated in Mathematica

Is there any way to get at the actual messages generated during the evaluation of an expression in Mathematica? Say I m numerically solving an ODE and it blows up, like so In[1] := sol = NDSolve[{x [...

Connecting points in Mathematica

I have a collection of points displayed in a graphic: alt text http://img69.imageshack.us/img69/874/plc1k1lrqynuyshgrdegvfy.jpg I d like to know if there is any command that will connect them ...

Mathematica, PDF Curves and Shading

I need to plot a normal distribution and then shade some specific region of it. Right now I m doing this by creating a plot of the distribution and overlaying it with a RegionPlot. This is pretty ...

Targeted Simplify in Mathematica

I generate very long and complex analytic expressions of the general form: (...something not so complex...)(...ditto...)(...ditto...)...lots... When I try to use Simplify, Mathematica grinds to a ...

热门标签