I have a linear recurrence problem when the next element relies on more than just the previous value, for example. Fibonacci sequence. One method computing the element n th is to determine it by calling a function, for example.
Fibonacci[0] = 0; Fibonacci[1] = 1; Fibonacci[n_Integer?Positive] := Fibonacci[n] + Fibonacci[n - 1]
and for the sequence I'm working with, this is exactly what I'm doing. (The definition is inside a Module , so I donβt pollute Global` .) However, I am going to use this with 2 10 - 2 13 so I worry about the extra overhead when I just need the last term and none of the previous elements. I would like to use Fold to do this, but Fold only conveys directly the previous result, which means that it is not directly useful for the general linear recursion problem.
I need a couple of functions to replace Fold and FoldList , which pass a certain number of elements of the previous sequence to the function, i.e.
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]]}
I had something that did this, but I closed the notebook before saving it. So, if I rewrote it myself, I will send it.
Edit : why am I not using RSolve or MatrixPower to solve this problem. My specific problem is that I am performing an n-point Pade approximant to analytically continue the function that I know only at a given number of points on the imaginary axis, {z i }. Part of creating an approximation is to create a set of coefficients a i , which is another recurrence relation, which is then fed into the final relation
A[n+1]== A[n] + (z - z[[n]]) a[[n+1]] A[n-1]
which defies neither RSolve nor MatrixPower , at least I see.