Navigation

language

Sage without Notebook

Sage – the interactive Shell

One starts the Sage interpreter using the sage command. This starts a customized version of the IPython shell, and imports many functions and classes, so they are ready to use from the command prompt. Upon starting Sage, one gets output similar to the following:

----------------------------------------------------------------------
| SAGE Version 4.5.2, Release Date: 2010-08-05                       |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------


sage:

To quit sage you press CRTL+D or type  quit or exit.

sage: quit
Exiting SAGE (CPU time 0m0.00s, Wall time 0m0.89s)

“Wall time” is the actually passed time. It differs from the CPU time, which doesn’t take into account the time of subprocesses as GAP or Singular.

sage: quit
Exiting SAGE (CPU time 0m0.00s, Wall time 0m0.89s)

Avoid to kill sage with  kill -9 , as some subprocesses possibly won’t be killed or temporary files in $HOME/.sage/tmp won`t be deleted.

Your Sage Session

The session is the sequence of input and output from when you start Sage until you quit. Sage logs all Sage input, via IPython. In fact, if you’re using the interactive shell (not the notebook interface), then at any point you may type %history (or %hist) to get a listing of all input lines typed so far. You can type ? at the Sage prompt to find out more about IPython, e.g., “IPython offers numbered prompts … with input and output caching. All input is saved and can be retrieved as variables (besides the usual arrow key recall). The following GLOBAL variables always exist (so don’t overwrite them!)”:

_:  last input (interaktive command line and browser interface)
__: penultimate input (only command line)
_oh: list of all inputs (only in command lines)

Here an example:

sage: factor(100)
 _1 = 2^2 * 5^2
sage: kronecker_symbol(3,5)
 _2 = -1
sage: %hist   # only works in the command line, not in the browser.
1: factor(100)
2: kronecker_symbol(3,5)
3: %hist
sage: _oh
 _4 = {1: 2^2 * 5^2, 2: -1}
sage: _i1
 _5 = 'factor(ZZ(100))\n'
sage: eval(_i1)
 _6 = 2^2 * 5^2
sage: %hist
1: factor(100)
2: kronecker_symbol(3,5)
3: %hist
4: _oh
5: _i1
6: eval(_i1)
7: %hist

From now on we will not use line enumeration for the rest of this tutorial.

One can also store a list of entries from a session in a macro for that session.

sage: E = EllipticCurve([1,2,3,4,5])
sage: M = ModularSymbols(37)
sage: %hist
1: E = EllipticCurve([1,2,3,4,5])
2: M = ModularSymbols(37)
3: %hist
sage: %macro em 1-2
Macro `em` created. To execute, type its name (without quotes).
sage: E
Elliptic Curve defined by y^2 + x*y + 3*y = x^3 + 2*x^2 + 4*x + 5 over
Rational Field
sage: E = 5
sage: M = None
sage: em
Executing Macro...
sage: E
Elliptic Curve defined by y^2 + x*y + 3*y = x^3 + 2*x^2 + 4*x + 5 over
Rational Field

While using the interative Shell one can execute every UNIX command by prefixing a !. For example is the output of the following code

sage: !ls
auto  example.sage glossary.tex  t  tmp  tut.log  tut.tex

the entire content of the current directory.

The PATH-Variables contain the „bin“ directory of Sage. So if you enter gpgapsingularmaxima, … , you start the sage version of this commands.

sage: !gp
Reading GPRC: /etc/gprc ...Done.

                           GP/PARI CALCULATOR Version 2.2.11 (alpha)
                  i686 running linux (ix86/GMP-4.1.4 kernel) 32-bit version
...
sage: !singular
                     SINGULAR                             /  Development
 A Computer Algebra System for Polynomial Computations   /   version 3-1-0
                                                       0<
     by: G.-M. Greuel, G. Pfister, H. Schoenemann        \   Mar 2009
FB Mathematik der Universitaet, D-67653 Kaiserslautern    \

 

Logging Input and Output

Logging or saving a session in sage is not the same (as explained in Saving and Loading complete Sessions). For logging Input (output is also a possible option) one uses the command logstart. Enter logstart?for further information related to this command. One can use this command to log all input or output, or to rerun those in a future session (simply by loading the logfile).

was@form:~$ sage
----------------------------------------------------------------------
| SAGE Version 4.5.2, Release Date: 2010-08-05                       |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------

sage: logstart setup
Activating auto-logging. Current session state plus future input saved.
Filename       : setup
Mode           : backup
Output logging : False
Timestamping   : False
State          : active
sage: E = EllipticCurve([1,2,3,4,5]).minimal_model()
sage: F = QQ^3
sage: x,y = QQ['x,y'].gens()
sage: G = E.gens()
sage:
Exiting SAGE (CPU time 0m0.61s, Wall time 0m50.39s).
was@form:~$ sage
----------------------------------------------------------------------
| SAGE Version 4.5.2, Release Date: 2010-08-05                       |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------

sage: load("setup")
Loading log file <setup> one line at a time...
Finished replaying log file <setup>
sage: E
Elliptic Curve defined by y^2 + x*y  = x^3 - x^2 + 4*x + 3 over Rational
Field
sage: x*y
x*y
sage: G
[(2 : 3 : 1)]

If you use Sage in the Linux KDE terminal konsole then you can save your session as follows: after starting Sage in konsole, select “edit” then “save history as…” and type in a name to save the text of your session to your computer. After saving this file, you could then load it into an editor, such as GNU emacs, and print it.

 

 

Paste Ignores Prompts

Suppose you are reading a session of Sage or Python computations and want to copy them into Sage. But there are annoying >>> or sage: prompts to worry about. In fact, you can copy and paste an example, including the prompts if you want, into Sage. In other words, by default the Sage parser strips any leading >>> or sage: prompt before passing it to Python. For example

sage: 2^10
1024
sage: sage: sage: 2^10
1024
sage: >>> 2^10
1024

 

Source: https://doc.sagemath.org/html/de/tutorial/interactive_shell.html#chapter-interactive-shell