As we mentioned above it is possible (and desirable) to write your functions using a text editor. We will use our favorite editor: emacs.
The first step is tell emacs about python. Start by editing (with emacs, why not?) the .emacs file that should be in your home directory. If you don't have this file there, don't worry: just open a new one.
Add the following lines to your .emacs file:
;;python mode stuff
(setq auto-mode-alist
(cons '("\\.py$" . python-mode) auto-mode-alist))
(setq interpreter-mode-alist
(cons '("python" . python-mode)
interpreter-mode-alist))
(autoload 'python-mode "python-mode" "Python editing mode." t)
Don't worry about the meaning of these lines, they tell emacs what to
do when you want to use python.
Save your modified .emacs file and restart emacs.
Open a new file called elliptic.py (you can call it anyway you like, but the termination .py is useful to distinguish python's files). You can open a file in emacs by using the File menu at the upper left corner, choosing open file and typing the name of the file at the bottom line on the emacs screen (remember to type Enter).
Now you got a clean new file for your (ab)use. Let's start by retyping the definition of the function on_curve. Two things to notice: emacs colors automatically your syntax (if it doesn't, ask someone to tell you how to do this). Also, indentation is kept automatically: you only have to make sure that you un-indent when you want, as we will see later.
Now modify the function to test if the point is on the curve
. Your file should look like:
def on_curve(x,y):
"""See if (x,y) is on the curve y**2==x**3+3x"""
return y**2==x**3+3*x
Now save it (using the menus: File and then Save).
You could go back to the python that you were using before but, instead, we will use emacs to run python. On emacs' Python menu, choose Start interpreter. If you did everything we said your emacs window will split and on one part you still have your elliptic.py file and on the other you have python. Now we have to tell python to load our function on_curve. Actually, what python loads is the whole file; to read the file go to emacs Python menu and choose Import/Reload file.
Now we can check if the point
is on_curve:
>>> on_curve(0,0) Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'on_curve' is not definedWell, obviously something is not right... The point is that now we should call the function on_curve as elliptic.on_curve instead (because it is part of the elliptic file --or, more properly, module). Try again
>>> elliptic.on_curve(0,0) 1
You can now explore the other options available in emacs' Python menu.