Because SAGE is a new system, we begin by explaining the main longterm
goals for SAGE. These have not all been attained, though
authors have done much since work on SAGE started in 2004.
- Efficient: Be fast. This is difficult, since
algorithms are sometimes not published, and finding optimal algorithms
is often difficult (years of work, Ph.D. theses, etc.).
- Free and open source: The source code must be freely
available and readable, so users can understand what the system is
really doing and more easily extend it. Just as mathematicians gain
a deeper understanding of a theorem by carefully reading or at least
skimming the proof, people who do computations should be able to
understand how the calculations work by reading documented source
code.
- Easy to compile: SAGE should be easy to compile from
source for Linux, OS X and Windows users. This provides more
flexibility for users to modify the system.
- Tools: Provide robust interfaces to some of the
functionality of PARI, GAP, GMP, Singular, mwrank, and NTL. These
are all are GPL'd and SAGE provides an interface for using them.
- Well documented: Reference manual with numerous examples.
- Extensible: Be able to define new data types or derive
from builtin types, and make code written in your favorite language
part of the system.
- User friendly: The hope is to eventually attain a high
level of user support. (The GAP Forum email list is an ideal
example of the support it is hoped that SAGE can attain.)
There are pros and cons.
The primary implementation language of SAGE is Python (see [Py]),
though code that must be fast is implemented in a compiled language.
Using Python has several advantages:
- Object persistence is well-supported in Python. There is
extensive support in Python for saving arbitrary objects you have
computed to disk files or a database.
- Excellent support for documentation of functions and packages
in the source code, including automatic extraction of
documentation and automatic testing of all examples. The handbook
examples are tested and guaranteed to work as indicated.
- Memory management: Python now has a well though out and robust
memory manager and garbage collector that correctly deals with
circular references, and allows for local variables in files.
- Python has many packages available now that would be immediately
useful to users of SAGE:
numerical analysis and linear algebra, 2D and 3D graphics,
networking (for distributed computation), database support, etc.
- Portability: Python is easy
to compile from source on most platforms in 2 minutes.
- Exception handling: Python has a sophisticated and well
thought out system of exception handling, whereby programs
gracefully recover even if errors occur in code they call.
- Debugger: Python includes a debugger, so when code fails
for some reason, the user receives an error message, extensive stack
trace, and can inspect the state of all relevant variables and move
up and down the stack.
- Profiler: There is a Python profiler, which runs code
and creates a report detailing how many times and for how long each
function was called.
- A Language: Instead of writing a new language for
mathematics as was done for MAGMA, Maple, Mathematica, MATLAB,
GP/PARI, GAP, Macaulay 2, SIMATH, etc., we use the Python
language, which is a popular and well thought-out computer
language that is being actively developed and optimized by hundreds
of skilled software engineers.
3.2.2 How Some Python Annoyances are Resolved in SAGE
People who do research mathematics and use Python often run into a few problems:
- Notation for exponentation:
**
versus ^
. In Python, ^
means
``xor'', not exponentiation, so in Python we have
>>> 2^8
10
>>> 3^2
1
>>> 3**2
9
This might be easy for some people to get used to, but for a person
used to typing LaTeXor this appears odd; it is also inefficient for
pure math research, since exclusive or is rarely used. For
convenience, SAGE pre-parses all command lines before passing them to
Python, replacing instances of ^
that are not in strings with
**
:
sage: 2^8
256
sage: 3^2
9
sage: "3^2"
3^2
- Integer division:
The expression
sin(2/3)
has much different
behavior in Python than
in any standard math system. In Python, if
and
are integers
then
is also an integer, namely the quotient of
divided
by
. Therefore
. This illustrates how Python is similar
to C in many ways (arrays are also indexed starting at 0). There has
been much talk in the Python community about changing Python so
2/3 returns the floating point number 0.6666..., and
making 2//3 return 0
.
We fix this in the SAGE interpreter, as mentioned elsewhere.
For example:
sage: 2/3
2/3
sage: 2//3
0
sage: sin(1/2)
0.479425538604
sage: sin(1//2)
0.0
sage: 2//3
0
sage: sin(RR(1/2))
0.47942553860420301
sage: RR
Real Field with 53 bits of precision
sage: a = RR(2/3)
sage: a
0.66666666666666663
sage: sin(a)
0.61836980306973699
- Long integers: Python has native support for arbitrary
precision integers, in addition to C-int's. These are slower than
what GMP provides, and have the extremely annoying property that
they print with an L at the end to distinguish them for
C-int's. SAGE also implements arbitrary precision integers, using
the GMP C-library, and these print without an L.
Rather than writing a new language, we use the Python language exactly
as is, and write a pre-parser for IPython so that the command line
behavior of IPython is what a mathematician expects. However, one
must still obey the standard Python rules when writing packages that
will be imported into SAGE.
If you would like to contribute to SAGE, your help will be greatly
appreciated! It can range from code to simply adding a section or two
to the SAGE reference manual. Just email William Stein at
wstein@ucsd.edu
and see the SAGE website, where there is a
long list of SAGE-related projects ordered by priority and category.
If you write a paper using SAGE,
please reference computations done with SAGE by including
[SJ] in your bibliography.
**
If you happen to have just read straight through this tutorial, and
have some sense of how long it took you, please let me know
(email wstein@ucsd.edu
).
Have fun with SAGE!
Release 0.8.2, documentation updated on October 22, 2005.
See About this document... for information on suggesting changes.