Aldebrn

Navigation

Recent site activity

Sage in Research

Intro

This  collection of notes is my "Sage for Matlab users." After many years of using Matlab for undergraduate engineering courses, I decided my graduate research (in statistical signal processing algorithms) could very much benefit from moving to a more serious mathematics computing platform.

Sage is a computational package that brings together many existing and new software packages. Although some of its packaged components are written in C, Lisp, or other languages, Sage itself is written in Python, a simple but very capable high-level language that is similar to Matlab's M scripting but more powerful, expressive, and complete---and these characteristics are very much beneficial to software supporting serious applied math research.

And this lies at the heart of any prospective move from Matlab to Sage: software. For one-off math scripts, Matlab's simplicity makes it a better choice, but for a larger collection of interlocking software components that implements some complicated mathematical idea, an applied math programmer can tap large productivity gains in using a more versatile tool. Put another way: Python gives you the tools used by professional programmers to manage complexity---in my experience the style of programming encouraged by Matlab's simplicity is very conducive to the stand-alone script but leads to code that is hard to maintain and extend when applied to writing a larger reusable application.

I can only describe my own experience for you, which has been very positive. For a very excellent overview of the many incredibly-valuable features of Sage, please see Bye Matlab, hello Python, thanks Sage.

Note that Sage isn't for everybody. Its primary authors are number theorists and algebrists who seek to replace commercial closed-source symbolic math software such as Magma. Therefore, although the numerical linear-algebra-style parts of Sage are very powerful, they can be a little "crufty," meaning the software is unpolished, and you may have to develop some software engineering skills to figure out how to do something you know you ought to be able to do. 

Brief example: in Matlab, everything (even a scalar) is a double float array. In Sage, you have native-Python number types (integer, single floats, etc.) and Sage-provided symbolic fields (Galois fields!) and number types (exact rationals, arbitrary precision real and complex numbers, etc.); you also have simple Python tuples and lists, but also separate Numpy-provided vector, array, and matrix objects, all with very specific functionalities. Usually Sage knows how to convert between the many types of numbers and matrices it provides, but sometimes it doesn't! And that's when you will need to  convert between data types explicitly (and read the documentation to find out how).

Therefore, I feel that if one already has (or is willing to gain) a little computer science experience, e.g., experience in reading API documentation, asking questions on developer mailing lists and IRC channels, and Googling a lot, and if one has the need to develop applied math software, you are quite prepared for moving from Matlab to Sage.

For the present moment, please excuse the disorganized nature of these notes and please visit us on at Freenode IRC (#sage-devel and #sage-support) or Sage Math.

Useful things

Sage is shipped with Ipython, which presents an interface to the Python interpreter with the sophistication we command-line interface (CLI) users have come to expect of our CLI apps:
sage -ipython

Sage also ships with Mercurial!
sage -hg

Using Mercurial for source control

For now, Mercurial (aka "hg") stuff is going to be here. Since Sage ships with hg, I'm learning it. 

For anything more complicated than simple one-off scripts testing some simple idea, I try to use a distributed source control system like Mercurial. I have steered clear of CVS and Subversion per Linus Torvalds' recommendation. The Mercurial book has this information in a couple of different places, so here's how to get up and running quick.
  1. Enter a directory that you want to version-control.
  2. "sage -hg init" to create a repository in the current directory.
  3. "sage -hg add *.c *.cu *.py" and etc. to tell hg you want to want to track these files.
  4. In Unix, create a file, ~/.hgrc, and in it at least this following lines:
    [ui]
    username = Ahmed Fasih <email@osu.edu>

  5. "sage -hg status" will show "A"'s next to the files you've added and "?"'s next to the files you haven't added.
  6. "sage -hg commit" will pop open a temporary file in your default text editor (careful here!) for you to type in a commit message. Save and quit the editor when done and the commit has happened.
  7. Edit one of the files you're tracking.
  8. "sage -hg status" will show an "M" next to that file.
  9. "sage -hg diff" will show you the differences.
  10. "sage -hg log" will show you past commits.
  11. At this point you probably will want to add this line to your ~/.bashrc and ~/.profile in Unix and either re-login or run "source ~/.bashrc" in your open terminals:
    alias hg="sage -hg"

Python libraries in Sage

Sage ships with a fully functional Python: "sage -python" will drop you into the same Python interpreter that is a couple of layers underneath Sage. So any Python add-on libraries can be installed by using "sage -python"! This is fantastic because you don't need to worry about version conflicts (Sage ships with everything it needs) or root priviliges (you untar Sage in your home directory and you're off).

PyCUDA

I have successfully gotten PyCUDA working in Sage. It was pretty easy to follow the directions carefully. (Boost has some trouble building with GCC 4.1, and it auto-detects a system-wide Python and builds for it, instead of Sage's Python.)

So now I can use PyCUDA within Sage. Let's get cracking. (Mar 6, 2009)
Subpages (1): Background for coding