LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Python 2.6 code behaves differently inside function than outside it (http://www.linuxquestions.org/questions/programming-9/python-2-6-code-behaves-differently-inside-function-than-outside-it-869990/)

dracuss 03-21-2011 09:58 AM

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.

Snark1994 03-21-2011 11:35 AM

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

Hope this helps,

dracuss 03-21-2011 12:51 PM

It was a problem with gvim :) Thank you very much for your help


All times are GMT -5. The time now is 10:54 AM.