问题
I have the following recursive function,
def subspaceiterate(A,V,v,j):
if j == 0:
return v
else:
v_jm1 = V[:,j-1]
v_jm1 = np.reshape(v_jm1,(np.size(V,axis=0),1))
v = v - np.matmul(v_jm1.T,np.matmul(A,v_jm1))
j = j - 1
subspaceiterate(A,V,v,j)
A
is an mxm matrix whose eigenvalues and eigenvectors I want to compute using an iterative method,V
is an mxm matrix that stores my initial guess for the eigenvectors of A
, v_j
is a particular column of V
, and j
is an index that I descend and use to subtract away the normalized Rayleigh-Taylor quotient of every "v_k"
for k e {0,1,...,j-1} from my particular v_j. I believe this is the method of subspace iteration. In my program I call it like so,
v_j = np.empty((np.size(V,axis=0),1))
v_j = V[:,j]
v_j = np.reshape(v_j,(np.size(V,axis=0),1))
j = 5
v_j = subspaceiterate(A,V,v_j,j)
However, this does not work as subspaceiterate()
returns an object
and not an ndarray
like I need it to so the code that comes after this is broken. To resolve this issue I have considered vectorizing my code; however, I have never done so before and presently I am lost at the following error
File "test.py", line 24, in subspaceiterate
v_jm1 = V[:,j-1]
IndexError: invalid index to scalar variable.
My understanding is that np.vectorize() works by using a for
loop in some fashion so I think j
is not a scalar anymore like I would hope, but whether my analysis is correct and how to right the issue is lost to me and I was hoping someone brilliant could shine some light on how to resolve this issue.
来源:https://stackoverflow.com/questions/65174372/issue-vectorizing-a-recursive-function-that-is-used-in-iterative-scheme-to-calcu