[SOLVED] Python 2.6 code behaves differently inside function than outside it
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Python 2.6 code behaves differently inside function than outside it
Hello,
I'm trying to write a small function that computes the linear equation systems by using the Jacobi iterative method. But I'm having such a problem:
There is a piece of code, that inside a function computes only for the first iteration and then it stops, giving wrong answer. Here is the code, right from the Python shell:
Code:
>>> a=[[9,1,1],[2,10,3],[3,4,11]
>>> b=[10,19,0]
>>> x_in=[0,0,0]
>>> def jacobi(a,b,x_in):
... if len(a)==len(b)==len(x_in):
... n=len(a)
... x_fin=x_in
... print("N={0}".format(n))
... for i in range(n):
... print ("Starting operation for row {0}".format(i))
... temp=b[i]
... for o in range(i):
... temp=temp-x_in[o]*a[i,o]
... for o in range(i,n):
... temp=temp-x_in[o]*a[i,o]
... x_fin[i]=temp/float(a[i,i])
... return x_fin
...
>>> jacobi(a,b,x_in)
N=3
Starting operation for row 0
[1.1111111111111112, 0, 0]
>>> jacobi(a,b,x_in)
N=3
Starting operation for row 0
[0.0, 0, 0]
While, when I write the same code, but outside the function, I get the normal answers for the first iteration:
Code:
>>> for i in range(n):
... print ("Starting Operation for row {0}".format(i))
... temp=b[i]
... for o in range(i):
... temp=temp-x_in[o]*a[i,o]
... for o in range(i,n):
... temp=temp-x_in[o]*a[i,o]
... x_fin=temp/float(a[i,i])
... print x_fin
...
Starting Operation for row 0
1.11111111111
Starting Operation for row 1
1.9
Starting Operation for row 2
0.0
What did I do wrong? It looks like I'm missing something. Thanks for your attention.
The first time you typed it, you've got dodgy indentation which means it doesn't work the same as the second time...
Compare
Code:
... #Second half of the function
... for i in range(n):
... print ("Starting operation for row {0}".format(i))
... temp=b[i]
... for o in range(i):
... temp=temp-x_in[o]*a[i,o]
... for o in range(i,n):
... temp=temp-x_in[o]*a[i,o]
... x_fin[i]=temp/float(a[i,i])
... return x_fin
with
Code:
>>> #Your loop code (indented to match the previous code)
>>> for i in range(n):
... print ("Starting Operation for row {0}".format(i))
... temp=b[i]
... for o in range(i):
... temp=temp-x_in[o]*a[i,o]
... for o in range(i,n):
... temp=temp-x_in[o]*a[i,o]
... x_fin=temp/float(a[i,i])
... print x_fin
...
The 2nd 'for o in range' in the first codeblock seems to be at the wrong indentation - by my reckoning it should currently raise a syntax error due to unexpected indentation.
Also, the 'x_fin=temp/float(a[i,i])' is at different indentations in the two code blocks. I don't actually know the algorithm you're describing, so I'm not sure which is correct, but one of them must be wrong
Finally, I think the issue which is causing you grief is the fact that you probably mean to move the 'return' to the same indentation level as the 'for i in range(n):'
The first one is going to work through the two loops and then return to the shell. The second one, however, prints the array out and continues through the 'for i in range(n):' loop you probably meant to put the return statement after the 'for i in range(n)' loop
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.