0

I have a running program with the Lanczos algorithm in Martlab and I am trying to convert it to Python. I have the following problem:

in Matlab I have the following loop:

beta(2)=0;

for j=2:m+2

w = A*V(:,j) - beta(j)*V(:,j-1);
alpha(j) = w'*V(:,j);
w = w - alpha(j)*V(:,j);
beta(j+1) = norm(w,2);
V(:,j+1) = w/beta(j+1);
end

I have implemented that the following way in Python:

beta(2)=0
for j in range [2: m+2]:
    w=A*V[:,j] - beta(j)*V[:,j-1]
    alpha(j)=w.transpose()*V[:,j]
    w = w - alpha(j)*V[:,j]
    beta(j+1)=norm(w,2)
    V[:,j+1]= w/beta(j+1)

The problem is I keep getting the error message SyntaxError: “can't assign to function call”. What could be the possible reason I keep getting this error message? It's not an indentation problem.

Thanks!

hpaulj
  • 221,503
  • 14
  • 230
  • 353
Terza
  • 21
  • 6

1 Answers1

2

Well, that's not the correct way of assigning values to an array. In Matlab/Octave you can access an array element by doing this arr(2), but this doesn't hold true in python. In Python, you write arr[2] if you want to access the element at 2nd index in an array arr (0-based indexing). Parenthesis after a variable name means calling or defining a function. this(2) would mean calling the function this() and passing 2 as an argument. I hope this will get you rid of the error.

Also, consider writing a couple of easy python programs which will get you well versed with the syntax.

Anant
  • 302
  • 2
  • 11