|
In this section the basic forms of assignment of values to identifiers are
described.
Given an identifier x and an expression expression, assign
the value of expression to x.
> 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);
163
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);
5/2
Note that redefining the infix operator will not change the
corresponding mutation assignment operator (in this case +:=).
Assignment of n≥1 values, returned by the expression on the right
hand side. Here the xi are identifiers, and the right hand side
expression must return m≥n values; the first n of these will be
assigned to x1, x2, ..., xn respectively.
Ignore the value(s) returned by the expression on the right hand side.
An expression which yields the value true if the `local' identifier
x 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).
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);
x[expr1,expr2,...,exprn] := 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 expri.
The most important case is that of (nested) sequences.
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 ]
]
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.
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 x1, x2, ..., xn. Naming of generators
usually has two aspects; firstly, the strings
x1, x2, ...xn are used for printing of the generators,
and secondly, to the identifiers
x1, x2, ...xn 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;
x1 := E.1; x2 := E.2; ... xn := E.n;
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.
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[1],
X[2],
X[3],
X[4],
X[5]
]
> &+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
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.
> 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
Relations
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
Relations
a^2 = b^-1 * a * b^3
b^3 = b^-1 * a * b^3
> G eq Parent(w);
true
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.
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 }
(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]
|