Lately I’ve been playing around with the free software package SAGE, which does a lot of things mathematical. Though I think most people will have heard of SAGE, let me give their mission statement as expressed on their website:

**Sage**is a free open-source mathematics software system licensed under the GPL. It combines the power of many existing open-source packages into a common Python-based interface.

*Creating a viable free open source alternative to Magma, Maple, Mathematica and Matlab*.

Though SAGE does everything under the sun and has the complexity of a flying saucer, I am mostly concerned with using it for teaching at the moment. Below I’ll go over some neat things I’ve been able to do with the software.

If you’ve never used SAGE the first thing you’ll notice is that it’s a bit of a pain to get working. The method I’ve had success with is downloading the source and compiling it from scratch. That’s not as hard as it sounds, and for a lot of people it’s a piece of cake, but it’s frustrating to think that this will stop a lot of people from using this really amazing software.

If you are a Windows user and want to fully install SAGE, these steps might help.

- Download and install the free software VirtualBox.
- Download an Ubuntu iso, and install it in VirtualBox, using these or other instructions.
- Follow these instructions in the section “Compile from Source.”

Well, at least you can feel pretty cool when you finally run it and get the somewhat anticlimactic sage prompt greeting you from the command line:

**sage:**

So what do you do with that prompt? The first thing you probably want to do is run the command

**sage: notebook()**

which will give you a slightly more graphical interface. From there you can go on to use the control SAGE gives you to make the most beautiful mathematical plots you can imagine.

Look at this for example:

With this example and the others to follow, if you click on the image you can see the code used to generate it. The above image is taken from an example in the SAGE documentation.

As a tool for making plots for exams, quizzes and notes (even textbooks?) SAGE compares very favorably with MAPLE. (I have to confess I have very little experience with *Mathematica*.) While SAGE lacks the “right click” easiness of MAPLE, certain things are more natural, mostly owing to the beautiful Graphics() object supported in SAGE. These objects can be “added,” as shown below, to produce highly customized diagrams.

To give an example, suppose you want to show your students the area bounded by the curves and . Then with the following commands, you get the picture below.

**var(‘x’)**

**p1 = plot(x^2,(x,-2,2), hue = “.3”,fill=true)**

**p2 = plot(x^3-2*x,-2,2, hue = “.7”,fill=true)**

**p1+=p2**

**p1.show()**

One thing that makes the usage natural is the way you can add plots (technically Graphics() objects) and delay showing them. When they are “shown” they can be shown with a variety of options.

Here is another method for producing a similar picture which is a little more visually pleasing (click for SAGE code)

What’s more, it’s easy to export all of these things to a variety of file formats.

You may want to add text to your pictures (even LaTeX) as in the following.

If you examine the code for the above image, you can see that the integral was automatically TeXed by SAGE, and plotted at coordinates that I was able to choose.

You can even control the rotation of text.

If you have to compute something onerous, you may want sage to do it for you, like in this example.

If you examine the code for the above image, you can see that it was SAGE which computed the 4th derivative of the function. Note also that the label locations on the curves were plotted by using the actual functions!

More power is added to the graphing capability by using SAGE’s ability to iterate. Here are the first 13 Taylor approximations to cosine around the origin:

If you examine the code for the above image you can see that the 13 Taylor approximations are not hard-coded, but rather produced by a repeat loop. It would be a simple matter to make the number of approximations shown 130 rather than 13. Also note that I am using the counter variable to control the transparency (*ie* alpha) of the curves drawn, so that the lines appear to become more solid as they become closer approximations. The hues of the lines are also changing as a function of the counter variable, and produce a nice spring palette.

Something similar with hyperbolic tangent becomes a lesson in radii of convergence. Here are the first 100 Taylor approximations to near the origin.

Though I have not done so, it would be simple to use iteration to make direction field plots of arbitrary resolution, for use in explaining differential equations. This might even be a good (too challenging?) exercise for the students.

SAGE also supports 3d plotting. The 3d image manipulation program works far better than the corresponding application packaged with MAPLE. There is a way to import the resulting Java application into a web page, but I will just supply still images here:

The ability to iterate can also be used to produce animations. This capability is highly flexible, and can be used to make animated gifs, or even export movie files.

If you examine the SAGE code for the image above, you can see that the program is performing an intriguing kind of “continuous” iteration, in which all are generated for a given and interval . The motion tends to give me a headache, but I thought I had to show an example! Scroll down!

Lastly I wanted to say a few words about SageTeX. It is useful to be able to use SAGE from within LaTeX for the following reasons:

- You can produce figures on demand, without exporting, converting to jpeg, etc, which can all be very tiresome.
- You can use SAGE to actually compute within your document, reducing the chances of errors in calculation and saving labor.
- Certain objects, like matrices, can be a pain to typeset, but this can be done automatically using the SAGE
**latex()**function. This function takes a SAGE object as input and outputs a latex description, so that, for example,

sage: a=matrix([[1,2,3,4],[5,6,7,8],[9,10,11,12]])

sage: latex(a)

\left(\begin{array}{rrrr} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & 10 & 11 & 12 \end{array}\right)

can be automatically generated.

The full power of SageTeX is nicely illustrated by a file called **example.tex** which can be found in

** $SAGE_ROOT/local/share/texmf/tex/generic/sagetex**

assuming you have succeeded in installing SAGE.

I can not help but editorialize that installing and using SageTeX took me approximately 4 hours of hair tearing frustration — it is comparable in difficulty to capturing the pegasus.

Only those with a fair amount of Linux expertise are likely to accomplish this goal, so let me briefly list some lessons learned in unabashed jargon. I will not touch on anything covered in the instructions given in the above website, but explain a few things which were *not* covered. Everything is with reference to Mint, but I assume Ubuntu is sufficiently similar that the following are still relevant.

- The file you need to edit to export new environment variables (
*eg***SAGE_ROOT**) is**/etc/bash.bashrc** - The command
**env**will list all environment variables. You will probably need to do a couple**env | grep SAGE**and**env | grep TEX**commands as you go along. - You can see the current value of an environment variable, for instance
**SAGE_ROOT**, by executing**echo $SAGE_ROOT** - When you installed
**texlive**it probably included a version of SageTeX which it has hidden in your directory structure, possibly here:**/usr/local/share/texmf/tex/generic/sagetex**. Unfortunately the version of SAGE expected by those files is probably not the same as the version of SAGE which you have. This is a problem, because the system does not work when there is a mismatch. Therefore you either have to move the old files so that**latex**cannot find them (and finds only your new files) or simply replace the old SageTeX with the new one. - Once all the above is done, you use SageTeX by first latexing a document (for instance
**fun.tex**), and then running SAGE from the command line on a generated file called**fun.sagetex.sage**. You are then supposed to run latex on**fun.tex**again, and then you get the desired output file. This *almost* works. In my case I got errors because an automatically generated python script**fun.sagetex.py**which is invoked by**fun.sagetex.sage**looks for a non-existent file**fun.sage**. All will be well if *before* you do**sage****fun.sagetex.sage**on the command line, you first do**cp****fun.sagetex.sage****fun.sage**. You will need to repeat this process every time you want to recompile**fun.tex**.

Technical problems aside, SAGE is an amazing software package, which has many applications. The fact that it is freely available means that it is not only free to us, but also to our students. When showing our students how to accomplish a technological feat we frequently have to choose a platform to teach them on (eg Linux vs Windows). There are ethical arguments which can be made that we are obligated to instruct them on the “most free” platform so that they are not “born dependent” on technology produced by a particular company. I believe this point carries water, but even if you disagree there are purely utilitarian reasons to teach our students on open source platforms when possible:

- The students will always have free access to the product
- Usually the open source version better shows the basic operational principles of what is happening
- Free and open source technology does not quickly become obsolete or stop being supported when a certain company fails or goes bankrupt. It is always around.

As I have said above, the cost of getting started in SAGE is relatively high, but I believe the technology is ultimately superior, to MAPLE at least. There are the open source/ free software arguments which can be included as a bonus incentive.

Thank you for providing us with such useful information.

Thank you for providing us with such useful information.

Thanks for sharing.

Thank you for providing us with such useful information.

Nice alternative to create it, thanks about that information

“That’s not as hard as it sounds, and for a lot of people it’s a piece of cake, but it’s frustrating to think that this will stop a lot of people from using this really amazing software”.

I have been planning to learn SAGE for long but, it tends to appear to me as if this great software is something difficult to access and learn. I am happy to have found your article. Great Share.

Siegmund Hübner, Inhaber des Hugo Sekt depot Xanten ist zufrieden mit jener Entwicklung seines Netz Handels.

Bieten bekommen sich eine Fülle prickelnder Hugo Sekt -Rezepte fuer jenes unvergessliches Menü zum Rendezvous – ob als

aromatische Saucen-Zutat zu Fisch sowie Fleisch oder als

schäumender Hugo Cocktail Schuss auch in raffinierte Süppchen und

auch Süßspeisen, Hugo Sekt verleiht fast allen fantasievollen

Küchen-Kreationen jenes gewisse Was. Auch bei besonderen Anlässen geht es sehr einfach nicht sehr ohne Hugo Sekt.

Write more, thats all I have to say. Literally, it seems as

though you relied on the video to make your point.

You definitely know what youre talking about, why waste your intelligence on just posting

videos to your weblog when you could be giving us something

enlightening to read?

Hi Hunter — don’t worry about your tone. That’s really useful feedback, actually. I’m glad to hear you found it very easy to get SageTeX working when you reinstalled. But we need to work on that. I’ve made a ticket in our issue tracker (http://trac.sagemath.org/sage_trac/ticket/14343) and hope to improve the installation guide so that it’s friendlier to beginning users, while still including information for more experienced folks.

Dear Dan,

Thank you for writing such amazing software! I am now a little embarassed about the tone I used in this blog post. At the time I was writing it I was feeling a little exasperated, and also probably a little too proud of myself.

I am not sure what foolish things I was doing last spring, but since writing the post I have upgraded to a fresh install of Linux Mint 14. Actually, I did this last week, and so I had not reinstalled SageTeX until a few minutes ago. When I did, it took me less than 5 minutes, using the 3rd option of copying /tex into /home/hunter/texmf.

The problem that I described about the sagetex token in the filename has disappeared.

You’re right that I was confused about exporting SAGE_ROOT. Maybe that was the source of my problems?

Whatever time I spent in trying to install the program was easily saved by using SageTeX when writing long project descriptions for my students.

The usefulness is second only to the coolness of using Sage from inside of LaTeX.

I have been using sage for almost a year. I started with FORTRAN, then moved to C++ (Boost), and then looking for some high level language I found R. R was nice, I discovered the power of a scripting language with R. But the R language is a bit awkward. I never felt comfortable with R. So I keep looking and I found SAGE. It is amazing! It is very powerful in many areas. First, it is basically Python which is probably one of the most versatile languages out there. In fact you can use SAGE in Python mode (I do that). You can install any Python package on SAGE. SAGE has its own Python incorporated so you don’t need to install anything extra, it is self contained, and even you can install it in your home directory if you don’t have root acess as is the case in computer clusters. Secondly, SAGE comprises a large and powerful universe of packages, always growing. From Scipy and Numpy for numerical and Matricial calculations, to Sympy and Maxsima for simbolic calculation. Also it contains its own graph package and Networkx for network theory. Much more stuff for combinatorics, number theory, etc… But also it incorporate R in a kind of Python mode! So all the statistical power of R is there for you. Even you can install R packages as if you were using R by its own. But the pearl is Cython. If your code is slow, then you can transform pieces of it just in plain C (which is one of the fastest languages out there) with usually a few modifications of your Python code. The integration of C and FORTRAN existing code is very easy too thanks to Cython. Also, there are many superb “experimental” Python packages (as pandas, graph-tool) out there that you can install as with any other Python distro. I have to say as a Phycist. SAGE made me my life far more easy :). And it is free and has a strong community out there!!

Hi, I’m the main author of SageTeX. I wanted to make some comments on your installation procedure. It’s true that getting it working is nontrivial, but it shouldn’t take so long.

Regarding step 1: you do NOT have to edit the system-wide bashrc file! The use of “SAGE_ROOT” in the installation documents does not mean you should export such an environment variable to every shell process! Just replace “SAGE_ROOT” with the actual root of your Sage installation. You can either do it manually, or set a temporary environment variable with

export SAGE_ROOT=/blah/blah

Point 4 is correct: TeXLive 2009 is still pretty common and the Debian-derived packages include an old version of sagetex.sty, which causes no end of trouble. You’re right that you can just delete that file, move it, or replace it with the correct version of sagetex.sty. It looks like Debian finally updated TeXLive, and the updated version is filtering into derivative distributions. (I’m not sure about other distributions, but I’m guessing they’re similar.)

Point 5: if you need to manually rename a file every single time, then something is terribly broken. The autogenerated .py file should not refer to the original .sage file at all, so I’m not sure why it would do that. What version of Sage are you using? What happens when you run Sage on fun.sagetex.sage?

I’m glad to see you persisted and got it all working, though! I’ll see about updating the installation instructions to be more helpful.

VirtualBox is very user friendly, I’ve used it on a few labs of my own. I had Mac OSX running on it.

-Joe

Sage looks awersome too bad we are studying R at university !

We are using R for our social media analysis project.

Guys download VMware Play for free and run Sage in that much easier than VirtualBox.

Jonas, if you look at the download options on sagemath, there are a lot of pre-compiled sage binaries, including a virtualbox image for windows.

These are good for someone who wants to just use sage, but building becomes the better option if you want to play around with the code (apply patches from http://trac.sagemath.org/ for example).

Suzanne, I’m a high school teacher who also uses Sage and R.

One of the biggest advantages of Sage is that it has the notebook interface which makes it really easy to investigate data/mathematics interactively.

The second advantage is that it is possible to run a pure R session from inside the notebook, so you get the familiarity and power of R, inside a modern and satisfying-to-use GUI.

downloading the source and compiling it from scratch

FYI, there is a PPA (personal package archive) repository which makes installing sage in Ubuntu a breeze. See:

https://launchpad.net/~aims/+archive/sagemath

Pingback: Round Up! : Footenotes

Jonas, thanks!

Michael, good points. Maybe the Wubi installer is easier than a virtual machine? That would help with the resources issue. My machine has 8gb but virtualized os’s seem to run well — I am really impressed with virtualbox. I have windows virtualized within linux and it is very useable.

BTW I just stumbled across the blog of SAGE’s developer William Stein: http://sagemath.blogspot.com/

Suzanne, mostly pretty pictures ? Actually I have to start learning R because of a project I’m doing with a student. It apparently has built in support for some statistical learning machines? That seems interesting. I saw that SAGE 5 will have a built in SVM but it doesn’t seem to be working yet. That would be nice.

I’ve never had to do many numerical things because I’m a logician, so I’m very naif-ish about MATLAB and octave.

Dr Stein seems to have some definite (choleric? :)) ideas about octave: http://sagemath.blogspot.com/2007/12/why-isnt-sage-just-part-of-octave.html

SAGE looks great! What made you learn SAGE instead of something like R or Octave? I’d love to hear what motivated your decision.

Recently, I started using R and I’ve come to really, really like it. I made the effort to learn it when it became more obvious to me that my Matlab habit was unhealthy. I’m not sure that I’ve heard of SAGE before, so if you are aware of any relative advantages of the platform over R or Octave, please share!

… Or, you can try the online Sage Notebook simple by creating an account! No mess!

http://www.sagenb.org/

Just note that, to run an entire second operating system on your computer by installing VirtualBox, then Ubuntu, you’ll need a fair amount of hard disk space and RAM. If your machine’s strong enough, though, you get to test Linux as well (which I know you’ve been dying to do, if you only had the time and patience…). This is the perfect reason to do both at once

I’ve been wondering about SAGE for some time now — open-source alternatives are in common usage for most applications, but this is one area where I’ve never had the opportunity and motivation to look beyond Maple/MATLAB/Mathematica. Great post! And beautiful pictures, too. It seems that the back-end is all there — we just need some techie wizards to smooth the installation process… When I’m feeling bold (bored?) I’ll give it a plunge, and I’ll start with this post.

-Jonas