LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-21-2011, 09:58 AM   #1
dracuss
Member
 
Registered: May 2006
Location: Chisinau, Moldova
Distribution: Gentoo, Debian sid
Posts: 151

Rep: Reputation: 29
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.
 
Old 03-21-2011, 11:35 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
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,

Last edited by Snark1994; 03-21-2011 at 11:42 AM.
 
Old 03-21-2011, 12:51 PM   #3
dracuss
Member
 
Registered: May 2006
Location: Chisinau, Moldova
Distribution: Gentoo, Debian sid
Posts: 151

Original Poster
Rep: Reputation: 29
It was a problem with gvim Thank you very much for your help
 
  


Reply

Tags
error, function, python


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] OpenLDAP Commands Over TLS/SSL Behaves Differently? peridian Linux - Security 1 02-20-2011 01:31 PM
rsync behaves differently under cron and under my normal environment jjge Linux - Software 1 03-16-2010 08:55 AM
why java panels behaves differently in linux and solaris ?? alred Programming 2 07-20-2006 02:21 AM
applet on linux server behaves differently naijaguy Programming 3 01-07-2005 02:09 PM
shell behaves differently in rl5 and rl3 Warmduvet Linux - General 22 09-22-2004 05:05 PM


All times are GMT -5. The time now is 09:44 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration