1

(I would be glad to accomplish the following in either Python or MATLAB/Octave. Below I outlined the problem in Python.)

I would like to define a 17x8 array of functions via a recurrence

if 0<k<16:
    f[k,n+1](j) = A(k,j)*f[k-1,n](j) + B(k,j)*f[k+1,n](j)
elif k==0:
    f[k,n+1](j) = B(k,j)*f[k+1,n](j)
elif k==16:
    f[k,n+1](j) = A(k,j)*f[k-1,n](j)

The functions A(k,j) and B(k,j) are not indexed, they do not need an array. They are not essential to this problem. The initial values for the recurrence are given by the known values

if k==8:
    f[k,0](j)=0
else:
    f[k,0](j)=1

which hold for arbitrary j.

How can I define an array of functions recurrently in a 2D array? I have seen examples such as this that construct an array of functions by

myFuncs = [f0,f1,f2]
myFuncs[2](...) #calls f2

However, this requires me to create and name individual functions before lumping them together in the myFuncs array. In contrast, I need to build this table up without naming all 136 functions in the 17x8 array. How can I accomplish this?

EDIT: To be explicit and demonstrate that it is possible to write the solution as a function of k, the recurrence I am trying to solve is

f[k,n+1](j) = sqrt(j+k-8)*f[k-1,n](j) + sqrt(k+j-7)*f[k+1,n](j)

Using the known values for n=0 I can obtain the following for n=1:

f[9,1](j) = sqrt(j+1)
f[7,1](j) = sqrt(j)

with the others equal to zero, and then for n=2

f[10,2](j) = sqrt((j+1)*(j+2))
f[8,2](j) = 2*j+1
f[6,2](j) = sqrt(j*(j-1))

with the others zero. Doing this by hand is prone to errors, however, and I would like to generalize this because there are two other recurrences I wish to calculate all these functions for.

BGreen
  • 370
  • 3
  • 17
  • 1
    Can you pick a language and write an MCVE? – Mad Physicist Aug 01 '20 at 14:14
  • You can always ask another question about porting it if you have a problem – Mad Physicist Aug 01 '20 at 14:15
  • As a tool, I'd like to suggest you look at NumPy, it has sophisticated matrix functions that I suspect are on a par with Octave/Matlab. There's also SciPy – Mark Bennett Aug 01 '20 at 14:44
  • @MadPhysicist I did pick a language for the example above; I used Python syntax. For something definite, just take A(k,j)=B(k,j)=1 or something. Apart from that, what do you mean for an MCVE? If I could write a working example, then my problem would have been solved. – BGreen Aug 01 '20 at 15:13
  • @CrisLuengo Thank you for catching that. "Out-of-bounds" terms in the recurrence are treated as 0 and I have edited the question to reflect this. I'm not sure what you mean by writing them down one by one, My intention was to put the recurrence relation given above into a for loop. – BGreen Aug 01 '20 at 15:30
  • @MarkBennett NumPy is what I'm planning to use, but I do not see how that would help at all. To my knowledge, NumPy's matrix functions are for working on a matrix of scalars. I need to define a matrix of functions recurrently. – BGreen Aug 01 '20 at 15:33
  • @CrisLuengo No, it is possible to obtain a function of `j`. I have edited the question with my specific case to demonstrate this. Because this question was already closed by the time of the edit, I have reposted it here: https://stackoverflow.com/questions/63219651/defining-a-2d-array-of-functions-using-a-recurrence-relation-python-or-matlab-o – BGreen Aug 02 '20 at 18:27
  • @CrisLuengo Thank you for pointing out those typos, and I apologize. The leftmost `f[k,n]` should have been `f[k,n+]` and the `,j` in the `sqrt` was also a typo. The "closed question" notification suggests posting new questions, so at first I was under the impression that was the appropriate action. I will flag this one as you suggest. – BGreen Aug 02 '20 at 19:07
  • 1
    I’ve voted to reopen. The notification is not clear, but it suggests to post a new question, not post the same question again. I wish they used clearer wording! – Cris Luengo Aug 02 '20 at 19:11
  • I just ran into this question again. I was following it, hoping to get notified when it was reopened, but apparently that is not how that feature works... – Cris Luengo Sep 16 '20 at 07:38

1 Answers1

0

You can implement this as a single recursive function:

function ret = f(k,n,j)
if n==0
   if k==8
      ret = 0;
   else
      ret = 1;
   end
else
   if k==0
      ret = A(k,j) * f(k-1,n-1,j);
   else if k==16
      ret = B(k,j) * f(k+1,n-1,j);
   else
      ret = A(k,j) * f(k-1,n-1,j) + B(k,j) * f(k+1,n-1,j);
   end
end

Note that this is not an efficient way of computing the value of the function, as we'd be recomputing the same values many times. The most efficient method would be to simply compute all the values in the 17x8 array for a given value of j, starting at n=1 and moving up from there. Alternatively, you can add memoization to the recursive function to avoid re-computing values.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • Right, I am aware I can calculate the values recursively, or use memoization. My intention with this question was to define an array of functions recursively. As I demonstrated in the example in the edit, it's possible to write for each k and n a closed-form expression that works for arbitrary j. The idea of doing this with a function is that I can calculate a value for each j as needed, keeping in memory a small 17x8 array instead of a 17x8x(j_max) array where j_max may be very large. – BGreen Sep 16 '20 at 12:04
  • P.S. For the time being, though, I had already decided to bite the bullet and use the explicit approach of calculating all 17x8x(j_max) values and saving them. By coding it as matrix multiplication instead of using for loops, this is doable fairly quickly. – BGreen Sep 16 '20 at 12:06
  • 1
    @BGreen: Right. What I tried to imply by this answer is that an array of functions that reference each other is equivalent to the one function I posted here, and it’s not an efficient way of computing. Explicitly calculating the full array is much more efficient. – Cris Luengo Sep 16 '20 at 13:12
  • I see, that's a good point. Thank you for clarifying. – BGreen Sep 16 '20 at 15:07