Ask Your Question
0

Writing code in Notepad and copy pasting to SageMath

asked 2019-05-13 22:20:06 +0100

Algebear gravatar image

updated 2019-05-13 22:20:28 +0100

So I'm trying my best to get the indent right everywhere, but sometimes I get the error that the indent is not right somewhere and also I get the error that I use some undefined name n_points; I have even tried typing the whole code into Sage myself, but then if I copy it from there and paste it in the notepad, it still includes all the dots. Even if I delete those dots, Sage still does not get happy when I copy and paste the code after only removing the dots via the notepad.... This is my code

def ell_geom_animation(E,init_points,n_points=10):
      var('x,y')
      #E = EllipticCurve(y^2==x^3-82*x);
      p = init_points
      j = len(p)
      ell_frames = [plot(E)]*(n_points+j)
      point_plots = []
      line_plots = []
      point_frames = []
      line_frames = []
for i in srange(0,j-1):
    point_plots += point(p[i][0:2],color='black',size=10)

 for i in srange(0,n_points):
     p += [E(p[i]+p[i+1])]
     point_plots += point(p[j+i][0:2],color='black',size=10)
     point_frames.append(point_plots)
     line_plots += line([p[i][0:2],p[i+1][0:2]],color='black',size=10)
     line_frames.append(line_plots)

 A1 = animate(ell_frames)
 A2 = animate(point_frames)
 A3 = animate(line_frames)
 return show(A1+A2+A3) # superimpose frame

Does anyone have some idea what is wrong with the alignments and maybe have a suggestion where I can write my code better such that I can simply copy and paste it in SageMath and press enter? I would very much appreciate suggestions and help.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2019-05-13 22:29:09 +0100

rburing gravatar image

updated 2019-05-13 22:41:41 +0100

Whitespace is important in SageMath (as in Python). The body of a function needs to be indented, so everything inside the function should be indented (e.g. by four spaces). Similarly, the body of a for-loop needs to be indented. This should be the correct indentation (I did not check anything else):

def ell_geom_animation(E,init_points,n_points=10):
    var('x,y')
    #E = EllipticCurve(y^2==x^3-82*x);
    p = init_points
    j = len(p)
    ell_frames = [plot(E)]*(n_points+j)
    point_plots = []
    line_plots = []
    point_frames = []
    line_frames = []
    for i in srange(0,j-1):
        point_plots += point(p[i][0:2],color='black',size=10)
    for i in srange(0,n_points):
        p += [E(p[i]+p[i+1])]
        point_plots += point(p[j+i][0:2],color='black',size=10)
        point_frames.append(point_plots)
        line_plots += line([p[i][0:2],p[i+1][0:2]],color='black',size=10)
        line_frames.append(line_plots)
    A1 = animate(ell_frames)
    A2 = animate(point_frames)
    A3 = animate(line_frames)
    return show(A1+A2+A3) # superimpose frame

In your snippet some of the code was outside of the function, so local variables were not defined.


Instead of removing the leading dots (from the Sage command line) you should replace them by spaces.


To avoid copying and pasting you can also load a file from the command line, or use the Jupyter Notebook interface instead.

edit flag offensive delete link more
2

answered 2019-05-14 07:25:16 +0100

Iguananaut gravatar image

updated 2019-05-14 07:35:27 +0100

In addition to @rburing's answer, I would add a couple more things:

First there is a %%file"cell magic" that is very convenient if you just want to save some input to a file: https://ipython.readthedocs.io/en/sta... (it's documented as "writefile" but there is an alias of it shortened to just "file"). Put this at the top of an existing input like:

sage: %%file foo.sage
....: <the existing code>

When you run this, instead of executing the code it will just save it to a file. You can prepend this to an existing code cell too. I do this sometimes if I want to save small snippets of code out to a file. It's not always the most convenient though.

Another useful "magic" (Sage's interface is built on IPython so all the IPython magics listed on the above-linked page are relevant) is %history. If you just run %history it will print out all the recently-entered code without the prompts. E.g.:

sage: 1 + 1
2
sage: for x in range(5):
....:     print(x)
....:
0
1
2
3
4
sage: %history
1 + 1
for x in range(5):
    print(x)
%history

(note that this includes the %history command itself as part of the history). %history also takes several optional arguments which you can read in the documentation. For example, it is possible to print just the last N lines of history.

Finally, while there is absolutely no shame in using plain ol' notepad (I use it all the time as a quick one-off clipboard for things when I'm working on Windows) a lot of these problems could be avoided by getting used to an editor intended for programming. On Windows, Notepad++ is quite venerable. The Atom editor is also popular on Windows, I hear, though I've never used it personally. I know some people have also used PyCharm for Sage.

Although most editors won't, out of the box, know what to do with the sage: and ...: prompts when copy/pasting from a Sage console, the thing about programming-specific editors is that most of them are programmable themselves to some extent, and can be taught things like how to automatically strip that off. Or even if you do it manually like using a find/replace, most code editors will have modes for different programming languages and will recognize Python and do things like automatically add indentation where appropriate and help you out with that. When I'm using an editor for programming (I mostly use vim personally, but with almost any of them) I almost never manually type indentation when writing Python code. At most usually it's necessary to press the <tab> key to add indentation when the editor can't guess that I want it, but usually it can (e.g. after writing a for x in y: statement).

edit flag offensive delete link more

Comments

Thanks for the suggestions! I have downloaded Notepad++ and after changing the settings by letting tabs be 4 spaces, it now works really well!

Algebear gravatar imageAlgebear ( 2019-05-14 13:34:19 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-05-13 22:20:06 +0100

Seen: 1,139 times

Last updated: May 14 '19