Using Sage for math

This page is about the open-source math software (, NOT Sage Publications or the SAGE Visualization group.


  • If all you want to do is use the web-based notebook, but you do have internet access, you don't have to download anything. Just go to
  • If you want to use Sage in other ways, download from (very large file).
  • The Sage website has detailed download/installation directions. In addition, you will probably need to add the path to Sage to your $PATH environment variable after installation.

Ways to use Sage

  • The web-based notebook (see above)
  • If you have downloaded Sage, you have several options:
    • The interactive shell (type quit or exit when you're done). Useful commands: who, reset(), restore(). Also: tab-autocompletion.
    • The notebook (if you start the notebook from the interactive shell, type Ctrl-c in the shell after you sign out of the notebook)
    • Save a .sage file (say, "myfile.sage") and then either
      • type sage myfile.sage in a terminal window, or
      • type load('myfile.sage') or attach('myfile.sage') in the Sage interactive shell. Useful commands: attached_files(), detach()
    • Through Python, by importing Sage as a library

A few basic examples

Note that in Sage, there is usually more than one way to do something.

Most of these examples are from Sage Beginner's Guide by Craig Finch,

A file that you can upload to the Sage notebook: ExamplesForComputingSeries.sws

Click to view examples you could use with the interactive shell (type or copy one line at a time)

R users, watch out!

  • Sage uses zero-based indexing. That is, if myVector is equal to (10, 20, 30, 40, 50), then myVector[1] is equal to 20, not 10.
  • If Sage's indexing were otherwise like R's indexing, then in the above example, myVector[0:2] would give you (10, 20, 30). But it doesn't. In Sage, a 'slice' starts with the first element in the specified range and continues up to, but not including, the last element in the named range. So myVector[0:2] gives you (10, 20).
  • The minus sign in an index means different things in R and Sage. In the above example, myVector[-1] is equal to 50.
  • In Sage, matrices are defined by row, not by column.
  • The only assignment operator in Sage is the single equals sign, =. R's symbol <- will not work.
  • In Sage, exp(2) equals e^2. To get the result you would get in R (a numerical approximation), use exp(2).n() or exp(2).n(digits = 5), etc.
  • 10/3 in Sage equals 10/3. To get the result you would get in R, use 10.0/3, or 10/3.0, etc.

Python users, watch out!

  • ^ in Sage is equivalent to Python's **. (Sage also recognizes **; you can use either one to raise something to a power.) If you want XOR in Sage, see
  • 10/3 in Sage equals 10/3. To get an integer quotient, use 10//3.

Sage and LaTeX

var('x x0 theta')
var('yhat', latex_name= "$\hat{y}$")
f(x) = (x + x0)^theta / (2*yhat)

Learning more/getting help

-- LaurieSamuels - 27 Dec 2012
Topic attachments
I Attachment Action Size Date Who Comment
ExamplesForComputingSeries.swssws ExamplesForComputingSeries.sws manage 84.7 K 11 Jan 2013 - 15:55 LaurieSamuels A Sage notebook file with some basic examples
Topic revision: r9 - 11 Jan 2013, LaurieSamuels

This site is powered by FoswikiCopyright © 2013-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback