[Next][Prev] [Right] [Left] [Up] [Index] [Root]


In this section the basic forms of assignment of values to identifiers are described.


Simple Assignment

x := expression;
Given an identifier x and an expression expression, assign the value of expression to x.

Example State_Identifiers (H1E1)

> x := 13;
> y := x^2-2;
> x, y;
13 167
Intrinsic function names are identifiers just like the x and y above. Therefore it is possible to reassign them to your own variable.

> f := PreviousPrime;
> f(y);
In fact, the same can also be done with the infix operators, except that it is necessary to enclose their names in quotes. Thus it is possible to define your own function Plus to be the function taking the arguments of the intrinsic + operator.

> Plus := '+';
> Plus(1/2, 2);
Note that redefining the infix operator will not change the corresponding mutation assignment operator (in this case +:=).
x_1, x_2, ..., x_n := expression;
Assignment of n >= 1 values, returned by the expression on the right hand side. Here the x_i are identifiers, and the right hand side expression must return m >= n values; the first n of these will be assigned to x_1, x_2, ..., x_n respectively.
_ := expression;
Ignore the value(s) returned by the expression on the right hand side.
assigned x : Var -> BoolElt
An expression which yields the value {true} if the `local' identifier x has a has a value currently assigned to it and {false} otherwise. Note that the assigned-expression will return {false} for intrinsic function names, since they are not `local' variables (the identifiers can be assigned to something else, hiding the intrinsic function).

Example State_MultipleReturns (H1E2)

The extended greatest common divisor function Xgcd returns 3 values: the gcd d of the arguments m and n, as well as multipliers x and y such that d=xm + yn. If one is only interested in the gcd of the integers m=12 and n=15, say, one could use:

> d := Xgcd(12, 15);
To obtain the multipliers as well, type

> d, x, y := Xgcd(12, 15);
while the following offers ways to retrieve two of the three return values.

> d, x := Xgcd(12, 15);
> d, _, y := Xgcd(12, 15);
> _, x, y := Xgcd(12, 15);

Indexed Assignment

x[expr_1][expr_2]...[expr_n] := expression;
x[expr_1,expr_2,...,expr_n] := expression;
If the argument on the left hand side allows indexing at least n levels deep, and if this indexing can be used to modify the argument, this offers two equivalent ways of accessing and modifying the entry indicated by the expressions expr_i. The most important case is that of (nested) sequences.

Example State_Indexing (H1E3)

Left hand side indexing can be used (as is explained in more detail in the chapter on sequences) to modify existing entries.

> s := [ [1], [1, 2], [1, 2, 3] ];
> s;
       [ 1 ],
       [ 1, 2 ],
       [ 1, 2, 3 ]
> s[2, 2] := -1;
> s;
       [ 1 ],
       [ 1, -1 ],
       [ 1, 2, 3 ]

Generator Assignment

Because of the importance of naming the generators in the case of finitely presented magmas, special forms of assignment allow names to be assigned at the time the magma itself is assigned.

E<x_1, x_2, ...x_n> := expression;
If the right hand side expression returns a structure that allows naming of `generators', such as finitely generated groups or algebras, polynomial rings, this assigns the first n names to the variables x_1, x_2, ..., x_n. Naming of generators usually has two aspects; firstly, the strings x_1, x_2, ...x_n are used for printing of the generators, and secondly, to the identifiers x_1, x_2, ...x_n are assigned the values of the generators. Thus, except for this side effect regarding printing, the above assignment is equivalent to the n + 1 assignments:

E := expression;
x_1 := E.1; x_2 := E.2; ... x_n := E.n;
E<[x]> := expression;
If the right hand side expression returns a structure S that allows naming of `generators', this assigns the names of S to be those formed by appending the numbers 1, 2, etc. in order enclosed in square brackets to x (considered as a string) and assigns x to the sequence of the names of S.

Example State_GeneratorNamingSequence (H1E4)

We demonstrate the sequence method of generator naming.

> P<[X]> := PolynomialRing(RationalField(), 5);
> P;
Polynomial ring of rank 5 over Rational Field
Lexicographical Order
Variables: X[1], X[2], X[3], X[4], X[5]
> X;
> &+X;  
X[1] + X[2] + X[3] + X[4] + X[5]
> (&+X)^2;
X[1]^2 + 2*X[1]*X[2] + 2*X[1]*X[3] + 2*X[1]*X[4] + 
    2*X[1]*X[5] + X[2]^2 + 2*X[2]*X[3] + 2*X[2]*X[4] + 
    2*X[2]*X[5] + X[3]^2 + 2*X[3]*X[4] + 2*X[3]*X[5] + 
    X[4]^2 + 2*X[4]*X[5] + X[5]^2

AssignNames(~S, [s_1, ... s_n] ) : Struct, [ MonStgElt ] ->
If S is a structure that allows naming of `generators' (see the Index for a complete list), this procedure assigns the names specified by the strings to these generators. The number of generators has to match the length of the sequence. This will result in the creation of a new structure.

Example State_GeneratorNaming (H1E5)

> G<a, b> := Group<a, b | a^2 = b^3 = a^b*b^2>;
> w := a * b;
> w;
a * b
> AssignNames(~G, ["c", "d"]);
> G;
Finitely presented group G on 2 generators
    c^2 = d^-1 * c * d^3
    d^3 = d^-1 * c * d^3
> w;
a * b
> Parent(w);
Finitely presented group on 2 generators
    a^2 = b^-1 * a * b^3
    b^3 = b^-1 * a * b^3
> G eq Parent(w);

Mutation Assignment

x o:= expression;
This is the mutation assignment: the expression is evaluated and the operator o is applied on the result and the current value of x, and assigned to x again. Thus the result is equivalent to (but an optimized version of): x := x o expression;. The operator may be any of the operations join, meet, diff, sdiff, cat, *, +, -, /, ^, div, mod, and, or, xor provided that the operation is legal on its arguments of course.

Example State_MutationAssignment (H1E6)

The following simple program to produce a set consisting of the first 10 powers of 2 involves the use of two different mutation assignments.

> x := 1;
> S := { };
> for i := 1 to 10 do
>    S join:= { x };
>    x *:= 2;
> end for;
> S;
{ 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 }

Deletion of Values

delete x : Var; -> Nil
(Statement.) Delete the current value of the identifier x. The memory occupied is freed, unless other variables still refer to it. If x is the name of an intrinsic Magma function that has been reassigned to, the identifier will after deletion again refer to that intrinsic function. Intrinsic functions cannot be deleted.
 [Next][Prev] [Right] [Left] [Up] [Index] [Root]