[Next] [Prev] [Right] [Left] [Up] [Index] [Root]
Interaction with the Environment

Interaction with the Environment

Some remarks on the environment.

Subsections

Starting and Terminating

If Magma has been installed properly, it can be activated by typing `magma'.

magma -s filename
When starting up Magma, it is possible to specify a start-up file with the -s option. A search for the specified file is conducted, starting in the current directory, and in directories specified by the environment variable MAGMA_PATH after that if necessary. If the file is not found, an empty start-up file is used; if the file has been found it will be loaded before the `prompt' appears.
magma -d
If the -d option is supplied to Magma, the licence for the current magmapassfile is dumped. That is, the expiry date and the valid hostids are displayed. Magma then exits.
magma -S integer
When starting up Magma, it is possible to specify a seed for the generation of pseudo-random numbers. (Pseudo-random quantities are used in several Magma algorithms, and may also be generated explicitly by some intrinsics.) The seed should be in the range 1 to (2 ^ (31) - 2) inclusive. If the integer supplied to the -S option is zero, or if the -S option is not used, Magma selects the seed itself.
quit;
<Ctrl>-D
Terminate the current Magma-session.
<Ctrl>-\
Immediately quit Magma.

Interruption

<Ctrl>-C
Interrupt Magma while it is performing some task (that is, while the user does not have a `prompt'). Magma will try to interrupt at a convenient point (this may take some time).

Input and Output

load "filename";
Input the file with the name specified by the string. The file will be read in, and the text will be treated as Magma input. `Tilde expansion' of file names is allowed.
save "filename";
Copy all information present in the current Magma workspace onto a file specified by the string "filename". The workspace is left intact, so executing this command does not interfere with the current computation.
restore "filename";
Copy a previously stored Magma workspace from the file specified by the string "filename" into central memory. Information present in the current workspace prior to the execution of this command will be lost. The computation can now proceed from the point it was at when the corresponding save-command was executed.
read identifier;
read identifier, prompt;
This statement will cause Magma to assign to the given identifier the string of characters appearing (at run-time) on the following line. This allows the user to provide an input string at run-time. If the optional prompt is given (a string), that is printed first.
readi identifier;
readi identifier, prompt;
This statement will cause Magma to assign to the given identifier the literal integer appearing (at run-time) on the following line. This allows the user to specify integer input at run-time. If the optional prompt is given (a string), that is printed first.
Read(F) : MonStgElt -> MonStgElt
Function that returns the contents of the text-file with name indicated by the string F. Here F may be an expression returning a string.
print expression;
print expression, ..., expression;
print expression : parameters;
Print the value of the expression. Some limited ways of formatting output are described in the Chapter on strings. Four levels of printing (that may in specific cases coincide) exist, and may be indicated after the colon: Default (which is the same as the level obtained if no level is indicated), Minimal, Maximal, and Magma. The last of these produces output representing the value of the identifier as valid Magma-input (when possible).
Write(F, x) : MonStgElt, Var ->
PrintFile(F, x) : MonStgElt, Var ->
    Overwrite: BoolElt                  Default: false
Procedure. Print x to the file specified by the string F. If this file already exists, the output will be appended, unless the optional parameter Overwrite is set to true, in which case the file is overwritten.
PrintFile(F, x, L) : MonStgElt, Var, MonStgElt ->
    Overwrite: BoolElt                  Default: false
Procedure. Print x in format defined by the string L to the file specified by the string F. If this file already exists, the output will be appendedunless the optional parameter Overwrite is set to true, in which case the file is overwritten. The level L can be any of the print levels on the print command above (i.e., it must be one of the strings "Default", "Minimal", "Maximal", or "Magma").
Write(F, x) : MonStgElt, Var ->
PrintFileMagma(F, x) : MonStgElt, Var ->
    Overwrite: BoolElt                  Default: false
Procedure. Print x in Magma format to the file specified by the string F. If this file already exists, the output will be appended, unless the optional parameter Overwrite is set to true, in which case the file is overwritten.
SetLogFile(F) : MonStgElt ->
    Overwrite: BoolElt                  Default: false
Procedure. Set the log file to be the file specified by the string F: all output will be sent to this log file as well as to the terminal. If a log file is already in use, it is closed and F is used instead. By using SetLogFile(F: Overwrite := true) the file F is emptied before output is written onto it.
UnsetLogFile() : ->
Procedure. Stop logging Magma's output.
SetEchoInput(b) : BoolElt ->
Procedure. Set to true or false according to whether or not input from external files should also be sent to standard output.
SetOutputFile(F) : MonStgElt ->
    Overwrite: BoolElt                  Default: false
Procedure. Redirect all Magma output to the file specified by the string F. By using SetOutputFile(F: Overwrite := true) the file F is emptied before output is written onto it.
UnsetOutputFile() : ->
Close the output file, so that output will be directed to standard output again.

Example Lang_Read (H1E1)

In this example we show how Read can be used to import output from a separate C program into your Magma session. We assume that a file mystery.c (of which the contents are shown below) is present in the current directory. We first compile it, from within Magma, and then use it to produce output for the Magma version of our mystery function.

> print Read("mystery.c");
#include <stdio.h>

main(argc, argv) int argc; char **argv; { int n, i;

n = atoi(argv[1]);

for (i = 1; i <= n; i++) printf("%d\n", i * i);

return 0; } > System("cc mystery.c -o mystery"); > mysteryMagma := function(n) > System("./mystery " cat IntegerToString(n) cat " >outfile"); > output := Read("outfile"); > return StringToIntegerSequence(output); > end function; > print mysteryMagma(5); [ 1, 4, 9, 16, 25 ]


Forcing errors

error expression, ..., expression;
Print the values of the expressions and stop execution of Magma. Useful when some incorrect input is given to a function, etc.
error if boolexpr, expression, ..., expression;
If the given boolean expression evaluates to true, print the values of the expressions and stop execution of Magma. Useful when checking that certain conditions must be met, etc.

Timing and Alarm

Cputime() : -> FldReElt
Return the CPU time (as a real number of default precision) used since the beginning of a Magma session.
Cputime(t) : FldReElt -> FldReElt
Return the CPU time (as a real number of default precision) used since time t (as a real number). Time starts at 0.0 at the beginning of a Magma session.
time statement
Executes the statement and prints the time taken.

Example Lang_Time (H1E2)

The time command can be used to time a single statement.

> n := 2^109-1;
> time print Factorization(n);
[<745988807, 1>, <870035986098720987332873, 1>]
Time: 5.300
Alternatively, we can extract the current time t and use Cputime. This method can be used to time the execution of several statements.

> m := 2^111-1;
> n := 2^113-1;
> t := Cputime();
> print Factorization(m);
[<7, 1>, <223, 1>, <321679, 1>, <26295457, 1>, <319020217, 1>, <616318177, 1>]
> print Factorization(n);
[<3391, 1>, <23279, 1>, <65993, 1>, <1868569, 1>, <1066818132868207, 1>]
> print Cputime(t);
11.881

Alarm(s)
Procedure. On UNIX systems, send the signal SIGALRM to the Magma process after s seconds. This is a way of making a Magma-process self-destruct after a certain period.

Environment Variables

We list some environment variables. The following are not Magma statements, but are to be used by the operating system.

MAGMA_STARTUP_FILE
The name of the default start-up file. It can be overridden by the magma -s command.
MAGMA_PATH
Search path for files that are loaded (a colon separated list of directories). It need not include directories for the libraries, just personal directories. This path is searched before the library directories.
MAGMA_MEMORY_LIMIT
Limit on the size of the memory that may be used by a Magma-session (in bytes).
MAGMA_MEMORY_EXTENSION_SIZE
The size of the memory extension allocated by Magma when more space is needed (in bytes).
MAGMA_LIBRARY_ROOT
The root directory for the Magma libraries (by supplying an absolute path name). From within Magma SetLibraryRoot and GetLibraryRoot can be used to change and view the value.
MAGMA_LIBRARIES
Give a list of Magma libraries (as a colon separated list of sub-directories of the library root directory). From within Magma SetLibraries and GetLibraries can be used to change and view the value.
[Next] [Prev] [Right] [Left] [Up] [Index] [Root]