Using Sage for math

This page is about the open-source math software (http://www.sagemath.org), NOT Sage Publications or the SAGE Visualization group.

Download/installation

  • 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 http://www.sagenb.org.
  • If you want to use Sage in other ways, download from http://www.sagemath.org/download.html (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') the file 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, http://www.packtpub.com/sage-beginners-guide/book.

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

Click to view examples

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 http://www.sagemath.org/doc/faq/faq-usage.html.
  • 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)
latex(f(x))

Learning more/getting help

-- LaurieSamuels - 27 Dec 2012
Topic attachments
I Attachment Action Size Date Who Comment
ExamplesForComputingSeries.swssws ExamplesForComputingSeries.sws manage 84.3 K 30 Dec 2012 - 16:58 LaurieSamuels A Sage notebook file with some basic examples
Edit | Attach | Print version | History: r9 | r8 < r7 < r6 < r5 | Backlinks | View wiki text | Edit WikiText | More topic actions...
Topic revision: r7 - 31 Dec 2012, LaurieSamuels
 

This site is powered by FoswikiCopyright © 2013-2022 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