On Fri, Oct 16, 2009 at 6:22 PM, StephenMKauffman
<[email protected]> wrote:
>
> I'm coming from writing Visual Basic code and am clueless about
> python. I typed this into KWrite and saved it with .sage extension. I
> used tab to indent and I am used to closing if then with an endif
> statement, likewise I'm used to ending an for next statement with next
> i etc.
>
> def nx(Tableau,int pr,int pc):
> newtab=Tableau
> if not pr in range(newtab.nrows()):
> print('invalid pivot row in function nx')
> return 'invalid pivot row in function nx'
> if not pc in range(newtab.ncols()):
> print('invalid pivot column in function nx')
> return 'invalid pivot column in function nx'
> if newtab[pr,pc]==0:
> print('invalid pivot is 0')
> return 'invalid pivot is 0'
> else:
> pivot=newtab[pr,pc]
> cellar=range(newtab.ncols())
> for j in range(newtab.ncols()):
> if j==pc:
> cellar(j)=0 #had this set to null in VB
> else:
> cellar(j)=-1*newtab[pr,j]/pivot
> for i in range(newtab.nrows()):
> for j in range(newtab.ncols()):
> if i<>pr and j<>pc:
> newtab[i,j]=newtab[i,j]+cellar(j)*newtab[i,pc]
> elif i==pr and j<>pc:
> newtab[i,j]=cellar(j)
> elif i<>pr and j==pc:
> newtab[i,j]=newtab[i,j]/pivot
> return newtab
>
> I don't know if I'm using the logical operators and, or, not, etc
> correctly. When I copy and paste this into the shell I also get a lot
> of indentation errors. If I type at the prompt I suppose you un-indent
> using the arrow keys since when I hit enter twice sage just executes
> what I've typed so far.
>
> I'd like to require the integer data type for vars pr and pc and I'd
> like the matrix input called Tableau to be in QQ. I don't know how to
> do this and I get errors from my attempted code. Also if I type the
> function in the shell how do you open it again for further editing. A
> lot of questions I know and I am reading the online tutorials but I
> can't find the answers. Any help debugging would be great, thanks.
>
> Steve
>
For learning Python, I recommend:
http://docs.python.org/tutorial/
and
http://www.diveintopython.org/
Regarding your code, edit it in a file say "foo.sage", then in Sage type:
sage: load foo.sage
Regarding endif, endfor, etc., you might want to put them in as comments, e.g.,
if not pr in range(newtab.nrows()):
print('invalid pivot row in function nx')
return 'invalid pivot row in function nx'
...
#endif
I did that a lot when I first started using Python.
The "int"'s here make no sense: "def nx(Tableau,int pr,int pc):"
,because Python does not have type declarations in function
definitions. Python is a dynamically but strongly typed language.
Welcome to Sage & Python!
William
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---