# Creating a table and using values from that table to compute the next value simultaneously

I have a function f with two integers as input arguments, and I need to create a table iterating on those input arguments. One of the properties of the function, is that f[i+1,j] is dependent on f[i,j], with this holding true for the 2nd argument as well, i.e in a table I can use the value positioned to the left or above to calculate the subsequent value.

Is there an elegant way of using the previous values of a table while it is being created (in one line without leaving Table[]),initiate C[[0,0]]=f[0,0] and define g[p_,q_] that uses the previous values and its own position?

Why do it like this? f is computationally expensive, using previous values will reduce greatly the number of operations needed.

Why not use functions that remember values they had found? The output of f will take lots of space. I do not want to have two copies, one remembered by the function, and one in the table I am generating.

• Have you seen RecurrenceTable[]? Aug 9, 2017 at 14:45

You should use RecurrenceTable as @J.M. suggests. But you should also try to do this out yourself. The first step in solving almost any programming challenge like this is to try to solve a simplified version of the problem. In this case, first try to do it with in 1 dimension instead of 2. I will do that with the well known Fibonacci sequence

There are two parts to this. (1) Defining a recursive function and (2) using Memoization.

You can define the n-th Fibonacci number with a function like below:

fib[1] := 1;
fib[2] := 1;
fib[n_] := fib[n - 1] + fib[n - 2];


We want to memoize this function called fib. Please look up memoization if you're not yet familiar with it. To do that, we'd re-write it as:

fib[n_] := fib[n] = fib[n - 1] + fib[n - 2];


Now we've defined it, you can test the function. And we can use Table to build out a table of values:

Table[fib[i], {i, 1, 100}]

• I should prolly add the cautionary note that RecurrenceTable[] can sometimes be troublesome to use for partial difference (doubly indexed) equations, so the technique Searke presents here is useful to know. Aug 9, 2017 at 15:20