Magma Lecture Notes : Session 1.
--------------------------------
Introduction.
-------------
This document is intended as a cheat sheet for basic Magma syntax. You must learn how to use these commands by experiment.
Use the documentation online and reference this sheet otherwise.
For anything not in here, here is the Magma handbook:
http://magma.maths.usyd.edu.au/magma/handbook/
You can also search for commands using ?
(I do not like this as much.)
Example: ?Real
Also here is a pretty good tutorial
http://modular.math.washington.edu/msri06/work/kohel/msri_magma.pdf
Tips for self-debugging:
If a command keeps giving you an error, first check to make sure that you are using the correct typecasting. For example,
if HasPropertyX(G) is not telling you whether or not G has property X, the documentation may say
HasPropertyX(G) : GrpPerm -> BoolElt
So you must make sure that Type(G) returns GrpPerm. If it returns another group type (e.g. GrpPC) it won't work,
and you will need to either find a command to turn G into a GrpPerm or program the test yourself.
If this is not the problem, make sure that you are not having a coercion problem - for example, multiplying two elements from
different groups results in an error, sometimes even if they are subgroups of the same group.
If nothing here works, email me at gruberan@mail.uc.edu and I will try to help you. (Please do not ask me to debug enormous
spaghetti code.)
** Do not wait until the last minute to start programming assignments! **
Even when conceptually easy, there are often syntax issues which take time to fix, especially when you are first learning.
--------------------------------
Interface:
To run programs, write them in a txt file and then type this in the Magma prompt
load "C:/Users/anotherghost/Documents/NameOfFile.txt";
or, if you are lazy like me, you can copy and paste the whole program from notepad into the Magma shell.
Many recommend getting a freeware text editor like Notepad++ or emacs, but personally, I just use notepad.
To output something to a file, use PrintFile( filepath , what you want to output to it );
Example:
PrintFile( "C:/Users/anotherghost/Documents/out.txt" , [Random(1,10):i in [1..10]]);
An easier way is to redirect all of Magma's output into a file by using SetOutputFile and UnsetOutputFile, which
works as follows:
SetOutputFile(filename);
(your code here)
UnsetOutputFile();
To write a comment in a file, put it between these guys /* */
This is good for annotating your programs, especially when sending them to me or a partner.
Example:
/* The following code will tell us whether G has property X */
HasPropertyX(G);
Elements and Collections:
Store a value y to variable x like this: x := y;
y can be whatever you want - a number, a set, a group.
Example: a := 2;
Define a set using these: { }
Define a sequence using these: [ ]
If you would like a set to be indexed, use these {@ @}
If you have a set and you want to index it: SetToIndexedSet
Defining collections:
{ f(x) : x in S | g(x) }
f is what you want. g (which is optional) is a condition.
Ex: { Order(g) : x in G };
{ g : g in G | Order(g) eq 2 };
#S gives you the size of S.
If S is a sequence or an indexed set, use S[i] to refer to the i^th element.
If you have an element x in an indexed set or sequence S and you want to know its position in S, use Index(x);
For sequences: Append(S, x) returns S with x tacked onto the end.
Append(~S, x) is the same as S := Append(S, x)
For sets: Use Include(S, x) or Include(~S,x) instead of the above.
You can also use Exclude(~S,x)
Membership testing:
g in G , g notin G
S subset G , S notsubset G
Note: g notin G is equivalent to not g in G
Control structures:
for i in [1..n] do
(stuff)
end for;
Conditional statements are anything that evaluates to 'true' or 'false', such as the membership testing
commands above. Some more conditional phrases:
eq equal
ne not equal
lt less than
le less than or equal
gt greater than
ge greater than or equal
a and b
a or b
a xor b "a or b, but not both"
while checks a condition and executes (stuff) if it is true. If it is false, it moves on.
Usage:
while condition do
(stuff)
end while;
repeat does executes (stuff), then checks condition. If the condition is true, it moves on, otherwise it starts over.
Usage:
repeat
(stuff)
until condition;
the 'if' command checks if a condition is true, and if so, executes the code between it and end if;
Usage:
if condition then
(stuff)
end if;
You can also use if with 'else', which will execute (stuff 1) if condition returns true, and (stuff 2) otherwise.
Usage:
if condition then
(stuff 1)
else
(stuff 2)
end if;
break; This gets you out of whatever loop you're in.
Example:
for i in [1..10]
i;
if i eq 4 then
break;
end if;
end for;
break i; This gets you out of the loop that's using the iterator i.
Example:
for i in [1..3] do
for j in [1..3] do
for k in [1..3] do
[i,j,k];
if j eq 2 then
break j;
end if;
end for;
end for;
end for;
Permutation Groups:
Some basic permutation groups are CyclicGroup(n), SymmetricGroup(n),
AlternatingGroup(n), DihedralGroup(n)
Getting elements of permutation groups: there are a couple ways of doing this.
1. elt< G | x1,x2,x3,...,xn >
Input is in permutation notation, output in cycle notation.
1 2 3 4
x1 x2 x3 x4
Example: S5 := SymmetricGroup(5);
x := elt< S5 | 2, 1, 4, 5, 3 >;
2. G ! (x1, x2)(x3,x4,x5)
Input is in cycle notation, output is (as always) in cycles.
Example: S5 ! (1,2)(3,4,5);
3. Get identity with Id(G)
List of all subgroups given by Subgroups(G);
Lattice of subgroups given by SubgroupLattice(G);
Getting a subgroup generated by some elements:
sub< G | e1 , e2, e3, ... >;
Example: sub< S5 | (1,2)(3,4,5), (2,3) >;
Constructing a permutation group on n letters with a specified generating set:
PermutationGroup< n | (x1,x2)(x2,x3,x4) , (y1,y2)(y3,y4), etc >;
Order(x) gives the order of an element, Order(G) the order of a group
IsAbelian(G)
IsCyclic(G)
g^n gives the nth power of a group element g
g^-1 gives the inverse of a group element g
Types, Coersion, and Numbers:
Different types of numbers: Integers(), RationalField(), RealField(), ResidueClassRing(n)
To make polynomials, first define the field F the coefficients will be in, then PolynomialRing(F, n)
Example: R := RationalField();
P := PolynomialRing(R,1);
x^2 + x + 1;
(We probably won't use polynomials very often in this class, but they are good for Coercion examples.)
Type(x) shows you the variable type of x. (This is very good when you can't figure out why
something doesn't work.)
Parent(x) tells you where an x lives.
Example: P := PolynomialRing(RationalField(),2);
Parent(x1);
A!b moves an element b to a different parent. You can only do this when
it makes sense to.
Example: Q := RationalField();
P := PolynomialRing(Q,1);
a := Q!1;
b := P!1;
Parent(a);
Parent(b);
Another: Z4 := Group;
Z2** := sub;
Z4!b;
Another: Q := RationalField();
R := RealField(5);
x := 1/2;
Q!x;
R!x;
Another: U7 := ResidueClassRing(7);
U11 := ResidueClassRing(11);
a := Random(1,6);
a;
b := Random(1,6);
b;
U7 !a;
U7 !b;
(U7 !a)*(U7 !b);
Order(U7 !a);
Order(U7 !b);
Order((U7 !a)*(U7 !b));
U11!a;
U11!b;
(U11!a)*(U11!b);
Order(U11!a);
Order(U11!b);
Order((U11!a)*(U11!b));
Matrices: To make a matrix over a field F, use Matrix(F,m,n,[ x1, x2, x3, x4, ... , x m*n ]);
Example: Matrix(R,3,2, [ 1, 2, 3, 4, 5, 6 ]);
Another: Matrix(Q,2,2, [ Random(0, 1) : i in [1..n] ]);
More useful things you may want to try out:
This allows you to make your own command:
function NameOfFunction( inputs )
local a,b,c,etc;
return something;
end function;
the "local" above tells Magma which variables are only intended to be used in the function, so if for example you ran the function
above in a program where you had already defined a varable called "a" to do something different, the "a" in the function would not
affect the "a" in the outside program.
Example: Magma is terrible at statistics - it doesn't even have a Mean command.
If you want one, you're going to have to make it yourself.
function Mean(list)
local total, mean, R;
total := 0;
for i in [1..#list] do
total := total + list[i];
end for;
R := RealField(5);
return R!(total/#list);
end function;
Mean( [ Order(g*h) : g,h in SymmetricGroup(3) ] );
Commands for formatted output:
This is how to manipulate strings, taught by example; run line by line.
x := "some letters";
x *:= " some more letters ";
y := 327 + 42;
Type(y);
x *:= IntegerToString(y);
print x;
M := Matrix(Integers(),4,4,[Random(0,1):i in [1..16]]);
Type(M);
Type(Sprint(M));
x := Sprint(M);
x *:= " <-- check that matrix out";
PrintFile( "filepath/matrix.txt" , x );
**