Introduction To Magma: Lecture and Hands On Session at UNCG 2016
Introduction To Magma: Lecture and Hands On Session at UNCG 2016
Florian Hess
University of Oldenburg
Specific criteria:
• Universality: Should be appropriate for a broad range of areas
within algebra and geometry.
• Mathematical system:
– Structures and morphisms should be “first class objects”.
– Precise, unambigous and similar formulation of mathematical
content by the language. Emphasis of “mathematical” data
structures over “computer science” data structures.
• Efficiency.
7 Greensboro May, 31st 2016
Universality
General trouble of computer algebra:
• One system or concept for all of mathematics very difficult,
probably impossible or insensible.
• Very specialised systems or concepts may soon need tools from
neighbouring areas.
Examples 1:
• Factorisation of x3 − 2? Define correct polynomial ring, define x,
then ask for factorisation.
• Define number field, ask for class number.
• Supports also various concurrent mathematical contexts.
• Use sets instead of removing double entries by for-loops over lists.
9 Greensboro May, 31st 2016
Efficiency
• Magma is intended as practical, heavy-duty research tool.
• Avoid use of generic data structures and implementation, use
highly specialised data structures and implementation in C and
link with general concept.
• Higher level data structures and algorithms may then be
implemented in Magma language without loss of efficiency.
Examples 2:
• Integers, polynomials, matrices, etc.
• Reduction operator over sequences works in the C to avoid slow
interpreter.
Identifier names must begin with a letter and must be distinct from
reserved words. They are case sensitive.
Conditional expression:
c := bool-expr select a else b; choose a or b depending
on bool-expr.
There is also a case statement and expression.
18 Greensboro May, 31st 2016
Iterative statements
Usual loops
• for i := expr-1 to expr-2 by expr-3 do
statements end for; (the by part may be omitted).
• for i in S do statements end for; where S allows
iteration.
• while bool-expr do statements end while;
• repeat statements until bool-expr;
• Early exit with break; statement in the statements.
• Early exit in for loop over i can be specified by break i; in
case of nested for loops.
Any of the y may be omitted. Those who are assume the default
value in the function body. Any of the z may be in which case the
corresonding result is thrown away.
Example:
• b, c := IsSquare(a); returns a boolean c and a square
root of a if it exists, and otherwise.
21 Greensboro May, 31st 2016
Procedures
General procedure definition:
f := procedure([˜]x1, ..., [˜]xn : y1 := expr1, ...)
local w1, ..., ws;
statements
end procedure;
Example:
f := func< i | i le 0 select 0 else i + $$(i-1) >;
forward f;
f := func< i | i le 0 select 0 else i + f(i-1) >;
24 Greensboro May, 31st 2016
Magma semantics
Within a function or procedure, the type of a variable, if not a
reference variable or an argument, is determined by the first use rule.
• First textual use when it is assigned a value: Variable identifier,
could have been declared by local statement (this is not
necessary).
• First textual use when its value is accessed: Value identifier, must
have been declared outside the function.
• Value computation in assignments comes first (a := a;).
Some operations:
• #, eq, ne, in, notin, subset, notsubset, join,
meet, diff, ...
• &+, &and, &meet, forall, exists, ...
• Random(), Representative(), ChangeUniverse(),
Include(), Exclude(), ...
• Conversion functions between the different types.
Operations:
• cat, &cat, &meet, ...
• Random(), Representative(), Universe(),
ChangeUniverse(), Reverse(), Position(),
Append(), Insert(), Exclude(), Remove(), Sort(),
And(), ...
• Conversion to and from enumerated sets.
Example:
• Fibonacci numbers [ i gt 2 select
Self(i-2)+Self(i-1) else 1 : i in [1..100] ];
Creation of tuples:
• < x1, ..., xn >, Append(), Prune(), Flat(), ...
Access:
• #T, T[i], ...
Creation of cartesian products:
• CartesianProduct(S1, S2), CartesianProduct( [ S1,
..., Sn ] ), CartesianPower(S, n), ...
Operations:
• #, Component(), Representative(), Random(), ...
To define a record, one first needs to define its record format. The
format specifies the names of the components and possibly their
types.
Access:
• Names(RF);
Operations:
• r‘misc; , r‘seq := [ 1, 2 ];
• delete r‘misc;, assigned r‘misc; (boolean).
• Names(), Format(),
• r‘‘s; where s evaluates to a string.
Example:
Creation of maps:
• When doing sub< ... >, quo< ... >.
• Via the graph: map< A->B |[ <x1,y1>,...,<xn,yn> ] >
• Via a rule: map< A->B | x :-> f(x), y :-> g(y) >
y :-> g(y) may be omitted. Also hom< ... > possible.
• Via images on the generators:
hom< A->B | [ y1, ..., yn ] >
For rings: hom< A->B | c, [ y1, ..., yn ] >, c maps
BaseRing(A) to B.
Partial maps (pmap< ... >) by graph or rule are also possible.
36 Greensboro May, 31st 2016
Maps
There is not much checking whether the definition of a map makes
sense. The non generic operations below do not work for all
structures.
Operations:
• Composition f*g (formal), Components().
• Domain(), Codomain(), Kernel(), Image(),
Inverse().
• f(a), a@f, a@@f, HasPreimage(a, f).
• Can be applied (image, preimage) elementwise on sets,
sequences, ...
• Coercion maps corresponding to internal coercion functions.
Into files:
• Write( F, x ); Append x to file with name F.
– Write( F, x : Overwrite := true ); overwrites.
• fprintf file, format, expr-1, ..., expr-n;
Then T is the type of a new structure, E the type of its elements if any.
T is inherited from P1,...,Pn, so ISA(T, Pi) is true.
intrinsic Print(X::T)
{Print X}
// Code: Print X with no new line, via printf
end intrinsic;