How do you form the Cartesian product of a list with itself?
In some cases, it is better to use ``indexed sets''. To take the
power of
with itself and then select the
element: first create this as a set, then use the
Subsequences( ,2); command, then convert that
to an ``indexed set'' using the SetToIndexedSet command,
as follows.
> Set1:={2^i:i in [1..3]};
> Set1_2:=Subsequences(Set1,2);
> Set1_2_2:=SetToIndexedSet(Set1_2);
> Set1_2_2[3];
[ 2, 8 ]
We could have entered
as an indexed set by
typing
Set1:={@2^i:i in [1..3]@}; but then
MAGMA would've objected to the next line since
Subsequences is not defined for indexed sets.
To add an element to a sequence or set you can use the Include command. For example,
> newset:=Include(Set1_2_2,[333]);
> newset;
{@
[ 8, 2 ],
[ 4, 2 ],
[ 2, 8 ],
[ 8, 4 ],
[ 4, 4 ],
[ 8, 8 ],
[ 2, 2 ],
[ 4, 8 ],
[ 2, 4 ],
[ 333 ]
@}
Suppose you want to select a subset of a set having a certain property,
how do you do this? Take, for example, the
above cartesian product
,
and find all the elements which add to 6.
> Set1_2_2;
{@
[ 8, 2 ],
[ 4, 2 ],
[ 2, 8 ],
[ 8, 4 ],
[ 4, 4 ],
[ 8, 8 ],
[ 2, 2 ],
[ 4, 8 ],
[ 2, 4 ]
@}
> L:=[ x : x in Set1_2_2 | x[1]+x[2] eq 6];
> L;
[
[ 4, 2 ],
[ 2, 4 ]
]