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
.
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://passagemath.org/docs/latest/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