Monday, October 25, 2010

Revision Control and Sage

 The main target audience of this blog post is mathematics researchers who are relatively new to revision control in the context of Sage.

Background


In the 1990s when I first started using revision control, it was a major, major pain in the arse. Making a tarball snapshot of the project directory with a date-stamp was better! In 2005 when I started Sage, I didn't know any better, and didn't both with revision control for the first year. In 2006, Gonzalo Tornaria told me that things had improved, and got me to use Darcs. That lasted just over a year, when I switched Sage to use Mercurial. The switch was due to major headaches installing Darcs (a Haskell program), and Darcs hanging when our repo got too big and complicated.

We are fortunate that today many difficult problems and design decisions for revision control have been addressed through lots of work. Revision control is amazingly good now compared to what it was 15 years ago!

Some milestones: RCS, CVS, Subversion, Bitkeeper & Monotone, Darcs, Git/Mercurial/Bazaar

A reference: see the Mercurial guide.

Getting started with Mercurial

In sage we use "hg" = (Mercurial). Why?

- Because I chose it in early 2007, when Darcs started sucking too much
- Distributed is the way to go these days. (Modern)
- Bazaar far too unstable in 2007, to put it mildly. Bazaar is by the people who made Ubuntu, and is much more stable today.
- GIT badly documented, too hard to setup, etc... back in 2007; again, git is much better today.

---> so Mercurial was the only reasonable choice then. It was just "good enough" at the time that we could survive. Hence Mercurial.

Mercurial comes with Sage, and you can use it like so:
  - "sage -hg"
  - sage: install_scripts command
  - sage: hg_sage.[tab]

- Or Install: easy to install standalone on OS X, Linux, and Windows.

- You can do "$ sudo easy_install mercurial" to install systemwide the latest version into your default system wide Python.   It will probably appear in /usr/local/bin/hg

Documentation for hg:
- http://mercurial.selenic.com/
- http://hgbook.red-bean.com/
- http://mercurial.selenic.com/learn/
- http://hginit.com/

Project Hosting


If you create your own HG projects, you can easily host them on Google Code (and also http://bitbucket.org/).

I'm doing this a lot lately with smaller projects, including:
- Purple Sage
- Sage Notebook

- Other systems. Google code only does HG and Subversion.
But there are also similar hosting services for git and bazaar:
- github -- free hosting for git projects
- Bazaar's Launchpad

- Sourceforge: The first major free hosting site I heard of is sourceforge.net, which offers hosting for all sorts of repos, including mercurial. It is more general, but not as "clean and focused" in abilities as the above, e.g., their mailing lists are often plagued by spam, and I seem to recall a lot of banner ads.

These hosting services are a very popular and fairly new thing (but not too new); all but sourceforge became popular well after I started Sage. They provide much, much more than just what the revision control systems do; they are more than just a place to host your code repository. They all provide:

- code hosting (of course)
- bug tracking
- code review
- mailing lists, wiki's, etc.

Sage isn't hosted on one of these probably because they are so new, and Sage is older. This may change.  Also, Sage is really, really big (Google code has a 2GB limit).

Main point: if you want to start a small project with a small number of other people -- example, writing a research paper and some corresponding code -- you can in seconds easily get the full suite of hosting, bug tracking, code review, etc., totally for free, backed up, high quality, etc. Obviously, you are trusting your data to either Google, Github, or Canonical, but you can easily backup most of it (e.g., the repos, mailing lists).

They want you to use them: "431,000 people hosting over 1,330,000 git repositories" (from github)

Story: In 2006, when I came to UW, Randy Leveque (a professor in applied math) found out about me running the Sage project, and wanted to know how to get Mercurial, Wiki's, trac, etc., setup, so that he could organize his software development project(s). I explained some basics, and I think he and his students setup various project infrastructure, and ran into assorted troubles. Today, I would just say "http://code.google.com" (or github, etc.) and be done with it! And Randy would be up and running with more tools than before in a matter of minutes.

Command line mercurial


The main point of the rest of this lecture will be about how to use Mercurial. It's very hard to beat the official Mercurial Quickstart, which we'll just go over together in class:

See the Mercurial Quickstart.

- Make a directory in your computer be under revision control
- Setup your .hgrc file, if you haven't already.
- Try cloning a remote repository, e.g., purple sage
- Try committing a change that spans multiple files, so you can see that all the changes are together. Then view the commit via "hg serve -p 8200".

WARNING: on OS X with the sage-included hg, this "hg serve" crashed for me with "Trace/BPT trap" when I visited http://localhost:8200. I opened trac ticket 10171.


So I did "sudo easy_install mercurial" to get a new version systemwide, and now this works for me: "/usr/local/bin/hg serve -p 8200"

A future blog post may cover more advanced HG topics, including branching, cloning, queues, and bundles.

Monday, October 4, 2010

Standalone C/C++ libraries that are included in Sage

Every copy of Sage includes many free open source C/C++ libraries, which can also be used standalone without Sage. They got into Sage because enough people really cared about using them, and felt it was worth the effort to convince other people that these should be included in Sage. Thus every single library also provides some key functionality to Sage. Thus understanding the main points about these libraries should be of interest to anybody who knows C/C++ who wants to do mathematical computation. Moreover, most of these libraries were written in C/C++ because the authors had already mastered the underlying algorithms, and really wanted an implementation that was extremely fast, often potentially orders of magnitude faster than what they might implement using only an interpreter, such as Python, Maple or Magma.

It's also possible using Cython (or even ctypes) to directly call any of the functionality of the libraries below from Python (hence Sage). Thus it is vital that you know about these libraries if you want to write fast code using the Sage framework.

When you want to look at the source code for any of these libraries, you can download the Sage source code from here or here, unpack it, look in the directory sage-x.y.z/spkg/standard/, and for any "spkg" (=tar, bzip2) file there, unpack it, e.g.,

wstein@sage:$ tar jxvf mpir-1.2.2.p1.spkg
wstein@sage:$ cd mpir-1.2.2.p1/src/
wstein@sage:$ ls
acinclude.m4     doc               missing        
...

Note that you can't unpack the spkg files included in a binary of Sage, since they are actually empty placeholders.

You can also find direct links to all of the standard spkg by going to this page, along with short descriptions of each.


Arithmetic

  • mpir (written in C and assembler; has a C++ interface): this is a library for doing fast arithmetic with arbitrary precision integers and rationals. It's very fast and cross-platform, and it's used by many other libraries. (NOTE: MPIR was started as an angry fork" of GMP=Gnu Multiprecision Library. GMP is used by Magma, Maple, and Mathematica; I've been told Mathematica doesn't use MPIR because of the overlap of Sage and MPIR developers, and their general suspicion toward the Sage project.)
  • mpfr (C library with C++ interface): this is a library for doing arithmetic with fixed precision floating point numbers, i.e., decimals with a given fixed choice of precision. "Floating-point numbers are a lot like sandpiles: Every time you move one you lose a little sand and pick up a little dirt." [Kernighan and Plauger]. MPFR is used by Magma (for details, google for "mpfr magma"), and several other libraries and systems out there. The number theory system and library PARI does *NOT* use MPFR; since PARI also does arbitrary precision floating point arithmetic, you see that PARI includes alternative implementations of some of the same algorithms (note: PARI also has its own alternative to MPIR). MPFR is unusual in that the main author -- Paul Zimmerman -- is unusually careful for somebody who works with floating point numbers, and my understanding is that all the algorithms in MPFR are proven to give the claimed precision. Basically, MPFR generalizes IEEE standards (that define what floating point arithmetic means) to arbitrary precision. MPFR also supports numerous "rounding modes". The mpmath Python library is in some cases faster than MPFR, but it has very different standards of rigor.
  • mpfi (C library): mpfi is built on top of MPFR, and implements arbitrary precision floating point "interval arithmetic". Interval arithmetic is an idea where you do arithmetic on intervals [a,b],
    and apply functions to them. Given two intervals, their sum, product, quotient, etc., is another interval. The actual "feel" of using this is that you're working with floating point numbers, and as you lose precision due to rounding errors, the computer keeps track of the loss every step of the way... and in some cases you can quickly discover you have no bits left at all! MPFI is nicely integrated into Sage (by Carl Witty), and is a pretty sophisticated and robust implementation of interval arithmetic, on which some other things in Sage are built, such as the field QQbar of all algebraic numbers.
  • flint (C library): "Flint" is an abbreviation for "Fast Library for Number Theory". However, I've listed it here under basic arithmetic, since so far in Sage at least we only use FLINT for arithmetic with polynomials over Z[x] and (Z/nZ)[x]. (Sage should use it for Q[x], but still doesn't; see the horendously painful trac ticket, number 4000!) The main initial challenge and reason for existence of flint was to multiply polynomials in Z[x] more quickly than Magma (hence any other software on the planet), and FLINT succeeded. Now FLINT also does many other relevant polynomial algorithms, e.g., GCD, addition, etc. Most new development is going into "FLINT2", which is a total rewrite of FLINT, with an incompatible C interface. Bill Hart's vision is that eventually FLINT2 have capabilities sort of like PARI. Sage users have found at least one serious bug involving f,g in Z[x] where f*g was computed incorrectly.
  • znpoly (C libary): (by David Harvey) one version of znpoly is included in FLINT, and another is a standalone package in Sage. The point of this library is to do fast arithmetic in (Z/nZ)[x]. ("Monsky-Washnitzer!" we once found a very subtle bug in znpoly-in-FLINT that only appeared when multiplying polynomials of huge degree.)
  • givaro (C++ library): in Sage we use it entirely for fast arithmetic over small-cardinality finite fields, up to cardinality "2 to the power 16". Givaro makes a log table and does all multiplications as additions of small ints. Givaro actually does a lot more, e.g., arithmetic with algebraic numbers, and a bunch of matrix algorithms, including Block Wiedemann, which we don't even try to make available in Sage.

NOTE: The C library API conventions of mpir (gmp), mpfr, mpfi, flint and zn_poly are all pretty similar. Once you learn one, the others are comfortable to use. Givaro is totally different than the others because it is a C++ library, and hence has operator overloading available. The upshot: in Givaro, you type "c=a+b" to add two numbers, but in the other libaries (when used from C) you type, maybe "mpz_add(c,a,b)".


Number Theory


  • pari (C library; interpreter): a hugely powerful C library (and interactive interpreter) for number theory. This library is very, very good and fast for doing computations of many functions relevant to number theory, of "class groups of number fields", and for certain computations with elliptic curves. It also has functions for univariate polynomial factorization and arithmetic over number fields and finite fields. PARI has a barbaric stack-based memory scheme, which can be quite annoying. Key Point: PARI is easy to build on my iPhone, which says something. That said, we have Neil Sloane, 2007: ``I would like to thank everyone who responded to my question about installing PARI on an iMAC. The consensus was that it would be simplest to install Sage, which includes PARI and many other things. I tried this and it worked! Thanks! Neil (It is such a shock when things actually work!!)'' PARI is very tightly integrated into Sage.
  • ntl (C++ library): a C++ library aimed at providing foundations for implementing number theory algorithms. It provides C++ classes for polynomials, vectors, and matrices over ZZ, Z/pZ, GF(q), (Z/pZ)[x], and RR, and algorithms for each. Some algorithms, e.g., for polynomial factorization in ZZ[x], are very sophisticated. Others, e.g., Hermite form of matrices over ZZ, are naive. In fact, most of the matrix algebra in NTL is naive, at least compared to what is in IML, Linbox, Sage and Magma. NTL is very stable and considered "done" by its author.
  • eclib (C++, standalone program): This is John Cremona's prized collection of C++ code that he wrote for computing elliptic curves, both finding them (using "modular symbols") and computing tricky things about them (via the "mwrank" program). It's a large amount of C++ code that Cremona developed over nearly 2 decades. There is still much functionality in there that could be made more readily available in Sage via Cython, but for which nobody has put in the effort to do so (I'm sure Cremona would be very helpful in providing guidance).
  • lcalc (C++ library, standalone program): This is Mike Rubinstein's C++ library for computing with "motivic L-functions", which are generalizations of the Riemann zeta function, of great importance in number theory. It's particularly good at computing tons of zeros of such functions in their critical strip, hence getting information about generalizations of the Riemann Hypothesis. It's the best general purpose L-functions program for computing zeros.
  • sympow (standalone C program): Though technically not a library it could be adapted into one. Sympow is a C program written by Mark Watkins for quickly computing special values of symmetric power L-functions (so related to what lcalc does). It has no dependencies (instead of PARI), because Mark didn't want to have to license sympow under the GPL.
  • ratpoints (C library): Michael Stoll's highly optimized C program for searching for certain rational points on hyperelliptic curves (i.e. of the form y^2 = f(x)). This library goes back to the early 1990s and work of Elkies. I believe this is in Sage for one reason only: Robert Miller wanted to implement 2-descent, and he needed this. (Unfortunately, Miller's implementation is only done in the case when there is a rational 2-torsion point.) This library could be made more useful in Sage, via making it so functionality in sage.libs.ratpoints is easily available for hyperelliptic curves...
  • genus2reduction (standalone C program that uses PARI): Not technically a library. This a C program that depends on PARI, and quickly computes "stuff" about genus 2 curves over the rational numbers... which as far as I know is fairly unique in what it does. A. Brumer used it recently for a large scale computation, and pointed out that it has stability issues.
  • flintqs (C++ program): This is a standalone C++ program that implements the quadratic sieve algorithm for integer factorization (which is good at factoring p*q with p and q of similar size and p*q up to maybe 100 digits). I think that this algorithm is also implemented in PARI, though in some cases flintqs is faster. A silly fact is that a better version of this program is evidently in FLINT itself, but for some reason Sage doesn't use it.
  • ecm (C library and C program; uses MPFR, MPIR): This implements the elliptic curve integer factorization algorithm, which Hendrik Lenstra invented, and is good at factoring p*n with p a prime with up to 30 (?) or so digits. There is another highly optimized implementation of the same algorithm as part of PARI, but ECM is in some (all?) cases better. It also has a huge number of tunable parameters. The ECM program is also often run standalone by people, sometimes on clusters, to attempt to factor integers. NOTE: Sage has a "factor" command, which just calls PARI. One could imagine it calling ECM and flintqs, etc., but nobody has made it do so... though many have tried.

Linear Algebra


  • IML (C library): "Integer matrix library". This library solves A*X = B, with A and B matrices over the integers. It does this by solving "A*X = B (mod p^n)" for several prime powers p^n, which involves computing the reduced row echelon form of A mod p (for various p) quickly... which is in turn done via matrix multiplication over matrices with double precision entries. It is the fastest software in the world for solving AX=B in many cases, especially as the entries of A and B get huge.
  • Linbox (C++ library): asymptotically fast black box linear algebra algorithms. It was just a "research sandbox", but "we" (mainly Clement Pernet) got Linbox whipped into shape enough to be of use in Sage for certain things. It is used at least for: matrix multiplication and computation of characteristic polynomials of matrices over the integers and finite fields, and asymptotically fast echelon form of matrices over finite field. I don't think it is used for much else yet. Implementing fast versions of the above in general is a gargantuan task, and we had done much of it (but no good charpoly!) natively in Sage before using Linbox, so now multiple implementations are available -- Linbox's are usually faster. Until Linbox/Sage, there were no fast open source programs for asymptoically fast exact linear algebra -- Magma was the only game in town (neither Maple or Mathematica are good at this either).
  • m4ri (C library): "Mary" -- scary fast arithmetic with matrices over the finite field GF(2) with 2 elements. This library uses various tricks (involving so-called "Gray codes") and asymptotically fast divide-and-conquer algorithms to compute reduced row echelon form and do matrix multiplication with matrices over GF(2). It is memory efficient and is the world's fastest at many benchmarks. There is related work (on "m4ri(e)") to do arithmetic over GF(2^n) and also some work by Boothby and Bradshaw on GF(p^n) for small p and n.
  • libfplll (C++ library): floating point LLL. The whole point of fpLLL is to implement very fast algorithms for computing LLL reduced basis for lattices, possibly with rounding error if you are not careful. LLL gets used as a key algorithm in many other algorithms (e.g., polynomial factorization). Magma also uses some variant of libfplll, and an older rewritten version of fpLLL is in PARI. If you look, e.g., at Mathematica, it does have some sort of limited LLL implementation, but it is nowhere near as sophisticated as what is available overall in Sage... NTL and PARI also have their own interfaces to LLL, which can have various pros and cons over using fpLLL directly. (In Sage, you can make a matrix over the integers and call the LLL or LLL_gram methods on it.) Magma also uses fpLLL.
  • polybori (C++ library): commutative algebra with boolean rings, which are characteristic 2 rings such that x^2 = x for every element of the ring. Evidently, polybori is important in cryptography and circuit design (?). I have personally never succeeded in really understanding how or seeing for myself polybori do well on benchmarks. Polybori is why the boost-cropped package is in Sage, which provides some C++ datastructure and algorithms.

Combinatorics


  • symmetrica (C++ library): a C++ library for doing very fast arithmetic with symmetric functions. The combinatorics code in Sage builds on this.
  • cliquer (C library): for finding cliques (=maximal complete subgraphs) in a graph.


Geometry


  • gfan (C++ program): "groebner fans" -- for computing a geometric object that parametrizes all Groebner basis for an ideal.
  • cddlib (C library): for computing with convex polytopes that gfan requires, using the "double description" method (whatever that is). There is code in sage.geometry.polyhedra that also uses cddlib.
  • glpk (C library): the GNU linear programming toolkit. This is a good free open source program for "linear programming". There are also commercial programs (e.g., CPLEX) for linear programming, and whenever people talk about linear programming software they always point out that some commercial programs are way, way faster than glpk. But glpk is in Sage.
  • palp (C program): computes stuff about lattice polytopes, like all the integers points in one. There is a big Sage package that builds on top of this. Evidently, this is of substantial interest to some physicists.

Numerical applied math

  • gsl (C library): implements core algorithms of scientific computing, is very stable and considered "done" by its authors. New relevant code goes into other libraries that use GSL.

Non mathematics


  • readline: provides command line editing, which is used by IPython, PARI, Singular, etc. Often misbuilt or not available on various platforms, so included in Sage. Readline is perhaps famous for being a key reason that both PARI and clisp are licenced under the GPL.
  • bzip2: bzip2 compression; easily usable from Sage via "import bz2". NOTE: this is used by the Sage build system so it is in SAGE_ROOT/spkg/base instead of SAGE_ROOT/spkg/standard/.
  • zlib: gzip-compatible compression; makes it so you can easily "gzip" files, and even work with gzip'd files in memory from Python.

Sunday, September 26, 2010

The Sagemath Cluster's Fileserver

In addition to being involved in programming in Sage, I am also the systems administrator for the "Sagemath cluster" of computers, which are used for Sage development, and also support mathematics research. This is a cluster of 5 machines with 128GB RAM and 24 cores in each machine, along with a 24 terabyte Sun X4540 fileserver.

When we setup this cluster nearly 2 years ago, we installed OpenSolaris and ZFS on the fileserver. It's run very solidly, in that the uptime was over 600 days a few days ago. However, no software would really work well on OpenSolaris for me -- top crashed usually, emacs crashed, stuff just didn't work. I never got Sage to build. Also, USB disks were a major pain on this machine, which complicated backups. Also, I frankly found the performance of ZFS and NFS-from-ZFS to be disappointing. In addition, nobody had a clue how to do maintenance and security updates on this server, meaning it was probable a danger.

Then Sun got bought by Oracle, and Oracle killed OpenSolaris. Also, I started getting really into MongoDB for database work related to the modular forms database project, and it would be nice to be able to run a MongoDB server and other related software and servers directly on my fileserver (yes, MongoDB supports Solaris, but...). Getting anything to work on Solaris costs too much time and confusion. So I decided to delete OpenSolaris and install Linux. Since there's many terabytes of data on the fileserver, and dozens of people using it, this involved many rsync's back and forth.


I eventually succeed in installing Ubuntu 10.04.1 LTS Server on disk.math. I setup a big 20TB software RAID-5 array on disk.math (with 2 spares), and added it to an LVM2 (=logical volume management) volume group.

I created 3 partitions:

        home -- 3.5 terabytes:  for people's home directories
        scratch -- 3.5 terabytes;  for scratch that is available on all machines on the cluster
        lmfdb -- 3.5 terabytes;  for the L-functions and modular forms database project.

Thus over 7.5 terabytes is not allocated at all right now.  This could be added to the lmfdb partition as needed.

The RAID-5 array has impressive (to me) performance:

   root@disk:/dev# /sbin/hdparm -t /dev/md0
     /dev/md0:
     Timing buffered disk reads:  1638 MB in  3.00 seconds = 545.88 MB/sec

All the above 3.5TB partitions are NFS exported from disk.math, and (will all be) mounted on the other machines in the cluster.

By using LVM, we will still get snapshotting (like ZFS has), which is important for robust backups over rsync, and is great for users (like me!) who accidentally delete important files.

I chose 3.5TB for the partitions above, since that size is easy to backup using 4TB external USB disks. Now that I'm running linux on disk.math, it will be easy to just plug in a physical disk to the fileserver and make a complete backup, then unplug it and swap in another backup disk, etc.

----

Some general sysadmin comments about, in case people are interested.

   (1) Oracle shutdown access to firmware upgrades, which meant I couldn't upgrade the firmware on disk.math.  Maybe it would have been possible via phone calls, etc., but I was so pissed off to see Oracle do something that lame.  It's just evil to not give away firmware upgrades for hardware.  Give me a break.  Oracle sucks.   I'm really glad I just happened to upgrade the firmware on all my other Sun boxes recently.

   (2) The remote console -- which is needed to install from a CD image on disk.math -- does not work correctly on 64-bit OS X or 64-bit Linux.    It just fails with "not supported on this platform" errors, but with no hint as to why.   By installing a 32-bit Linux into a virtual machine, I was able to get this to work.

  (3) Linux NFS + software RAID + Linux LVM2 (=logical volume management) does roughly "the same thing" as ZFS did on the old disk.math machine.   I spent about equal time with the documentation for both systems, RAID+LVM2+NFS and ZFS, and the documentation for RAID+LVM2+NFS is definitely less professional, but at the same time vastly more helpful.  There's lots of good step-by-step guides, forums, and just generally a *ton* of useful help out there.  With the ZFS documentation, there is basically one canonical document, which though professional, is just really frustrating as soon as you have a question not answered in it.     I personally greatly prefer the Linux ecosystem.

  (4) RAID+LVM2+NFS is much more modular than ZFS.   Each part of the system can be used by itself without the other parts, each has its own documentation, and each can make use of the other in multiple flexible and surprising ways.    It's very impressive.    One of the reasons I chose ZFS+Solaris instead of Linux+whatever 2 years ago is that I didn't realize that Linux offers similar capabilities.... because it didn't back in 2001 when I was learning tons about Linux.  But it does now.  Linux on the server has really, really improved over the years.   (I've been using Linux since 1993.)

Tuesday, August 17, 2010

*-Overflow

I spent some time obsessively browsing http://mathoverflow.net during the last few days (and posting too), and it is a really awesome site.  I found out about it last December, when I visited Berkeley to give a talk, and had lunch with one of the guys that runs the site, but didn't pay much attention to it until recently.  It's much more suitable for discussion of research-level mathematics than general use and development of Sage.

Mathoverflow is interesting in that their comment system supports jsmath with realtime preview, which is something the TinyMCE editor in the Sage notebook doesn't do. It's also something that http://stackoverflow.com doesn't do, as far as I can tell. The  grad students at Berkeley that organize mathoverflow probably somehow hacked jsmath into the comment system.

I was very surprised to learn that Mathoverflow and Stackoverflow (and all the dozens and dozens of stackoverflow-based sites) are closed source. There is a company that runs stackoverflow and hosts related sites, and keeping their code closed is part of their business model.

Some people sort of forked something related to stackexchange at some point, and created this: CNPROG, which is fortunately based on Python.  There are about 20 sites using CNPROG now, including one for all questions about the use of Python in science, mathematics, and engineering.
I tried this site, answering a question about Cython, and it is snappy and clean.

Perhaps I will add jsmath or mathjax support to cnprog and start another site (http://q.sagemath.org ?), say on using math software as an aid to mathematical research. The site would not be specific to Sage or restricted to open source. The FAQ would say that all questions must be of the form: "(How) can I use a computer to compute XYZ, where XYZ should be some sort of advanced mathematical question." The emphasis on "advanced" and "research" is that there are already other sites about how to do elementary stuff (=people's homework), and it will provide an excuse to keep that out, which will greatly raise the quality.   It is, after all, exactly this uncompromising focus on research that makes http://mathoverflow.net so interesting.

EDIT: I've now created http://ask.sagemath.org which is based on http://askbot.org, which is in turn based on CNPROG. 

Monday, June 7, 2010

PARI on the iPAD (a component of Sage)

I got PARI (http://pari.math.u-bordeaux.fr/), which is a very powerful C program for doing number theory, to run natively on my iPad and iPhone!





"All" I had to do was:

1. Jailbreak my iPad using Spirit.
2. Make a gcc wrapper script on my MAC laptop that can cross-compiler binaries for iPhoneOS, and put this script first in my path (I called it gcc):

#!/bin/sh
# the following is all on one line:
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc -arch armv6 -mthumb -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.2.sdk/  "$@"

3. Download the PARI source code and extract it:  pari-2.3.5.tar.gz
4. Configure and build PARI (this requires that you have the latex XCode installed).
5. The build fails, so I edited the file src/kernel/none/mp_indep.c in a somewhat random way to get it to compile.  More precisely, I commented out lines 794-801, and replaced them by:

#  define INDEX0 1
#  define INDEX1 0


6. Copy Odarwin-i386/gp-sta over to the iPad/iPhone using ssh (I installed an ssh daemon using Cydia). 
7. Run it, via iSSH (purchased from the App store) connected to localhost -- see above. 


Here is a link to my gp-sta, in case you want to just drop it on your phone and run it.  

If I can put a simple GUI on this, I may try to submit this to the App store, so that anybody can trivially install GP for free.   Another possibility would be to get it included in Cydia. 

This is of course just a tiny step on the way to porting Sage to the iPad.




Addendum


I was also able to build Pari *directly* on the iPhone with readline support. Installing GCC was pretty crazy.

William-A-Steins-iPad:~/pari/pari-2.3.5 mobile$ Odarwin-arm/gp-sta 
                                 GP/PARI CALCULATOR Version 2.3.5 (released)
                            arm running darwin (portable C kernel) 32-bit version
                      compiled: Jun  7 2010, gcc-4.2.1 (Based on Apple Inc. build 5555)
                             (readline v6.0 enabled, extended help not available)

                                    Copyright (C) 2000-2006 The PARI Group

PARI/GP is free software, covered by the GNU General Public License, and comes WITHOUT ANY WARRANTY 
WHATSOEVER.

Type ? for help, \q to quit.
Type ?12 for how to get moral (and possibly technical) support.

parisize = 4000000, primelimit = 500000
? 2+3
%1 = 5
? 

My notes for installing GCC on the iphone:

1) Install this

  http://files.getdropbox.com/u/876743/fake-libgcc_1.0_iphoneos-arm.deb

dpkg -i fake-libgcc_1.0_iphoneos-arm.deb

2) apt-get install iphone-gcc


3) Install headers:

Copy these over, which I got from http://www.megaupload.com/?d=55ZNOCKI, and extra to /usr/include

   sdk-2.0-headers.tar.gz

I literally just put them in /usr/include/   

4) Missing

  /usr/lib/libgcc_s.1.dylib

...

wget http://apt.saurik.com/debs/libgcc_4.2-20080410-1-6_iphoneos-arm.deb


5) Hmm:

  http://blog.aaronash.com/?p=15

I downloaded this and extracted it as lib, and had to use some funny password.   
"aksblog.co.nr"
Then I copied lib/libSystem.dylib to /usr/lib/ on my iPad

... and now gcc seems to work! 

6) This isn't needed for some reason: Make it so I can sign my apps:

    apt-get install ldid  

ldid -S main

Maybe I don't need this only because I'm not making a gui yet. 


7) Compiling runs into fork limits a lot, so raise the limit:

William-A-Steins-iPad:~/pari/pari-2.3.5 mobile$ sysctl -w kern.maxprocperuid=64
kern.maxprocperuid: 26
sysctl: kern.maxprocperuid: Operation not permitted
William-A-Steins-iPad:~/pari/pari-2.3.5 mobile$ sudo sysctl -w kern.maxprocperuid=64
Password:
kern.maxprocperuid: 26 -> 64

Friday, May 7, 2010

A Python on iPad benchmark

Now that my iPad is really mine, i.e., jailbroken, I installed Python on it and tried a large integer arithmetic benchmark. I'm curious because this will indicate something about the performance of fully porting Sage to the iPad (though of course Sage would use MPIR for large integers...).

First, on my 64-bit 2.6Ghz Intel Dunnington box (sage.math.washington.edu):
>>> import resource
>>> def cputime(s=0): return sum(resource.getrusage(resource.RUSAGE_SELF)[:2])-s
... 
>>> t=cputime()
>>> a=3**(10**6)
>>> cputime()-t
0.53000000000000003
>>> b=a*(a+1)
>>> cputime()-t
2.46


Next on my laptop, which is a top-end macbook air (64-bit intel core2 duo running 64-bit python 2.6.x):
>>> t=cputime()
>>> a=3**(10**6)
>>> cputime()-t
0.64609700000000003
>>> b=a*(a+1)
>>> cputime()-t
3.1051849999999996

And, on the iPad

>>> t=cputime()
>>> a=3**(10**6)
>>> cputime()-t
2.3500000000000014
>>> b=a*(a+1)
>>> cputime()-t
9.5899999999999963

Not bad!! Note that 32-bit versus 64-bit (and Python 2.5 versus 2.6) may be relevant, depending on how Python big integer arithmetic is implemented.

As a bonus, I have an older iPhone 3G (not 3Gs), where we get:

>>> t=cputime()
>>> a=3**(10**6)
>>> cputime()-t
7.6699999999999999
>>> b=a*(a+1)
>>> cputime()-t

Wow, so the iPhone 3G at this benchmark is about TEN TIMES slower than the iPad. No wonder the iPad feels snappier.


Note that I view genuinely porting Sage to the iPad/iPhone as something that will only ever be available to people who jailbreak their devices. This is because Apple bans running any interpreter except Javascript on the iPhone. (Of course, I can always hope that Apple will get sued into opening up their app store...) For a person like me, a jailbroken iPad is dramatically superior to the standard iPad -- full multitasking, ssh access, Python, being able to easily change screen brightness, having full access to the filesystem, etc. Now if only I had Sage! (Of course, Sage usable on the iPad via the web, but sometimes I don't have reliable network access.)

Monday, December 7, 2009

Mathematical Software and Me: A Very Personal Recollection

[You can also read a pdf version of this blog post.]

I find it difficult for me to write a history of Sage without writing
a history of my personal involvement with mathematical software. I
loved using calculators since my earliest memories. My father somehow
got a mechanical electric adding machine for me when I was quite
young, back in the 1970s, and I spent a great deal of time filling
ribbons of paper with it. Then he got me an old TI scientific
calculator from a yard sale, with a LED readout. At age 11, when I
moved from Oregon to Texas, I bought a sliderule, which was pretty
exciting for a while. Then in junior high I finally got a real
scientific calculator, a little Casio, whose instruction manual I
devoured. This was my first inroduction to trigonometry, statistics,
and many other bits of computational mathematics. I was prepared
though, since I had done elementary school on an unusual
work-at-your-pace alternative curriculum, and had already worked
through 10th grade level mathematics.

The first mathematical computer software I ever used was Mathematica,
back in 1992 on a Windows 3.1 PC, when I was a 17-year old
undergraduate at Northern Arizona University in Flagstaff, Arizona.

Naturally, my copy of Mathematica was pirated, since like many
students I was extremely poor at the time. The only thing I found it
useful for at the time was drawing 3d plots (just for fun), and even
then it was frustrating, since there was no way to interactively
change the viewpoint. I also obtained a copy of MATHCAD somehow,
which I found much more useful than Mathematica. This is perhaps not
surprising, because at the time I was a computer engineering
undergraduate, so taking courses in Physics, Electrical Engineering,
Programming, etc.. I was definitely not a mathematics major: I
remember once finishing a multivariable calculus class and thinking
``this is the last mathematics class I'll ever take"! I also used
Maple for about an hour or two in a computer lab for a mathematics
course I took, but found it very cumbersome; this was a token 1-hour
introduction to math software that professors gave their students,
perhaps to justify the grant that paid for the computer lab. At the
time, I viewed Maple, Mathematica, and MATHCAD as software that didn't
really go beyond scientific calculators in any exciting way.

My next encounter with mathematics software was in early 1994 when I
became a mathematics major, after accidentally encountering an
abstract algebra book misfiled under computer science in a used
bookstore, and being instantly mesmerized by ideas such as groups,
rings, and fields. I was in another small computer lab and stumbled
on printouts of the documentation (Northern Arizona University
professor) Mike Falk had for Cayley, which was the predecessor to
Magma. I was amazed and excited, since the capabilities and ideas in
Cayley went so far beyond anything I had thought was even possible in
mathematical software before. I'm pretty sure I never got to actually
use Cayley though, since like now, the software was very expensive
and hard to get. Instead, I just spent a lot of time reading the
reference manual full of its beautiful examples of computing with
groups, rings, and fields, which were the very objects that had
enticed me into mathematics in the first place.

In fact, after that brief encounter with Cayley, I didn't touch
mathematics software again, or even do any nontrivial computer
programming for 3 years. This was because in 1994 I became intensely
interested in theoretical mathematics, and spent most of my time
during the next 3 years systematically doing exercises in mathematics
books, ranging from basic books on linear algebra and combinatorics
(which was big in Arizona) to Hartshorne's Algebraic Geometry.

In 1997, I was a graduate student at Berkeley, and didn't want to
teach so much, so I landed a job (funded by the NSF VIGRE program) in
the department for one year doing programming of curriculum materials
for an undergraduate linear algebra course, along with fellow graduate
student Tom Insel. Though I had been using Linux for years, I had
never thought about free software, or that actual people could
contribute to it. I thought of Linux as ``Unix that I can install on
my own computer", and back then one still paid for Linux by buying a
CD, since downloading over a modem was way too slow. Tom and I spent
a lot of time working on our project together, and he told me how he
had written some software included in Slackware (a Linux
distribution), so got free copies of the CD when new versions came
out. Hey, anybody can contribute to Linux!

I also remember Tom complaining frequently about how we were forced to
program in MATLAB for our project, and he mentioned many other
alternatives that would have been better for what we were doing,
including Java. We were doing GUI programming, with a tiny, tiny bit
of actual mathematics thrown in, and MATLAB's handle-based system for
writing graphical users interfaces was really painful. I had a lot of
experience with C/C++/Windows 3.1 GUI programming from my computer
science undergraduate days, and agreed that MATLAB was pretty awkward
for what we were doing at the time. In retrospect, what we did was
probably pointless, and perhaps never got used. However, the
experience was extremely valuable for both of us, and I'm glad NSF
funded it.

In the meantime, my Ph.D. thesis was going nowhere, despite nearly 3
years of graduate school. One day, I heard about a problem Ken Ribet
was asking all the graduate students: ``Is there a prime number p such
that the Hecke algebra at level p is ramified at p?" It's the first
research problem I can ever remember hearing that was almost certainly
impossible to solve without using a computer. Fellow grad students
Janos Csirik (now at D.E. Shaw), Matt Baker (now at Georgia Tech), and
I searched and found one paper by Hijikata (?), I think from the mid
1970s, which gave an algorithm that might allow one to answer the
above question for specific p's. But to implement the algorithm, it
was necessary to compute class numbers of a huge number of quadratic
fields, and none of the mathematics software I had ever heard of until
then could do this. Janos and Matt mentioned PARI, and I installed it
on my computer. And indeed, it could quickly and easily compute class
groups! PARI was also the first free mathematical software I
encountered.

So I started coding up the algorithm (the Eichler-Selberg trace
formula) in PARI. I had a lot of experience with C++, which is a real
programming language with user defined data types, exception handling,
templates, etc. I rememember in 1992 carefully reading several C++
book cover-to-cover, and I wrote a large amount of code (video games!)
in C++ long, long ago. In contrast, PARI was an immediate shock.
This was a language with no local variables, no real scoping, only a
couple of builtin types, and for a while I thought (incorrectly) that
entire function definitions always had to be on one line, since that
was the case in example code I found. But the algebraic
number theory algorithms implemented in the internal library were
amazing, deep, and very fast. So I implemented the trace formula, and
ran it to try to answer Ribet's question. The program did not
work---basic consistency checks failed. It turned out that there was
a major bug in the algorithm for computing class groups. In
particular, the function qfbclassno, silently returned wrong
answers.

You would think that qfbclassno would be fixed by now. But no. It's
only frickin' 12 years later!! I just checked right now, and the
documentation for PARI still says ``Important warning. For $D < 0$,
this function may give incorrect results when the class group has a
low exponent (has many cyclic factors), because implementing Shanks's
method in full generality slows it down immensely.'' This is buried
in the documentation. The only change is that I think in 1997 the
documentation said that the authors were ``too lazy'' to implement the
full algorithm. Note: In the 1990s the function was classno instead at that
time. I found in tutorial.tex from
pari-1.39a.tar.gz:

Type {\tt classno($-$10007)}. GP tells us that the result is
77. However, you may have noticed in the explanation above that
the result is only usually correct. This is because the
implementers of the algorithm have been lazy and have not put the
complete Shanks algorithm in PARI, causing it to fail in certain
very rare cases. In practice, it is almost always correct, and the
much more powerful {\tt buchimag} program, which {\it is}
complete, can give confirmation.


So I worked around that problem, and was able to run the algorithm for
all primes up to about 300, but didn't find any primes as in Ribet's
question. A few weeks later, at the Arizona Winter School in March
1998 in Tucson, Arizona
(AWS), I mentioned
this to Joe Wetherell (who was another Berkeley grad student), while
we were walking to lunch, and he told me he had written a program---also
in PARI---for computing with modular symbols (following John Cremona's
book), with which I might be able to push the computation a little
further. He gave me a copy later, and I started playing around with
it, and computed the discriminants of all of the Hecke algebras of
prime level up to about 500. NOTE: That program lives on here, in case
you're interested: tables.
And it still works (here with GP 2.4.3)!!!

? M37 = modsym(37,+1);\\
1. Generating M-symbols (0 ms)\\
2. Hashing M-symbols (2 ms)\\
3. Quotienting out by relations (3 ms)\\
4. Computing the kernel of delta (0 ms)\\
Total time....................... (5 ms)\\
? factor(charpoly(T(2,M37)))\\
\%27 =\\
%[x 1]\\
%[x + 2 1]\\


Again, I didn't find any examples, and I wrote to Ken Ribet to tell
him. Then I hopped on a plane and flew to Cambridge, England, to
visit Kevin Buzzard.

Once I got settled in Cambridge, I rechecked my calculations for some
reason... and found an example for the prime $p=389$! Somehow, I had
just missed the example in my previous check. I was extremely excited
as I wrote to Ken Ribet, with my first ever genuine contribution to research
mathematics, which appears in a big paper Ken published on the
Manin-Mumford conjecture---I had shown that his new theorem definitely
did not prove that conjecture for all modular curves. I was also hooked
on computing modular forms, and started making tables. It was also
the height of the mad cow disease scare in England, so I became a
vegetarian.

I tried to push the PARI program that Joe Wetherell had given me to
make bigger tables, but it very quickly ran out of steam. The main
algorithms that the modular symbols algorithm relies on are all manner
of linear algebra over the rational numbers, including computation of
characteristic polynomials, and kernels of sparse and dense matrices,
and also factorization of polynomials over the integers. Despite its
first rate algebraic number theory capabilities, PARI was (and still
is) terrible at linear algebra with really big matrices over the rational
numbers. Also, I found the PARI programming language unbearably
naive.

So I spent the summer of 1998 writing a much more general C++ program
for computing with modular forms called HECKE. Here it is:
hecke. If you grab the file
hecke-july99.gz from that web page, and drop it on just about any Linux
box, it should just work: NOTE
Here is Hecke which I just tried on a Core 2:

% ./hecke-july99
HECKE Version 0.4, Copyright (C) 1999 William A. Stein
HECKE comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; read the included COPYING file for details.


HECKE: Modular Forms Calculator Version 0.4 (July 9, 1999)

William Stein
Send bug reports and suggestions to was@math.berkeley.edu.
Type ? for help.


Anyway, I spent all my time for many months
writing HECKE. This program built on several other C++ math
libraries, including LiDIA and NTL. It doesn't use PARI, because
using PARI from C is... really weird, and LiDIA/NTL had equivalent
functionality at the time. Much of the time I spent on HECKE involved
(1) designing algorithms, generalizing work in Cremona's book, etc.,
and (2) implementing and optimizing algorithms for linear algebra over
the rational numbers. For (2), Kevin Buzzard hooked me up with the
same code Cremona used, which Cremona had got from some South American
student at Cambridge named Luis (?). I then spent a large amount of
time optimizing that code for my computations. The actual linear
algebra algorithms in that code were very naive compared to the
algorithms in Sage now, but they were better than anything available
in any other software I was aware of, or in research papers at the
time.

I was able to compute many fairly sophisticated tables using HECKE,
and it soon became the canonical software for computing with modular
forms, since it was the only software generally available for such
computations. This is perhaps similar to how NAUTY was for a very
long time the canonical software for computing graph automorphism
groups. Naturally, I made HECKE freely available on my webpage. I
remember once getting an email from Ken Ono, who has probably written
over 100 papers on modular forms, many inspired by concrete examples.
He had run across HECKE on my web page, installed it, and was totally
blown away by the capabilities of HECKE, and how useful it would be
for his research. A whole new world had opened up to him, and he told
me he was promptly ordering a new fast computer specifically to run
HECKE on. I was happy to help. Also, my thesis work was starting to
go well, because the computations I was doing was suggesting
interesting new (and do-able) mathematics, left and right.

Then, in 1999, David Kohel---who had been a Berkeley grad student with
me until December 1996---was visiting Berkeley from Sydney, Australia,
and told me about implementing algorithms related to his thesis in
Magma. I think this was the first I had ever heard of Magma, despite
Magma having been around for several years. Magma was expensive and
nearly impossible to get unless you knew the right person, since it
was sold via informal channels. I think there was an install on the
computers in Berkeley, but those computers were ancient vintage
1990-ish Sun workstations, so nobody would seriously try to use them.
Anyway, David had implemented code for computing with rational
quaternion algebras, and this was the only implementation of that
algorithm in the world. Coincidentally, I had extended an old idea of
Ribet to come up with a new algorithm for computing Tamagawa numbers
of modular abelian varieties (see ants and
compgrp). I really, really wanted to
implement my algorithm, because it would allow me to compute all of
the invariants in the Birch and Swinnerton-Dyer conjecture (except
Sha) for most rank 0 modular abelian varieties, which would be a huge
step forward. But my algorithm fundamentally relied on exactly the
computations in rational quaternion algebras that David Kohel had
implemented in Magma. And that was no small undertaking---it's a
complicated algorithm, it takes somebody familiar with the relevant
theory months to implement and optimize, and it builds on many other
basic capabilities. I had a thesis to finish. David---who was then
officially a Magma developer, was able to give me a copy of Magma for
my own computer, which had his code in it. Combining all this with
HECKE, and copying and pasting, I was the first person ever to systematically
compute Tamagawa numbers of general modular abelian varieties (at primes
of multiplicative reduction).

So in 1999 David Kohel put me in a situation where I was fundamentally
dependent on a closed source non-free program in order to continue my
own research. Ironically, during the same afternoon in my apartment
in Berkeley, he mentioned the GNU Public License (GPL), and suggested
I released HECKE under the GPL. Before that moment, I had never even
heard of the GPL. I did as he suggested, but had no idea what it
meant really. That was perhaps fortunate, since the dependencies of
HECKE are NTL and LIDIA, and the NTL license is GPL, but the LIDIA
license is GPL incompatible, so technically I guess HECKE can't be
distributed. Incidentally, LIDIA is still licensed under a
GPL-incompatible license, despite many emails I've received suggesting
the license would change to GPL---licenses don't change easily, due
to having to get agreement from all copyright owners.

I obviously had to actually use Magma at some level in order to do
these computations. Despite having a lot of experience with
programming, I initially found the Magma language and system extremely
hard to learn or do anything with. It was certainly much harder to
use initially than PARI. On the one hand, Magma is a massive system, with
thousands of commands and thousands of pages of reference manual documenation, but
on the other hand there is very little in the way of ``introspection",
i.e., given an object A, it is hard to get context sensitive help
about A. However, one thing was clear: Magma was dramatically better
at dense linear algebra over the rational numbers than HECKE. It was
a whole different world. We're talking jaw dropping speed. In fact,
Magma in the late 1990s on an old computer, was far faster at large
linear algebra over the rationals than Maple or Mathematica is today
on the latest hardware. I would soon find out the reason.

Allan Steel is an enthusiastic Australian, who was an undergraduate at
University of Sydney in the early 1990s and fell in love with the
Magma project. I guess David Kohel told John Cannon about me in 1999,
and Allan happen to be visiting Berkeley for a conference, so Allan
and I met. We ended up talking a huge amount over 3 days. Allan
answered my every question about the language and the system---usually
with an answer about how he had implemented it that way for a
certain reason. So within a few days I knew Magma well enough to be
quite productive in it. Also, Allan gave me some hints about why
linear algebra was so fast: together with polynomial and integer
arithmetic, asymptotically fast linear algebra was one of his main
interests. He explained an exciting array of algorithms, many of
which he had developed or---more importantly---made practical.
There were dozens of tricks that I had never heard of, such as
rational reconstruction, multimodular algorithms, p-adic algorithms,
etc., which were far beyond what I had done with HECKE, or seen in any
other software. And they meant that it would be possible to push my
modular forms computations much farther. All I had to do was rewrite
HECKE in Magma.

It was my last year of graduate school at Berkeley, and I probably
should have been writing my thesis, but instead John Cannon flew me
down to Sydney, Australia, to work with the Magma group for a month
and do a complete new implementation of all the algorithms in HECKE on
top of Magma. I shared an office with Claus Fieker, a German who has
implemented a large amount of the algebraic number theory in Magma,
among other things. As I started doing this, I had some serious
concerns, including: Magma did not allow users to define their own
types (or classes), there is no exception handling, there is no ``eval
statement", no way for users to write compiled code, Magma is closed
source, and Magma is not free. I raised all of these concerns with
Cannon and others, and was assured at the time that they would all be
addressed really soon, except the free part. Regarding free, they
said that I could give copies of Magma to whoever I wanted, so long as
I checked with John Cannon first.

I don't remember why exactly, but I remember once during that month in
1999 going on a walk through the park near the U Sydney campus near a
pond of ducks, sitting on a bench, and realizing that I was making a
huge sacrifice of my freedom as a researcher by going down this path.
Magma was not open source---John Cannon had absolute control over the
system. Magma was not free. And as a language, Magma was
significantly behind C++. It didn't even have a sensible notion of
scope, and one added new data types by entering entries in big tables,
then recompiling the kernel. I asked Cannon why it was so far behind,
and he explained that the grants he was able to secure simply wouldn't
pay for language design. The people who supported Magma with funding
(mainly granting agencies) would only support implementing and
optimizing mathematical algorithms, and the license fees only paid
enough to support ``maintenance", which mainly meant the person who
collected the license fees and distributed Magma via ftp. Ten years
later the Magma language has hardly improved at all. They finally
have exception handling, but still no user defined types, etc., etc.
The library of functionality implemented in Magma is huge though; the
issues with the language didn't stop people from implementing many
exciting algorithms, which have supported huge amounts of research in
number theory and other areas.

I sat down on that park bench, and realized what a dangerous path I
was taking in giving up so much freedom so early in my career. I
resolved at that moment not to do it. At that moment I started
designing what would eventually become Sage. I started thinking about
the language I would implement (I had taken a course in writing
interpreters when I was a student), about implementing all the linear
algebra algorithms Allan had hinted at (but given no details), etc.
I then realized that if I did this, I would have to do it by myself,
since almost everybody I knew used Magma, and would consider my plan
too difficult and pointless. I wouldn't get to do number theory for
years. My spirit broke. And Cannon told me that many of my issues
with Magma would be addressed within a year.

I spent the next 5 years writing and using Magma. I gave dozens and
dozens of talks all over, and convinced anybody who wanted to do
computations with modular forms that Magma was the way to go. I gave
away free copies of Magma (with John Cannon's official blessing),
taught undergraduate courses using Magma, and was generally very
productive. Also in 2003, I had a real job and money, so I started
forming this philosophy of software, where I would judge what software
I used purely based on capability and functionality, and not on price
or openness. I started using Microsoft Windows fulltime, since it
best supported my PDA and had the widest range of software. I bought
some $500 (with educational discount) suite of Adobe software for
video editing, photos, vector graphics, etc. And of course I used
Magma. I was a well paid academic at a well-endowed university
(Harvard), and wanted the best that money could buy.

In 2002, William Randolph Hearst III also donated money to Harvard to
buy me a cluster computers, and by 2003, I wanted to easily script
running lots of computations in parallel. Since Magma didn't have any
parallel capabilities, I stumbled on some language called ``Python"
(Version 2.3), which looked a lot like Magma, but was designed for
general purpose scripting. I started using it to run many
computations in parallel on that cluster. It was the best tool I
could find for the job.

I also started working much harder on making the number theory data I
was computing with Magma available online, and naturally I turned to
Python. Dimitar Jetchev (a Harvard undergrad) and I wrote Python code
to make the data easily queryable via a web interface, and also wrote
code that made it so one could do computations in Magma (and PARI in
some cases) over the web. One incarnation of this is hosted on the
Magma website: calc

As I learned about Python, a funny thing happened. I had by this
point developed a large list of issues with Magma. For example, the
documentation and examples in the Magma reference manual aren't
automatically tested to ensure they give the claimed output, and they
often get out of sync with the actual code. Python is in many ways
similar to Magma -- the language itself feels somewhat similar, and it
has the same ``batteries included" philosophy. The surprising thing
was that Python had solved the dozens of major problems I had with
Magma! I was excited about this in 2003, and so during my next (and
last) trip to work with the Magma group in Sydney, and started trying
to incorporate these solutions into my new Magma code, and to explain
what I had learned to John and others. Right after I returned from
Sydney, I recall excitedly explaining all of this to David Goldschmidt
at a reception at an AMS meeting.

I was quite surprised when a month or two later, in early 2004, John
Cannon really soundly rejected my ideas and even had one of his
employees rewrite my new modular abelian varieties code to get rid of
them. In retrospect, now that I run a large software development
project, I can understand why he didn't get what I was doing. But I
was really suprised then. Second, I went to a big Magma conference at
IHP in Paris, where Manjul Bharghava (a young professor at Princeton),
me, and many other people gave some series of talks. I recall
listening to Manjul's talk as he described a research problem he was
working on, and during his talk he explained that the whole thrust of
his research was seriously stymmied because Magma is closed source.
He needed to adapt some relatively minor part of the some algorithm in
Magma related to quadratic forms, and simply couldn't due to it not
being in the interpreter level of Magma. This just didn't seem right.

I also had lunch with John Cannon during that workshop, where he
explained some big plans he had to edit a huge sequence of volumes
about the mathematics behind algorithms implemented in Magma. I
suggested that it would be nice if these were freely available, and he
did not think that would be possible. He made good on his idea, and I
actually published a paper in the first such volume:
bsdmagma. I wrote that paper in Windows
using Microsoft Word (and converted to LaTeX using WinEdt, a non-free
LaTeX frontend, only at the very end)!

Finally, at that IHP workshop I learned two other things. First, I
learned that a workshop is an incredibly efficient way to develop
mathematical software, far more efficient than the Magma model of
hiring people for months at a time and flying them to Sydney, and
certainly vastly more efficient than what Maple, Mathematica, and
Matlab do, which costs literally costs hundreds of millions of dollars
per year. Second, I recall overhearing conversations about the Magma
language during tea breaks by some locals who were interested in the
workshop, but had not drank the Magma koolaid, and in these
conversations it became clear that I wasn't the only person that found
the Magma language to be deficient.

I started reading slashdot in early 2004, mainly for the interesting
tech news, and the comments kept mentioning ``open source", which was
honestly something I had paid almost no attention to until then.
Intrigued I decided to look around and see how open source mathematics
software had done since I had abandoned it in 1999. NTL was no longer
being actively developed, the LiDIA project was nearly dead, PARI
hadn't changed much (except to break all my old code), but with some
more algorithms for relative number fields. So in five years, the
situation with the open source number theory software environment had
got worse. I realized that I was probably partly to blame, having
tried to convince every number theorist I knew to use Magma, and often
given them free access to ensure they could. I had helped hook a
generation.

About this time I was also writing an elementary number theory book
(which eventually became this book: ent). I had
planned to have a chapter about number theory using each of
Mathematica, PARI, Magma, and Maple. I had the first three programs,
but didn't have access to Maple. Somebody suggested that being a
faculty member and writing a book should be a good argument for Maple
to send me a free copy, so I contacted them. A person from Maplesoft
called me back, and explained that though I was writing a book with a
chapter on Maple, they could not give me a free copy. However, they
could give me the special academic discount of 500 dollars. I asked
if he could do better, and he called me back the next day and said:
``If you can get 4 of your colleagues to also buy Maple at 250
dollars/each then I can sell you Maple for 250 dollars." I was
offended, so I ``obtained'' a ``trial'' copy, and started writing my
chapter anyways. I started trying all the same things as I had easily
done in PARI and Magma, e.g., checking primality of numbers, etc. I
was totally surprised to find that Maple was terrible, being massively
slower than Pari, Magma or Mathematica for most elementary number
theory computations relevant to my book. (Maplesoft was bought by
some Japanese company a few months ago, by the way.)

I also installed Linux in a virtual machine (under Windows), to see
what all the fuss was about, and found I started using Linux all the
time instead of Windows, because the software was better (even Magma
runs much better under Linux than Windows). I deleted Windows and
installed Linux. I was also starting to definitively realize that my
huge list of problems with Magma would never, ever get resolved, and
was getting increasingly frustrated by these problems because Python
didn't have them.

I started talking a lot with Thomas Barnet-Lamb about a crazy idea to
create a new open source math software system with readable
implementations of algorithms, and nothing hidden in some stupid
proprietary layer. Thomas was then a first year Harvard grad student
who had won some international computer programming competition, so
I figured he would enjoy talking about software. I also talked a lot
with Dylan Thurston about this crazy idea; Dylan had started grad
school at the same time as me at Berkeley, graduated the same time,
and had the same first two jobs as me, was also an Assistant
Professor. Both Thomas and Dylan gave me many ideas for programming
languages to consider, including OCaml (which Thomas liked), Haskell
(which Dylan was a huge fan of), etc. After having used Magma for
years, with its highly optimized algorithms, I desperately needed a
fast language. But I also wanted a language that was easy to read,
and that mathematicians could pick up without too much trouble, since
I wanted people like Manjul to someday use this system and not have
their research cut off. And I knew from experience that unreadable
source code is no better than closed source.

I'm not going to go into negatives of any languages. Though I used
Python a lot, for a long time I didn't consider it seriously at all
for this crazy project, since I tried implementing some basic
arithmetic algorithms in Python and found that they were vastly too
slow to compete with Magma (or C). I had also tried quite hard to use
SWIG to make C++ available in Python, but SWIG is extremely frustring,
and has horrible performance (due to multiple layers of wrapping), at
least compared to what Magma could do.

In October 2004, I was flying back from Europe (the Paris Magma
conference) and started reading the Python/C API reference manual
straight through. I realized that Python is far, far more than just
an interpreter. It is a C library that implements everything you
need, and has a well defined and well documented API. I did some
sample benchmarks on the plane, and found not surprisingly that I
could write code as extensions to Python that was just as fast as
anything one could write for Magma by modifying the Magma kernel,
since under the hood, both were written in C. Also, on the flight, I
realized that because the Python/C interface uses reference counting,
it would be vastly easier to write the C extensions I would need using
some sort of language I would design. I got home and somehow stumbled
onto Pyrex, which was exactly what I was planning to write. I tried
it out, did benchmarks, and realized that I had a winner.

With Pyrex and Python, I could implement algorithms and make them as
fast as anything in Magma, assuming I could figure out the right
algorithm. Moreover, the dozens of issues I had with Magma, many of
which were simply a function of them not having the resources to do
language development, were already solved in Python. And Python would
continue to move forward with no work from me. It was mid-2004 and
because of Python, the overall software ecosystem was much better than
in 1999, despite open source number theory software having not moved
forward much.

I started going to (and sometimes hosting) the Boston Python user
group meetings, which was quite large, and gave me much useful
feedback. And I decided it was time to move past my test and
prototype stage and get to work. My plan, as I had explained it to
Thomas, was to create a complete new system from the ground up using
Python + Pyrex. All the code would have an easy to read Python
implementation that was well documented, in some cases there would be
a much faster Pyrex implementation of the same code, etc. With my
naive plan in hand, I sat down with the main elliptic curves file of
the PARI source code, and started to translate.

I think I made it through one function. Where some might have
doggedly persisted for years with such an approach, I quickly ran out
of patience. In fact, when it comes to software and programming I can
be extremely impatient. I realized that my entire plan was insane,
and would take too long. I had discussions with Thomas, Dylan, and
others, and everybody I knew who was seriously into number theory
computation was using Magma, so I realized that I was going to have to
do this entire project myself. So I realized translating was doomed.
Somehow, even with all my experience, I had massively underestimated
the complexity of the algorithmic edifice that is any serious
mathematical software system.

I read the PARI C API reference, and used Pyrex to write a wrapper so
that I could call some basic PARI functions from Python. I implemented
basic rational and integer types using Pyrex and GMP, and the
performance was reasonable. One day, I was using Matplotlib (a Python
library) to draw some plots for Barry Mazur that involved explicit
computation with the incomplete Gamma function, and was frustrated
because neither PARI nor Magma had an implementation of this special
function at the time. Harvard had a Mathematica site license, so I
had a copy of Mathematica, and I wrote code using the pexpect Python
library to hold open a single Mathematica session and use it to
compute the incomplete Gamma function. Problem solved. This was when
the interfaces between Sage and other mathematics software systems was
born.

In January 2005, I was at the AMS meeting in Atlanta, Georgia, hacking
on my code, and David Joyner walked up to me and asked what I was
doing. Until then, I had not shown my Python/Pyrex math software
project to people. There were a few reasons, including feeling sure
that it was massively too difficult to pull off, that working on
something like this would seriously piss off John Cannon, etc. But
feeling brave, I showed David what I was doing and I was surprised
that he found it interesting. I promised to post a copy online, which
he could download.

David Joyner is the first to admit that it's a good idea to make
software easy for him! So I had to make it easy for him to download
and install my program, which I called Manin at the time (after one of
my favorite mathematicians). My target audience wasn't ``Debian"; it
wasn't ``Python programmers"; it wasn't elite hackers---it was David
Joyner. I had to make this program trivial for him to install, work
with, develop, etc. I thought about how it had literally taken me
huge amounts of time just to build Python, GMP, PARI, etc. all from
source in a directory for development, and realized that there was no
way in hell David would get anywhere on Manin if I told him that his
first step was to figure out how to build all those programs, then get
back to me. So I setup something that would do it all automatically
in a self contained way. He tried it, it ``just worked", and he got
really excited and started writing code for Manin. David is a coding
theorist, and wanted group theory and coding theory functionality in
Manin, but didn't want to write it all himself from scratch, so he
asked in email about making Manin and GAP talk to each other somehow.
I showed him my pexpect code for controlling Mathematica, and he
adapted it to create a GAP interface.

David also works at the US Naval Academy where he evidently teaches a
lot of Calculus and Differential Equations courses. He was having so
much fun with Manin, he asked about adding something to do symbolic
calculus to Manin. This was 2005, and I personally had never used any
symbolic calculus software aside from Mathematica and Maple 12 years
earlier, since I viewed computational symbolic calculus as pretty much
irrelevant for most computational number theory, and the Calculus I
had taught never required a computer since computers weren't allowed
on exams. (I now think no computational technique should be a priori
viewed as irrelevant to research in number theory!) So I asked David
about the available open source options, and he said they were Axiom
and Maxima, neither of which I had ever heard of. I can't remember
how we chose Maxima instead of Axiom, but it was some combination of
Maxima being easier to build, easier to understand, and having about
1000 times as many users. In any case, like with PARI, GMP, and
Python, I added Maxima and GAP to the Manin distribution. I also
changed the name from Manin to SAGE = Software for Algebra and
Geometry Experimentation.

David also convinced me Sage needed commutative algebra. At first, he
talked to people and tried to implement everything from scratch, but
even the resulting arithmetic was dreadfully slow. Groebner basis
would be a nightmare waiting on the horizon. We were both impatient,
so we decided to try to find an open source program already out here,
and just use it. There were two choices: Macaulay 2 and Singular,
which had a lot of overlap in functionality. For what we wanted---basic
commutative algebra---they both did all we needed. Singular
built from source easily in a few minutes on every computer I cared
about. Macaulay 2 was ridiculously hard to build and took a long
time. I think based mostly on that, we chose Singular. Also, it was
encouraging that Singular had a relatively large development team, and
did better in some benchmarks I tried.

At the same time as all this, I was traveling a lot and interviewing
for tons of tenure/tenure track jobs all over the place. I got some
job offers with tenure, and suddenly had the crazy idea that if I
worked fulltime on SAGE for a year two, my career could not be
destroyed. This really encouraged me. My number theory research
slowed a lot, and I spent all my extra time on SAGE for a while.

Remember David Kohel, who six years ago in 1999 first introduced me to
Magma? It turns out that like me he spent years and years writing a
large library of software on top of Magma for number theory and
cryptography research. However, at some point he had a fairly serious
falling out with the Magma group, whose details I will omit. Suffice
to say, like me he was motivated to at least look for other options.
He started building and using Sage, and started doing huge amounts of
work on Sage as well, e.g., introducing morphisms and categories
systematically into Sage, and implementing tons of code related to
elliptic curves, algebraic varieties, etc. David Kohel was a
Biologist as an undergraduate and has an amazing eye for general
structure. He also had many technical issues with Magma, which were
mostly different than mine. For example, he felt that the Magma
developers had made a mistake with the design of morphisms, and he
didn't want Sage to make the same mistakes. And he was right to
worry, since for things I didn't care too much about, I would usually
just copy Magma... or as David would say, ``copy Magma's mistakes".


I moved to San Diego and Joe Wetherell who first introduced me to
modular symbols in 1997 also got involved in Sage development, though
mostly from the conversation point of view. Joe had long ago quit
grad school to start a software company in the early 1990s, then
retired from that and went back to grad school, so he had a fairly
mature perspective on software development, and he knew a huge amount
about number theory and optimized algorithms. So 2005 was a long,
long year in which David Kohel in Australia, David Joyner in Maryland,
and me in San Diego, wrote code.

At the end of 2005, the three of us had written a ton of code,
integrated numerous components togethers, and finally had something.
On December 6, Jaap Spies mentioned Sage on the sci.math.symbolic
newsgroup, in response to which some guy named Richard Fateman
declared Sage a curiosity and made some unencouraging assertions about
the way the world works (regarding users, funding, etc.):

mathforum

I was certain deep down inside that Sage would fail anyways, that what
we wanted to do with Sage was totally impossible, so Fateman's
comments couldn't discourage me further. I just didn't care that Sage
was doomed. I couldn't help pushing further.

John Cannon found out about Sage, maybe as a result of the postings on
sci.math.symbolic, and right before Christmas in 2005 he sent me this
email:

---------------------------------------------------------------
Date: Mon, 19 Dec 2005 16:54:09 -0800
From: "John Cannon"
Subject: Magma calculator
William,

This is to formally advise you that your permission to run a
general-purpose calculator based on Magma ends on Dec 31,
2005. This was originally set up at your request so students
in your courses at Harvard could have easy access to Magma.

Please confirm receipt of this letter.
Wishing you a happy Christmas,
John
---------------------------------------------------------------


This single email seriously scared me. Though I was working on Sage
very hard for nearly a year at this point, I honestly didn't then
expect Sage to really be able to replace Magma for me. Magma was the
commercially funded result of fulltime work over decades (really
starting in 1973 with the first version of Cayley). The amount of
work to get from what I had with Sage in December 2005 to what I had
with Magma in December 2000, was still absolutely momentous. I didn't
even know if it could be done by a single human being. Moreover, as
far as I could tell many of the critical linear algebras algorithms I
needed (to make the difference between a calculation taking a minute
or a year) existed only in the secret kernel of Magma and Allan
Steel's head, and they were going to stay locked there forever as far
as I could tell. For example, in June 2004 (before Sage existed),
Allan and I were together at the ANTS VI conference. I started asking
Allan to explain some of the algorithms, and he would explain things
to a point, but not nearly enough to do an actual implementation. And
he gave me this look, like he knew I was trying to get something out
of him.

Isn't it weird that mathematics can be done that way? In 2004, almost
everybody in the world doing serious computations with elliptic
curves, modular forms, etc., were using Magma. Magma was the industry
standard, Magma had won for the forseable future. David Kohel and I
were a big reason why. And yet what kind of mathematics is it, when
much of my work fundamentally depends on a bunch of secret algorithms?
That's just insane. Moreover, it turns out that these algorithms I'm
alluding to are really beautiful (and they are now standard and in
some cases better than what's in Magma, in my opinion, thanks to great
work of people like Giesbrecht, Perent, Kaltofen, Storjohann,
Saunders, Albrecht, etc.).

Anyway, John Cannon's email above seriously scared me. I wasn't in
any way confident that Sage would ever replace Magma for my work and
teaching, and I had big plans involving interactive mathematical web
pages. These plans were temporarily on hold as I was drawn into
Sage. But there were still there. What John did with that email is
tell me, in no uncertain terms, that if I was going to create those
interactive mathematical web pages, they couldn't depend on
Magma. ``This is to formally advise you that your permission to run a
general-purpose calculator based on Magma ends." I was scared. It
was also the first time I saw just how much power John Cannon had over
my life and over my dreams. That email was sent on a whim. I hadn't
got any official permission to run that Magma calculator for a
specific amount of time (just open ended permission). What John made
crystal clear to me was that he could destroy my entire longterm plans
on a whim. I looked around for other options, and there just weren't
any. Sage had to succeed. But still I was certain that it just
wasn't humanly possible, given that I had to do almost all the work,
with limited funding and time.

At this time I had an NSF grant, and also startup money at UCSD, hence
I could rebudget some of my NSF grant. David Joyner suggested that we
run a ``Sage Days", which I guess was named after the East Coast
Computer Algebra Days (ECCAD). David and I organized the first one,
and David did an amazing job inviting a great cast of speakers,
including Steve Linton (of GAP), Sebastian Pauli (of KANT), etc., and
Joe Buhler who also lives in San Diego made sure we scheduled the
workshop so that a lot of people would show up. We had Sage Days in
early February, and I released Sage version 1.0 during my talk, which
started the workshop. The talks went well, people were extremely
enthusiastic about Sage, the coding sprints were intense: the first
version of Sagetex was written, and the current sophisticated GAP
interface was written then by Steven Linton, Kiran Kedlaya and David
Roe wrote that Macaulay 2 interface, and I had the first spark of
insight about how to create the Sage Notebook, after watching a talk
by Robert Kern about some failed attempt to give IPython a notebook
interface. That was the first time I realized a notebook style
interface had some value. And Gonzalo Tornaria got us to finally
start using revision control for our source (!), which meant way more
people could easily contribute. (I had used revision control before
with Magma, but with Sage I had been just taking snapshots regularly.)

As a direct result of Sage Days 1, development picked up. Then I
moved to University of Washington (Seattle) two months later.
Somehow, during the summer of 2006 I was invited by MSRI to run a
2-week summer workshop on modular forms for about 40 graduate
students. I invited David Kohel and a few other speakers. At this
point, honestly some aspects of Sage sucked. I had written a massive
amount of code, for really wide range of things. Power series,
fraction fields, number fields, modular symbols, linear algebra, etc.
I probably should have just used Magma for the workshop, and indeed at
least one speaker entirely did. But I didn't... and this was a
turning point. Some of the students, such as David Harvey (grad
student at Harvard), Robert Bradshaw (Seattle), Craig Citro, and many
others, became highly interested in fixing the numerous flaws they ran
into with Sage. After the talks, we had huge all night coding sprints
in the dorm lobby. Students constantly asked me questions about how
to do things in Sage, and my answer was usually: ``It's easy. Implement
it and send me a patch!" They made a t-shirt for the conference with
this quote on it.

Next we started planning Sage Days 2 in Seattle in late 2006. This
second Sage Days was also well attended and resulted in major
fundamental development directions. For example, David Harvey led a
charge to redesign the coercion model and David Roe got obsessed with
implementing every model imaginable of the p-adics (this still isn't
really done over 3 years later). Sebastian Pauli gave a talk in which
he explained what anybody who takes an algebraic number theory course
knows (or pays attention to Weil), which is that there is a number
field and function field analogy and that all the algorithms carry
over. Guess what---Magma has a sophisticated implementation of all
the relevant algorithms in the function field case, due to work of
Florian Hess that built on work of the KANT group and others, and PARI
has absolutely nothing for algebraic number theory over functions
fields. Sebastian explained that in fact Magma is the only program in
the world that provides both sides of this analogy, I think hoping
that we would do something about this problem. (Now it is 2009, and
still nothing at all has happened---Magma is the only program in the
world for the function field half of algebraic number theory. Gees.)

We had about 13 talks on the first day of Sage Days 2. At the end of
the day, David Savitt (at student of Richard Taylor, and now a
professor in Arizona) looked at me and declared me insane.

After Sage Days 2, I spent over 2 very, very painful months
implementing David Harvey's proposed coercion model. I learned (or
rather, remembered) how difficult certain types of changes to a large
interrelated library can be. Also, a student from San Diego---Alex
Clemesha---followed me to Seattle, and I paid him fulltime to work on
Sage using my startup money. He implemented 2d graphics for
mathematicians (instead of scientists, which is what matplotlib
provides), and he also helped a lot with the first version of the Sage
Notebook. In fact, he was a big Mathematica user before he started
using Sage, and he really missed the Mathematica Notebook, so he
wanted something similar in Sage. When he used Mathematica, he had a
job programming webpages using webMathematica, so really wanted
something that combined the notebook idea with a webpage. We came up
with various ideas. Then I hired an undergraduate, Tom Boothby, who
had just quit a 6-year career in web programming to go back to
college. Together, the three of us figured out how to write AJAX
applications, and the first version of the notebook was born.

It was a controversial decision at the time to write a webapp
instead of a traditional local GUI application. There were many
reasons we made this choice, but for me it was mainly because (1) I
had written some serious local GUI applications before and knew that
they are not easy to write, not portable, and hard to build from
source, and (2) I had tried out wxMaxima (the local GUI maxima
interface) and was just totally shocked to see how bad it was, due to
having to reinvent the wheel---they would have to implement font
dialogs, tabs, everything from scratch; in contrast, with a web
application much of that comes for free. So my motivation was
entirely to create a desktop application quickly. That it happen to
later make it possible for people to collaborate easily, use a Sage
notebook over the web, etc., is a nice bonus. And, it's clear by now
that web applications (like Facebook, Gmail, etc.) are extremely
popular now, and will only get way more popular in the future.

In 2007, the Sage project started really picking up steam. Bobby
Moretti, another UW undergraduate, got obsessed with making it
possible to actually do symbolic calculus in Sage itself. Until
Bobby's code was added to Sage in mid 2007, absolutely all symbolic
Calculus in Sage had to be done via explicit unnatural calls to
Maxima, and involved embarassing and confusing conversions. Bobby, me
and others spent a lot of work designing Sage's first symbolic
calculus interface, and Bobby wrote a pure Python ``proof of concept"
reference implementation that used Maxima via a pseudotty behind the
scenes for everything. This took him months, and probably had a
negative impact on all other aspects of his life. But he heroically
pulled it off. It went into Sage and changed things
dramatically---suddenly, Sage could actually be used for some
undergraduate courses. This increased interest in Sage dramatically.

A few months later, in November 2007, Sage was nominated for the
Trophees du Libre, and Martin Albrecht presented Sage at the meeting
for finalists. We won first place in the Scientific Software
category. This resulted in a blitz of publicity (e.g., several
slashdot articles, and articles in papers around the world in many
languages), and greatly increased the number of Sage downloads.

Around this time we also have Sage Days 5 at the Clay Mathematics
Institute, and Craig Citro convinces us to switch to a 100% peer
review and 100% doctest policy on all new Sage code. Also, I hire
Michael Abshoff to do release management for one year, which
temporarily frees me up to work more on coding, grant proposals, and
my own research.

There is of course much, much more to this story. But it's too
recent, and sometimes a story shouldn't be told until enough time has
elapsed.