Interface Issues

Background jobs

Yes, a Sage job can be run in the background on a UNIX system. The canonical thing to do is type

$ nohup sage < command_file  > output_file &

The advantage of nohup is that Sage will continue running after you log out.

Currently Sage will appear as “sage-ipython” or “python” in the output of the (unix) top command, but in future versions of Sage it will appears as sage.

Referencing Sage

See citing Sage.

Logging your Sage session

Yes you can log your sessions.

(a) You can write the output to a file, by running Sage in the background ( Background jobs ).

(b) Start in a KDE konsole (this only work in linux). Go to Settings \(\rightarrow\) History ... and select unlimited. Start your session. When ready, go to edit \(\rightarrow\) save history as ....

Some interfaces (such as the interface to Singular or that to GAP) allow you to create a log file. For Singular, there is a logfile option (in singular.py). In GAP, use the command LogTo.

LaTeX conversion

Yes, you can output some of your results into LaTeX.

sage: M = MatrixSpace(RealField(),3,3)
sage: A = M([1,2,3, 4,5,6, 7,8,9])
sage: print(latex(A))
\left(\begin{array}{rrr}
    1.00000000000000 & 2.00000000000000 & 3.00000000000000 \\
    4.00000000000000 & 5.00000000000000 & 6.00000000000000 \\
    7.00000000000000 & 8.00000000000000 & 9.00000000000000
    \end{array}\right)
>>> from sage.all import *
>>> M = MatrixSpace(RealField(),Integer(3),Integer(3))
>>> A = M([Integer(1),Integer(2),Integer(3), Integer(4),Integer(5),Integer(6), Integer(7),Integer(8),Integer(9)])
>>> print(latex(A))
\left(\begin{array}{rrr}
    1.00000000000000 & 2.00000000000000 & 3.00000000000000 \\
    4.00000000000000 & 5.00000000000000 & 6.00000000000000 \\
    7.00000000000000 & 8.00000000000000 & 9.00000000000000
    \end{array}\right)
sage: view(A)
>>> from sage.all import *
>>> view(A)

At this point a dvi preview should automatically be called to display in a separate window the LaTeX output produced.

LaTeX previewing for multivariate polynomials and rational functions is also available:

sage: x = PolynomialRing(QQ,3, 'x').gens()
sage: f = x[0] + x[1] - 2*x[1]*x[2]
sage: h = f /(x[1] + x[2])
sage: print(latex(h))
\frac{-2 x_{1} x_{2} + x_{0} + x_{1}}{x_{1} + x_{2}}
>>> from sage.all import *
>>> x = PolynomialRing(QQ,Integer(3), 'x').gens()
>>> f = x[Integer(0)] + x[Integer(1)] - Integer(2)*x[Integer(1)]*x[Integer(2)]
>>> h = f /(x[Integer(1)] + x[Integer(2)])
>>> print(latex(h))
\frac{-2 x_{1} x_{2} + x_{0} + x_{1}}{x_{1} + x_{2}}

Sage and other computer algebra systems

If foo is a Pari, GAP ( without ending semicolon), Singular, Maxima command, resp., enter gp("foo") for Pari, libgap.eval("foo")} singular.eval("foo"), maxima("foo"), resp.. These programs merely send the command string to the external program, execute it, and read the result back into Sage. Therefore, these will not work if the external program is not installed and in your PATH.

Command-line Sage help

If you know only part of the name of a Sage command and want to know where it occurs in Sage, just type sage -grep <string> to find all occurrences of <string> in the Sage source code. For example,

$ sage -grep berlekamp_massey
matrix/all.py:from berlekamp_massey import berlekamp_massey
matrix/berlekamp_massey.py:def berlekamp_massey(a):
matrix/matrix.py:import berlekamp_massey
matrix/matrix.py:            g =
berlekamp_massey.berlekamp_massey(cols[i].list())

Type help(foo) or foo?? for help and foo.[tab] for searching of Sage commands. Type help() for Python commands.

For example

help(Matrix)

returns

Help on cython_function_or_method in module sage.matrix.constructor:

matrix(*args, **kwds)
    matrix(*args, **kwds)
    File: sage/matrix/constructor.pyx (starting at line 21)

        Create a matrix.

        This implements the ``matrix`` constructor::

            sage: matrix([[1,2],[3,4]])
            [1 2]
            [3 4]

        It also contains methods to create special types of matrices, see
        ``matrix.[tab]`` for more options. For example::
--More--

in a new screen. Type q to return to the Sage screen.

Reading and importing files into Sage

A file imported into Sage must end in .py, e.g., foo.py and contain legal Python syntax. For a simple example see Permutation groups with the Rubik’s cube group example above.

Another way to read a file in is to use the load or attach command. Create a file called example.sage (located in the home directory of Sage) with the following content:

print("Hello World")
print(2^3)

Read in and execute example.sage file using the load command.

sage: load("example.sage")
Hello World
8
>>> from sage.all import *
>>> load("example.sage")
Hello World
8

You can also attach a Sage file to a running session:

sage: attach("example.sage")
Hello World
8
>>> from sage.all import *
>>> attach("example.sage")
Hello World
8

Now if you change example.sage and enter one blank line into Sage, then the contents of example.sage will be automatically reloaded into Sage:

sage: !emacs example.sage&     #change 2^3 to 2^4
sage:                          #hit return
***************************************************
                Reloading 'example.sage'
***************************************************
Hello World
16
>>> from sage.all import *
>>> !emacs example.sage&     #change 2^3 to 2^4
>>>                          #hit return
***************************************************
                Reloading 'example.sage'
***************************************************
Hello World
16

Python language program code for Sage commands

Let’s say you want to know what the Python program is for the Sage command to compute the center of a permutation group. Use Sage’s help interface to find the file name:

sage: PermutationGroup.center?
Type:           instancemethod
Base Class:     <class 'instancemethod'>
String Form:    <unbound method PermutationGroup.center>
Namespace:      Interactive
File:           /home/wdj/sage/local/lib/python2.4/site-packages/sage/groups/permgroup.py
Definition:     PermutationGroup.center(self)
>>> from sage.all import *
>>> PermutationGroup.center?
Type:           instancemethod
Base Class:     <class 'instancemethod'>
String Form:    <unbound method PermutationGroup.center>
Namespace:      Interactive
File:           /home/wdj/sage/local/lib/python2.4/site-packages/sage/groups/permgroup.py
Definition:     PermutationGroup.center(self)

Now you know that the command is located in the permgroup.py file and you know the directory to look for that Python module. You can use an editor to read the code itself.

“Special functions” in Sage

Sage has many special functions (see the reference manual at http://doc.sagemath.org/html/en/reference/functions/), and most of them can be manipulated symbolically. Where this is not implemented, it is possible that other symbolic packages have the functionality.

Via Maxima, some symbolic manipulation is allowed:

sage: maxima.eval("f:bessel_y (v, w)")
'bessel_y(v,w)'
sage: maxima.eval("diff(f,w)")
'(bessel_y(v-1,w)-bessel_y(v+1,w))/2'
sage: maxima.eval("diff (jacobi_sn (u, m), u)")
'jacobi_cn(u,m)*jacobi_dn(u,m)'
sage: jsn = lambda x: jacobi("sn",x,1)
sage: P = plot(jsn,0,1, plot_points=20); Q = plot(lambda x:bessel_Y( 1, x), 1/2,1)
sage: show(P)
sage: show(Q)
>>> from sage.all import *
>>> maxima.eval("f:bessel_y (v, w)")
'bessel_y(v,w)'
>>> maxima.eval("diff(f,w)")
'(bessel_y(v-1,w)-bessel_y(v+1,w))/2'
>>> maxima.eval("diff (jacobi_sn (u, m), u)")
'jacobi_cn(u,m)*jacobi_dn(u,m)'
>>> jsn = lambda x: jacobi("sn",x,Integer(1))
>>> P = plot(jsn,Integer(0),Integer(1), plot_points=Integer(20)); Q = plot(lambda x:bessel_Y( Integer(1), x), Integer(1)/Integer(2),Integer(1))
>>> show(P)
>>> show(Q)

In addition to maxima, pari and octave also have special functions (in fact, some of pari’s special functions are wrapped in Sage).

Here’s an example using Sage’s interface (located in sage/interfaces/octave.py) with octave (https://www.gnu.org/software/octave/doc/latest).

sage: octave("atanh(1.1)")   ## optional - octave
(1.52226,1.5708)
>>> from sage.all import *
>>> octave("atanh(1.1)")   ## optional - octave
(1.52226,1.5708)

Here’s an example using Sage’s interface to pari’s special functions.

sage: pari('2+I').besselk(3)
0.0455907718407551 + 0.0289192946582081*I
sage: pari('2').besselk(3)
0.0615104584717420
>>> from sage.all import *
>>> pari('2+I').besselk(Integer(3))
0.0455907718407551 + 0.0289192946582081*I
>>> pari('2').besselk(Integer(3))
0.0615104584717420

What is Sage?

Sage is a framework for number theory, algebra, and geometry computation that is initially being designed for computing with elliptic curves and modular forms. The long-term goal is to make it much more generally useful for algebra, geometry, and number theory. It is open source and freely available under the terms of the GPL. The section titles in the reference manual gives a rough idea of the topics covered in Sage.

History of Sage

Sage was started by William Stein while at Harvard University in the Fall of 2004, with version 0.1 released in January of 2005. That version included Pari, but not GAP or Singular. Version 0.2 was released in March, version 0.3 in April, version 0.4 in July. During this time, support for Cremona’s database, multivariate polynomials and large finite fields was added. Also, more documentation was written. Version 0.5 beta was released in August, version 0.6 beta in September, and version 0.7 later that month. During this time, more support for vector spaces, rings, modular symbols, and windows users was added. As of 0.8, released in October 2005, Sage contained the full distribution of GAP, though some of the GAP databases have to be added separately, and Singular. Adding Singular was not easy, due to the difficulty of compiling Singular from source. Version 0.9 was released in November. This version went through 34 releases! As of version 0.9.34 (definitely by version 0.10.0), Maxima and clisp were included with Sage. Version 0.10.0 was released January 12, 2006. The release of Sage 1.0 was made early February, 2006. As of February 2008, the latest release is 2.10.2.

Many people have contributed significant code and other expertise, such as assistance in compiling on various OS’s. Generally code authors are acknowledged in the AUTHOR section of the Python docstring of their file and the credits section of the Sage website.