Symbolic mathematics on Linux

This article brought to you by LWN subscribers Subscribers to LWN.net made this article — and everything that surrounds it — possible. If you appreciate our content, please buy a subscription and make the next set of articles possible.

This article is an introduction to the world of free and open-source applications for symbolic mathematics. These are programs that assist the researcher or student through their ability to manipulate mathematical expressions, rather than just make numerical calculations. I'll give an overview of two large computer algebra packages available for Linux, and a briefer sampling of some of the more specialized tools aimed at particular branches of mathematics.

This category of software is traditionally called a "computer algebra system", but that description can be misleading. These systems can find analytic solutions to algebraic and differential equations; solve integrals; sum infinite series; and generally carry out nearly any kind of mathematical manipulation that can be imagined. At the least, symbolic mathematics software can replace the bulky handbooks of mathematical information that have been lugged by generations of graduate students.

Over decades, mathematicians have honed these programs, encoding within them the accumulated mathematical knowledge of centuries: information about special functions, for example, that's so difficult (for some of us) to remember. They have learned to reduce such things as algebraic simplification and calculating derivatives to patterns of symbol manipulation ripe for automation. The earliest of these systems, developed in the 1960s, were based on Lisp, the obvious choice at the time, but development of later systems used a variety of languages.

Fortunately, most of the best of this software is free and open source, which allows us to look under the hood and examine or alter the algorithms employed.

Maxima

The ancestor of all symbolic mathematics systems is Macsyma [PDF]. It began as an academic project at MIT in the 1960s, but it was eventually licensed to Symbolics (a company founded by former MIT people), which sold it throughout the 1980s. A fork of an earlier version became Maxima, which remains the predominant free-software symbolic mathematics solution. Maxima is actively developed, and has attained a high degree of sophistication and completeness; source is licensed under the GNU GPLv2.

On Ubuntu, at least, the packaged version is quite close to the latest development version; if your distribution lags, you can build it from the sources in the Git repository on SourceForge. If you intend to do any real work with the program you should also install the "maxima-share" package (after installing Maxima itself). This will install a large number of extra mathematical libraries that are used transparently by Maxima and make it far more capable.

The traditional way to use Maxima is in a terminal. Just type " maxima ", and you will be presented with an interactive command prompt. You can enter mathematical expressions in the customary computerized dialect (x^2 for x squared, etc.), followed by a semicolon, and Maxima will respond with a simplified or "solved" version of the expression. If Maxima can't do anything with the expression, it usually just repeats it: tell it "foo" and it will respond with "foo". Readline support allows you to recall previous inputs and edit them, which is particularly convenient for the type of exploration that Maxima seems to inspire, at least for this user.

But if you tell it:

integrate(%e^(-x^2), x);

Maxima will respond with something that looks like:

sqrt(%pi) erf(x) ---------------- 2

This example is here to illustrate three things: First, special numbers such as π and e are represented, in input and output, as %pi and %e . If you say, for example, %e , Maxima will just say %e back; to see the numerical value of the constant, say float(%e) .

Second, mathematical expressions in the output are, by default, rendered in an ASCII approximation of mathematical notation, making their structure easier to grasp than the computerese that we are obliged to use for the input. You can enter tex(%) to get the result in a form ready for pasting directly into a TeX or LaTeX document. (The symbol % refers to the immediately preceding output; each input and output is numbered, and can be referred to directly, as %o6 for output number six, %i6 for input number six, etc.)

Third, there is a wealth of mathematical knowledge baked in to Maxima. Notice the erf in the numerator of the answer: this refers to the error function, well known to statisticians. Maxima will provide the results of integrals and the solutions of differential equations in terms of the special functions that it knows about, when appropriate.

If you'd like to know what that error function looks like, you just need to say:

plot2d(%, [x, -2, 2]);

That will pop up a plot like the one at the right.

Remember, the % notation refers to the last result returned. The three terms in square brackets are the variable on the horizontal axis and its range. Maxima uses gnuplot for its plotting. If you're familiar with gnuplot, you can add options to the plot command to tweak the plot's appearance, or set global options that will apply to every plot in the session. If you have a high-resolution screen you may want to apply these global options to make the plots easier to see:

set_plot_option( [gnuplot_preamble, "set termoption font 'courier,24'; set termoption lw 4"]);

In fact, these options were used for the example plots in this article.

Maxima also provides an interface to gnuplot's 3D plotting commands. Here's a simple example of a surface plot:

plot3d(sin(x)*cos(y), [x, 0, 2*%pi], [y, 0, 2*%pi]);

By setting the gnuplot preamble, either globally or per plot, you can access contour, parametric, or any of gnuplot's other plotting modes. The plots use the x11 gnuplot terminal. When you interact with them, you are interacting directly with the gnuplot subsystem, so you can use the mouse to zoom 2D and 3D plots and rotate 3D surfaces.

Using Maxima at the terminal is convenient because it starts instantly, is responsive, and there is nothing extra to install. However, the console interface has some disadvantages: the ASCII output is not easy on the eyes, especially after a long session; there is only one plot window, which gets reused for each plot; and there is no convenient record of your session.

There are a handful of alternative interfaces that solve one or more of these problems. None of them are ideal, nor as capable as the solution that we'll see in the next section; but I'll briefly describe them here, to provide an idea of what's available, and in case any one of them hits a sweet spot for the reader.

Two slightly more graphical interfaces to Maxima are Xmaxima and wxMaxima. The first of these is based on Tk and the second on wxWidgets. They both allow plots to be embedded with the input and output, and allow for various options to save the session, which is useful for creating notebooks or documents from Maxima explorations. Neither one seems to provide true typeset output, although wxMaxima can use the jsMath fonts to make the results somewhat more attractive. wxMaxima can serve as a gentler introduction to Maxima for those who prefer to get started without frequent trips to the manual, as it bristles with menus and dialogs that expose some, but far from all, of the program's options.

As we saw above, Maxima knows how to create TeX versions of its output. Therefore it should be possible to simply run TeX behind the scenes and display math that looks like real math. There are at least two interfaces that follow this strategy. The WYSIWYG editing platform TeXmacs can interface with Maxima and display typeset output, but this is probably mainly of interest to those who are already using TeXmacs. Perhaps of more general interest, especially to Emacs users, is the imaxima mode of that editor, where you can embed plots and fully typeset output directly into the editing buffer. A readline-like functionality is simulated by typing M-p instead of up-arrow.

Installation of imaxima can be an adventure. Several files need to be placed in the correct places, and variables set in the .emacs configuration file. In Ubuntu this can all be done automatically by installing the maxima-emacs package. The downside here is that this package will install large chunks of TeX Live. As the typical user of Maxima is likely to already have installed a more up-to-date version of TeX Live than most Linux distributions' package managers provide, this will create some redundancy — but it does save time. Some alternatives for those who already have TeX installed are to download the Maxima source, which includes the most critical required files, or to try to find recent versions of the imaxima mode files on the web. Either way, smooth operation is likely to require some customization in your .emacs file, some of which can be set through the Emacs user options interface, which exposes some imaxima settings; see the imaxima link above to get started.

The next figure shows part of a Maxima session in emacs using the imaxima interface. The regular plot commands can be used as in the terminal, and separate gnuplot windows will pop up. To get embedded graphs, use the wxplot2d() and wxplot3d() analogues. With the use of comments, for which Maxima uses C-style syntax, the Emacs buffer can become a notebook in the Jupyter style (in fact there is also a Maxima kernel for Jupyter). The notebook can be converted into HTML or LaTeX with the Emacs commands imaxima-to-html and imaxima-latex , respectively, making it a convenient way to generate lecture notes or parts of papers. The HTML export is serviceable out of the box, but the current state of LaTeX export fails to create correct graphics insertion commands, requiring some extra ad hoc post-processing steps.

The Emacs session depicted also illustrates a few additional Maxima features, such as the use of infinity, sums, and defining functions.

In the space available, I can't even scratch the surface of all the math that Maxima can do. It has grown by accretion over the decades, and continues to grow, as mathematicians take advantage of its extensible nature to teach it the secrets of their discipline's sundry specialties. Maxima is especially congenial to the Lisp programmer, who can drop into its Lisp subsystem at the interactive prompt and make contact with the internal representation of its mathematical expressions.

Sage

In 2006 a single developer, Ondrej Certik, started a project called SymPy, which is a symbolic mathematics program, like Maxima, written entirely in Python. It grew quickly, and is now a mature project with scores of contributors. SymPy can be used from a specially wrapped IPython, providing an experience similar to Maxima in the terminal, or as a library. It would deserve its own section in this article were it not easier to discuss it in the context of Sage.

Sage is an enormous system for symbolic and numerical mathematics. It is unique in presenting a unified interface to 90 distinct components. Behind the scenes, Sage will automatically use the appropriate component to perform the calculation or manipulation that the user desires; or, when there is more than one way to do something, Sage can be directed to use a particular library or algorithm.

Because of this, Sage can handle optimization problems, astronomical calculations, elliptic curves, number theory, interval arithmetic, networks, cryptography, statistics, Rubik's cubes, ray tracing, and much else that I don't understand. It can display 2D and interactive 3D graphics, including visualizations of molecular structures. It includes embedded versions of Python (with NumPy and many other Python libraries), R, SciPy, SymPy, Maxima, and many other subsystems. Sage even claims to be able to outsource computations to the Wolfram Alpha computational engine, but in my testing this did not work.

Naturally, Sage is a big download. The usual way to install it is to download a 1GB archive from headquarters and expand that to a 3GB directory tree. Start it by typing ./sage in a terminal from the SageMath directory. This approach is convenient, but may result in some redundant installations on your system. On Ubuntu, at least, there is also a PPA that will allow installation through its package manager.

Sage is based on Python rather than Lisp. It has two interfaces, each of which presents a version of Python with superpowers. After typing the sage command you will be faced with a wrapped IPython prompt, as when using SymPy. You can interact with Python normally, but you will find a few alterations in syntax; for example, ^ is used for exponentiation rather than ** . You can perform symbolic math manipulations (and numerical calculations) from the prompt just as with Maxima, but with a somewhat different syntax (no semicolons required). There is no need to import any libraries, as everything has been set up within the special IPython environment. You can plot your results, but rather than gnuplot Sage uses matplotlib for 2D graphics and the Jmol Java molecular viewer to put 3D graphs in a window where you can interact with them.

Where Sage really shines is in its notebook interface. If you type notebook() at the IPython prompt, Sage starts a web server and connects it to a new tab in your browser. Here you can talk to Sage just as in the IPython interface, but the responses will be typeset by jsMath, which uses an embedded JavaScript version of the TeX mathematical typesetting algorithms. Thanks to AJAX and other JavaScript magic, interaction using the notebook is smooth; there are keyboard shortcuts for evaluation and for manipulating the "cells" into which the notebook is organized. Graphics are embedded into the page, and you can interact with 3D plots directly, zooming and spinning with the mouse. With a single click, the notebook can be converted into a neat HTML version suitable for printing or for use as a web page. You can even share the live notebook online.

The figure shows an extract from a Sage notebook in Firefox, while playing with the included graph theory packages. It gives some idea of the appearance, but cannot convey the fun of using Sage. This project has succeeded in gathering a carefully curated set of components and presenting them with a unified interface that is powerful and enjoyable to use.

Specialized packages

Maxima and Sage, powerful and wide-ranging as they are, are the general practitioners of the symbolic math world. Sometimes one needs to consult a specialist. Here I survey a few of the more narrowly focused mathematics systems, some of which are research projects, such as a system for symbolic computations in general relativity.

A related project is Cadabra, designed to help with the tensor manipulations and other math used in field theory. Cadabra is unusual in that the input language as well as the output are a subset of TeX. It's available as a reasonably up-to-date Ubuntu package.

While on the subject of physics, the FORM project is popular with particle theorists. Interaction is through text files that define the computation to be performed, and which can be processed in a multi-threaded, multi-processor, or sequential style.

Fermat, the personal project of Robert H. Lewis of Fordham University, specializes in polynomial and matrix algebra over the rational numbers and finite fields. It's the best in class for the specialized set of algebraic problems that it's designed for.

The CoCoA (Computations in Commutative Algebra) System specializes in polynomial systems and commutative algebra. It can employ Gröbner basis computations, unlike Fermat. It can also do map coloring and logic problems.

Macaulay2 concentrates on algebraic geometry and commutative algebra. It's been supported by the National Science Foundation since 1992. You communicate with Macaulay2 through a its own specialized, interpreted language.

TRIP specializes in perturbation series computations, specially adapted to celestial mechanics. In development since 1988, TRIP does both numerical and symbolic work, and is integrated with gnuplot.

I hope this incomplete roundup of a handful of specialized mathematics packages in this section provides a general impression of the range of activity in this field. For anyone with even a nascent interest in mathematics, systems like Sage, in particular, can make exploration of this world a great deal of fun.