简体   繁体   English

定义一个二维函数数组,而不首先单独构造每个 function(Python 或 MATLAB/Octave)

[英]Defining a 2D array of functions without constructing each function individually first (Python or MATLAB/Octave)

(I would be glad to accomplish the following in either Python or MATLAB/Octave. Below I outlined the problem in Python.) (我很高兴在 Python 或 MATLAB/Octave 中完成以下任务。下面我概述了 Python 中的问题。)

I would like to define a 17x8 array of functions via a recurrence我想通过重复定义一个 17x8 的函数数组

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.函数A(k,j)B(k,j)没有索引,它们不需要数组。 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 .这适用于任意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.但是,这需要我在将它们集中到myFuncs数组中之前创建并命名各个函数。 In contrast, I need to build this table up without naming all 136 functions in the 17x8 array.相反,我需要在不命名 17x8 数组中的所有 136 个函数的情况下构建此表。 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编辑:为了明确并证明可以将解决方案编写为 k 的 function,我试图解决的重复是

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:使用n=0的已知值,对于 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其他等于 0,然后对于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.但是,手动执行此操作容易出错,我想对此进行概括,因为我希望计算所有这些函数的其他两个递归。

You can implement this as a single recursive function:您可以将其实现为单个递归 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.请注意,这不是计算 function 值的有效方法,因为我们将多次重新计算相同的值。 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.最有效的方法是简单地计算 17x8 数组中给定j值的所有值,从n=1开始并从那里向上移动。 Alternatively, you can add memoization to the recursive function to avoid re-computing values.或者,您可以将记忆添加到递归function以避免重新计算值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM