Cam's Blog

December 5, 2009

Sage days 18

Filed under: Math — cfranc @ 10:39 am

I’ve been at the fantastic Sage days 18 conference in Cambridge this past week. In case you haven’t heard, Sage is a free open source mathematics package. It is particularly useful for doing number theoretic computations, and its functionality is expanding rapidly. I learned a few incredibly useful things this week that I’ll share now:

  • Tab completion: this feature is so useful because it removes the need for you ever to look in a manual to find the names of commands. Sage uses a command prompt interface. If you type the letter N and then hit tab, Sage will show you all possible commands beginning with the letter N. So if you’ve got a computation that you’d like to do, experimenting with tab completion allows you to easily hunt down the proper commands to make Sage do what you’d like. This seems to be at least ten times faster than searching for the correct commands in some arcane document. Also, making frequent use of tab completion will introduce you to all sorts of useful features that you may not have known existed in Sage otherwise. For instance, to find short vectors in a lattice I used to create a matrix with rows containing a basis for the lattice, and then I would call the LLL algorithm on this matrix. This worked, but while experimenting I randomly discovered another way to do this: you can define the lattice as a module in Sage, and then you can call a function, the name is something like short_vector_list, which returns a list of all vectors in your lattice of length up to a given bound. Very useful!
  • The question mark: let’s say you’ve used tab completion and you see a command that looks like it’ll do what you want it to do. You can enter the command into the prompt with a question mark immediately after it, for instance NumberField?, and Sage will bring up the documentation of this function. Then you can easily see what input the function takes, and what it will compute. Thanks to this feature and tab completion, I anticipate never needing to look in a Sage manual ever again.
  • The DOUBLE question mark: if you enter a command into Sage followed by two question marks, for example NumberField??, then it will prompt you with the documentation for NumberField, as well as the source code for the function in question! If you’re curious about how a certain algorithm works, this allows you to go straight to the source and see exactly what Sage is doing behind the scenes. You might suspect that Sage is not doing things as efficiently as possible, and you can easily use the double question mark to check. Then since Sage is open source, you’re free to alter things as you like!

In the past week these three simple features alone have made Sage so incredibly easy to use that I feel stupid for not having used them consistently in the past. If you’ve tried Sage and were frustrated by constantly needing to search for the proper commands, give these hacks a try and you should have a much smoother user experience. If you haven’t tried Sage but regularly do mathematics, then get with the mathematical zeitgeist and give Sage a try.


1 Comment »

  1. I know how you feel! It took me months before I learned about many of the easy ways to find commands and see what they do in Macaulay 2 (my computational math program of choice).

    In fact, on a different but related note, the number of times I copied and pasted (or retyped!) lists is embarassing. It was just a month or two ago that I figured out how to avoid that kind of thing. And the day I found out about the shortcuts was pretty great. 🙂

    Comment by jbraj — December 6, 2009 @ 10:16 am

RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at

%d bloggers like this: