# Table where an element depends on the sum of previous elements

I'm trying to create a table whose elements will be a sum over previously declared elements up to a certain iteration. I've reduced what I'm trying to do to the simplest form (posted below):

M[x_] := M[x] = Table[
If[
n == 1, r, 2 x^n Sum[M[x][[i]], {i, 1, n-1}]
],
{n, 1, 5}]


When I evaluate the above expression, it produces an exceedingly long expression that is obviously wrong. What's the mistake that I'm making here?

• M[x] only references itself here, so it will be an infinite recursion. When you call M for instance, the RHS contains a sum over the elements of M. – Marius Ladegård Meyer Mar 5 '16 at 23:14
• I'm sorry I don't understand. Why would it be an infinite recursion? Say for instance, we're on the iteratino n=2 then it should sum from i=1 to i=1 and the expression should be 2x^n M[x][] where x could now be specified. Right? – user38332 Mar 5 '16 at 23:25
• Each item M[x] is referenced it must call itself to determine its value. Table must complete and return a value to M[x] before M[x] can be assigned a value. Since the looping is happening inside of Table the calls in Table to M[x] are causing M[x] to be called again in a never ending loop. – Edmund Mar 5 '16 at 23:31
• Have you seen RecurrenceTable[]? – J. M. will be back soon Mar 5 '16 at 23:52
• If I wanted to set the ith element to be the sum of all previous elements would RecurrenceTable[] be able to do the job? – user38332 Mar 6 '16 at 0:18

I think FoldList is what you are looking for.

m[x_] := FoldList[#1 + #1 2 x^#2 &, Prepend[r]@Range[2, 3]]


Then

m[y]


{r, r + 2 r y^2, r + 2 r y^2 + 2 y^3 (r + 2 r y^2)}

Just need to change the 3 to a 5.

Hope this helps.