Top
Back: Getting started
Forward: Rings and standard bases
FastBack: How to use this manual
FastForward: General concepts
Up: Getting started
Top: Singular Manual
Contents: Table of Contents
Index: Index
About: About this document

2.3.1 First steps

Once SINGULAR is started, it awaits an input after the prompt >. Every statement has to be terminated by ; .

 
37+5;
==> 42

All objects have a type, e.g., integer variables are defined by the word int. An assignment is made using the symbol = .

 
int k = 2;

Test for equality resp. inequality is done using == resp. != (or <>), where 0 represents the boolean value FALSE, and any other value represents TRUE.

 
k == 2;
==> 1
k != 2;
==> 0

The value of an object is displayed by simply typing its name.

 
k;
==> 2

On the other hand, the output is suppressed if an assignment is made.

 
int j;
j = k+1;

The last displayed (!) result can be retrieved via the special symbol _ .

 
2*_;   // the value from k displayed above
==> 4

Text starting with // denotes a comment and is ignored in calculations, as seen in the previous example. Furthermore SINGULAR maintains a history of the previous lines of input, which may be accessed by CTRL-P (previous) and CTRL-N (next) or the arrows on the keyboard.

The whole manual is available online by typing the command help; . Documentation on single topics, e.g., on intmat, which defines a matrix of integers, is obtained by

 
help intmat;

This will display the text of intmat, in the printed manual.

Next, we define a $3 \times 3$ matrix of integers and initialize it with some values, row by row from left to right:

 
intmat m[3][3] = 1,2,3,4,5,6,7,8,9;
m;

A single matrix entry may be selected and changed using square brackets [ and ].

 
m[1,2]=0;
m;
==> 1,0,3,
==> 4,5,6,
==> 7,8,9

To calculate the trace of this matrix, we use a for loop. The curly brackets { and } denote the beginning resp. end of a block. If you define a variable without giving an initial value, as the variable tr in the example below, SINGULAR assigns a default value for the specific type. In this case, the default value for integers is 0. Note that the integer variable j has already been defined above.

 
int tr;
for ( j=1; j <= 3; j++ ) { tr=tr + m[j,j]; }
tr;
==> 15

Variables of type string can also be defined and used without having an active ring. Strings are delimited by " (double quotes). They may be used to comment the output of a computation or to give it a nice format. If a string contains valid SINGULAR commands, it can be executed using the function execute. The result is the same as if the commands would have been written on the command line. This feature is especially useful to define new rings inside procedures.

 
"example for strings:";
==> example for strings:
string s="The element of m ";
s = s + "at position [2,3] is:";  // concatenation of strings by +
s , m[2,3] , ".";
==> The element of m at position [2,3] is: 6 .
s="m[2,1]=0; m;";
execute(s);
==> 1,0,3,
==> 0,5,6,
==> 7,8,9

This example shows that expressions can be separated by , (comma) giving a list of expressions. SINGULAR evaluates each expression in this list and prints all results separated by spaces.


Top Back: Getting started Forward: Rings and standard bases FastBack: How to use this manual FastForward: General concepts Up: Getting started Top: Singular Manual Contents: Table of Contents Index: Index About: About this document
            User manual for Singular version 4-0-3, 2016, generated by texi2html.