  
  [1X1 Set-Theoretic Unions of Residue Classes[0X
  
  
  [1X1.1 Entering residue classes and set-theoretic unions thereof[0X
  
  [1X1.1-1 ResidueClass[0m
  
  [2X> ResidueClass( [0X[3XR, m, r[0X[2X ) __________________________________________[0Xfunction
  [2X> ResidueClass( [0X[3Xm, r[0X[2X ) _____________________________________________[0Xfunction
  [2X> ResidueClass( [0X[3Xr, m[0X[2X ) _____________________________________________[0Xfunction
  [6XReturns:[0X  In  the  three-argument  form  the  residue  class  [3Xr[0m mod [3Xm[0m of the
            ring [3XR[0m,  and in the two-argument form the residue class [3Xr[0m mod [3Xm[0m of
            the "default ring" (-> [10XDefaultRing[0m in the [5XGAP[0m Reference Manual) of
            the arguments.
  
  In  the  two-argument case, [3Xm[0m is taken to be the larger and [3Xr[0m is taken to be
  the  smaller  of  the arguments. For convenience, it is permitted to enclose
  the argument list in list brackets.
  
  Residue  classes  have  the  property  [10XIsResidueClass[0m. Rings are regarded as
  residue  class  0 (mod 1),  and  therefore  have  this  property.  There are
  operations  [10XModulus[0m and [10XResidue[0m to retrieve the modulus [3Xm[0m resp. residue [3Xr[0m of
  a residue class.
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> ResidueClass(2,3);[0X
    [4XThe residue class 2(3) of Z[0X
    [4Xgap> ResidueClass(Z_pi([2,5]),2,1);[0X
    [4XThe residue class 1(2) of Z_( 2, 5 )[0X
    [4Xgap> R := PolynomialRing(GF(2),1);;[0X
    [4Xgap> x := Indeterminate(GF(2),1);; SetName(x,"x");[0X
    [4Xgap> ResidueClass(R,x+One(R),Zero(R));[0X
    [4XThe residue class 0*Z(2) ( mod x+Z(2)^0 ) of GF(2)[x][0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  [1X1.1-2 ResidueClassUnion[0m
  
  [2X> ResidueClassUnion( [0X[3XR, m, r[0X[2X ) _____________________________________[0Xfunction
  [2X> ResidueClassUnion( [0X[3XR, m, r, included, excluded[0X[2X ) _________________[0Xfunction
  [6XReturns:[0X  The  union of the residue classes [3Xr[0m[i] mod [3Xm[0m of the ring [3XR[0m, plus /
            minus finite sets [3Xincluded[0m and [3Xexcluded[0m of elements of [3XR[0m.
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> ResidueClassUnion(Integers,5,[1,2],[3,8],[-4,1]);[0X
    [4X(Union of the residue classes 1(5) and 2(5) of Z) U [ 3, 8 ] \ [ -4, 1 ][0X
    [4Xgap> ResidueClassUnion(Z_pi([2,3]),8,[3,5]);[0X
    [4XUnion of the residue classes 3(8) and 5(8) of Z_( 2, 3 )[0X
    [4Xgap> ResidueClassUnion(R,x^2,[One(R),x],[],[One(R)]);[0X
    [4X<union of 2 residue classes (mod x^2) of GF(2)[x]> \ [ Z(2)^0 ][0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  When  talking about a [13Xresidue class union[0m in this chapter, we always mean an
  object as it is returned by this function.
  
  There    are    operations    [10XModulus[0m,    [10XResidues[0m,   [10XIncludedElements[0m   and
  [10XExcludedElements[0m to retrieve the components of a residue class union as they
  have originally been passed as arguments to [2XResidueClassUnion[0m.
  
  The  user has the choice between a longer and more descriptive and a shorter
  and less bulky output format for residue classes and unions thereof:
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> ResidueClassUnionViewingFormat("short");[0X
    [4Xgap> ResidueClassUnion(Integers,12,[0,1,4,7,8]);[0X
    [4X0(4) U 1(6)[0X
    [4Xgap> ResidueClassUnionViewingFormat("long");[0X
    [4Xgap> ResidueClassUnion(Integers,12,[0,1,4,7,8]);[0X
    [4XUnion of the residue classes 0(4) and 1(6) of Z[0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  [1X1.1-3 AllResidueClassesModulo[0m
  
  [2X> AllResidueClassesModulo( [0X[3XR, m[0X[2X ) __________________________________[0Xfunction
  [2X> AllResidueClassesModulo( [0X[3Xm[0X[2X ) _____________________________________[0Xfunction
  [6XReturns:[0X  A sorted list of all residue classes (mod [3Xm[0m) of the ring [3XR[0m.
  
  If the argument [3XR[0m is omitted it defaults to the default ring of [3Xm[0m -- cf. the
  documentation  of [10XDefaultRing[0m in the [5XGAP[0m reference manual. A transversal for
  the   residue   classes   (mod [3Xm[0m)   can   be   obtained   by  the  operation
  [10XAllResidues([3XR[0m[10X,[3Xm[0m[10X)[0m,  and  their  number  can  be  determined  by the operation
  [10XNumberOfResidues([3XR[0m[10X,[3Xm[0m[10X)[0m.
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> AllResidueClassesModulo(Integers,2);[0X
    [4X[ The residue class 0(2) of Z, The residue class 1(2) of Z ][0X
    [4Xgap> AllResidueClassesModulo(Z_pi(2),2);[0X
    [4X[ The residue class 0(2) of Z_( 2 ), The residue class 1(2) of Z_( 2 ) ][0X
    [4Xgap> AllResidueClassesModulo(R,x);[0X
    [4X[ The residue class 0*Z(2) ( mod x ) of GF(2)[x], [0X
    [4X  The residue class Z(2)^0 ( mod x ) of GF(2)[x] ][0X
    [4Xgap> AllResidues(R,x^3);  [0X
    [4X[ 0*Z(2), Z(2)^0, x, x+Z(2)^0, x^2, x^2+Z(2)^0, x^2+x, x^2+x+Z(2)^0 ][0X
    [4Xgap> NumberOfResidues(Z_pi([2,3]),360);[0X
    [4X72[0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  
  [1X1.2 Methods for residue class unions[0X
  
  There  are  methods  for  [10XPrint[0m,  [10XString[0m and [10XDisplay[0m which are applicable to
  residue class unions. There is a method for [10Xin[0m which tests whether some ring
  element lies in a given residue class union.
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> Print(ResidueClass(1,2),"\n");[0X
    [4XResidueClassUnion( Integers, 2, [ 1 ] )[0X
    [4Xgap> 1 in ResidueClass(1,2);[0X
    [4Xtrue[0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  There are methods for [10XUnion[0m, [10XIntersection[0m, [10XDifference[0m and [10XIsSubset[0m available
  for  residue  class unions. They also accept finite subsets of the base ring
  as arguments.
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> S := Union(ResidueClass(0,2),ResidueClass(0,3));[0X
    [4XZ \ Union of the residue classes 1(6) and 5(6) of Z[0X
    [4Xgap> Intersection(S,ResidueClass(0,7));[0X
    [4XUnion of the residue classes 0(14) and 21(42) of Z[0X
    [4Xgap> Difference(S,ResidueClass(2,4));[0X
    [4XUnion of the residue classes 0(4) and 3(6) of Z[0X
    [4Xgap> IsSubset(ResidueClass(0,2),ResidueClass(4,8));[0X
    [4Xtrue[0X
    [4Xgap> Union(S,[1..10]);[0X
    [4X(Union of the residue classes 0(2) and 3(6) of Z) U [ 1, 5, 7 ][0X
    [4Xgap> Intersection(S,[1..6]);[0X
    [4X[ 2, 3, 4, 6 ][0X
    [4Xgap> Difference(S,[1..6]);[0X
    [4X(Union of the residue classes 0(2) and 3(6) of Z) \ [ 2, 3, 4, 6 ][0X
    [4Xgap> Difference(Integers,[1..10]);[0X
    [4XZ \ <set of cardinality 10>[0X
    [4Xgap> IsSubset(S,[1..10]);[0X
    [4Xfalse[0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  If  the  underlying  ring has a residue class ring of a given cardinality t,
  then a residue class can be written as a disjoint union of t residue classes
  with equal moduli:
  
  [1X1.2-1 SplittedClass[0m
  
  [2X> SplittedClass( [0X[3Xcl, t[0X[2X ) __________________________________________[0Xoperation
  [6XReturns:[0X  A  partition  of  the residue class [3Xcl[0m into [3Xt[0m residue classes with
            equal  moduli,  provided  that  such a partition exists. Otherwise
            [10Xfail[0m.
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> SplittedClass(ResidueClass(1,2),2);[0X
    [4X[ The residue class 1(4) of Z, The residue class 3(4) of Z ][0X
    [4Xgap> SplittedClass(ResidueClass(Z_pi(3),3,0),2);[0X
    [4Xfail[0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  Often  one  needs  a  partition  of  a  given residue class union into "few"
  residue classes. The following operation takes care of this:
  
  [1X1.2-2 AsUnionOfFewClasses[0m
  
  [2X> AsUnionOfFewClasses( [0X[3XU[0X[2X ) ________________________________________[0Xoperation
  [6XReturns:[0X  A set of disjoint residue classes whose union is equal to [3XU[0m, up to
            the finite sets [10XIncludedElements([3XU[0m[10X)[0m and [10XExcludedElements([3XU[0m[10X)[0m.
  
  As  the  name of the operation suggests, it is taken care that the number of
  residue  classes  in the returned list is kept "reasonably small". It is not
  guaranteed that it is minimal.
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> ResidueClassUnionViewingFormat("short");[0X
    [4Xgap> AsUnionOfFewClasses(Difference(Integers,ResidueClass(0,30)));[0X
    [4X[ 1(2), 2(6), 4(6), 6(30), 12(30), 18(30), 24(30) ][0X
    [4Xgap> Union(last);[0X
    [4XZ \ 0(30)[0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  One can compute the sets of sums, differences, products and quotients of the
  elements of a residue class union and an element of the base ring:
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> ResidueClass(0,2) + 1;[0X
    [4X1(2)[0X
    [4Xgap> ResidueClass(0,2) - 2 = ResidueClass(0,2);[0X
    [4Xtrue[0X
    [4Xgap> 3 * ResidueClass(0,2);[0X
    [4X0(6)[0X
    [4Xgap> ResidueClass(0,2)/2;[0X
    [4XIntegers[0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  [1X1.2-3 PartitionsIntoResidueClasses[0m
  
  [2X> PartitionsIntoResidueClasses( [0X[3XR, length[0X[2X ) _______________________[0Xoperation
  [6XReturns:[0X  A  sorted list of all partitions of the ring [3XR[0m into [3Xlength[0m residue
            classes.
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> PartitionsIntoResidueClasses(Integers,4);[0X
    [4X[ [ 0(2), 1(4), 3(8), 7(8) ], [ 0(2), 3(4), 1(8), 5(8) ], [0X
    [4X  [ 0(2), 1(6), 3(6), 5(6) ], [ 1(2), 0(4), 2(8), 6(8) ], [0X
    [4X  [ 1(2), 2(4), 0(8), 4(8) ], [ 1(2), 0(6), 2(6), 4(6) ], [0X
    [4X  [ 0(3), 1(3), 2(6), 5(6) ], [ 0(3), 2(3), 1(6), 4(6) ], [0X
    [4X  [ 1(3), 2(3), 0(6), 3(6) ], [ 0(4), 1(4), 2(4), 3(4) ] ][0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  [1X1.2-4 RandomPartitionIntoResidueClasses[0m
  
  [2X> RandomPartitionIntoResidueClasses( [0X[3XR, length, primes[0X[2X ) __________[0Xoperation
  [6XReturns:[0X  A  "random"  partition  of  the ring [3XR[0m into [3Xlength[0m residue classes
            whose  moduli have only prime factors in [3Xprimes[0m, respectively [10Xfail[0m
            if no such partition exists.
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> RandomPartitionIntoResidueClasses(Integers,30,[2,3,5,7]);[0X
    [4X[ 0(7), 2(7), 5(7), 3(14), 10(14), 1(21), 8(21), 15(21), 18(21), 20(21), [0X
    [4X  6(63), 13(63), 25(63), 27(63), 32(63), 34(63), 46(63), 48(63), 53(63), [0X
    [4X  55(63), 4(126), 67(126), 137(189), 74(567), 200(567), 263(567), [0X
    [4X  389(567), 452(567), 11(1134), 578(1134) ][0X
    [4Xgap> Union(last);[0X
    [4XIntegers[0X
    [4Xgap> Sum(List(last2,Density));[0X
    [4X1[0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  [1X1.2-5 Density[0m
  
  [2X> Density( [0X[3XU[0X[2X ) ____________________________________________________[0Xoperation
  [6XReturns:[0X  The natural density of [3XU[0m as a subset of the underlying ring.
  
  The  [13Xnatural  density[0m  of  a  residue  class  r(m) of a ring R is defined by
  1/|R/mR|,  and  the  [13Xnatural  density[0m  of a union U of finitely many residue
  classes  is  defined  by  the  sum  of  the  densities  of the elements of a
  partition of U into finitely many residue classes.
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> Density(ResidueClass(0,2));[0X
    [4X1/2[0X
    [4Xgap> Density(Difference(Integers,ResidueClass(0,5)));[0X
    [4X4/5[0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  For looping over residue class unions of the integers, there are methods for
  the operations [10XIterator[0m and [10XNextIterator[0m.
  
  
  [1X1.3 On residue class unions of ℤ^2[0X
  
  Essentially,  residue  class  unions  of  ℤ^2 are treated like residue class
  unions  of  any  other  ring,  and  there  is roughly the same functionality
  available  for them. However there are some differences and a few additional
  features, which are described in this section.
  
  The  elements  of ℤ^2  are  represented  as  lists  of length 2 with integer
  entries.  The  modulus  of  a  residue class union of ℤ^2 is a lattice. This
  lattice  is  stored as a 2 x 2 integer matrix of full rank in Hermite normal
  form,  whose  rows  are  the spanning vectors. Residue classes of ℤ^2 modulo
  principal ideals are presently not implemented.
  
  Residue  class unions of ℤ^2 can be multiplied by matrices of full rank from
  the right. A snippet of a residue class union of ℤ^2 is shown in "ASCII art"
  when one [10XDisplay[0m's it with option [10XAsGrid[0m.
  
  We give some illustrative examples:
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> R := Integers^2;[0X
    [4X( Integers^2 )[0X
    [4Xgap> 5*R+[2,3];[0X
    [4X(2,3)+(5,0)Z+(0,5)Z[0X
    [4Xgap> Difference(R,last);[0X
    [4XZ^2 \ (2,3)+(5,0)Z+(0,5)Z[0X
    [4Xgap> Density(last);[0X
    [4X24/25[0X
    [4Xgap> L1 := [[2,1],[-1,2]];;[0X
    [4Xgap> L2 := [[6,2],[0,6]];;[0X
    [4Xgap> AllResidueClassesModulo(R,L1); # The modulus is transformed to HNF.[0X
    [4X[ (0,0)+(1,3)Z+(0,5)Z, (0,1)+(1,3)Z+(0,5)Z, (0,2)+(1,3)Z+(0,5)Z,[0X
    [4X  (0,3)+(1,3)Z+(0,5)Z, (0,4)+(1,3)Z+(0,5)Z ][0X
    [4Xgap> cl1 := ResidueClass(R,L1,[0,0]);[0X
    [4X(0,0)+(1,3)Z+(0,5)Z[0X
    [4Xgap> cl2 := ResidueClass(R,L2,[0,0]);[0X
    [4X(0,0)+(6,2)Z+(0,6)Z[0X
    [4Xgap> cl3 := Intersection(cl1,cl2);[0X
    [4X(0,0)+(6,8)Z+(0,30)Z[0X
    [4Xgap> S1 := Difference(cl1,cl2);[0X
    [4X<union of 35 residue classes (mod (6,8)Z+(0,30)Z)>[0X
    [4Xgap> S2 := Difference(cl2,cl1);[0X
    [4X<union of 4 residue classes (mod (6,8)Z+(0,30)Z)>[0X
    [4Xgap> Display(S1); # The set is written as union of "few" residue classes:[0X
    [4X(0,5)+(1,3)Z+(0,10)Z U (1,3)+(2,6)Z+(0,10)Z U (2,6)+(6,8)Z+(0,10)Z U[0X
    [4X(4,2)+(6,8)Z+(0,10)Z U (0,10)+(6,8)Z+(0,30)Z U (0,20)+(6,8)Z+(0,30)Z[0X
    [4Xgap> Display(S2);[0X
    [4X(0,6)+(6,8)Z+(0,30)Z U (0,12)+(6,8)Z+(0,30)Z U (0,18)+(6,8)Z+(0,30)Z[0X
    [4X U (0,24)+(6,8)Z+(0,30)Z[0X
    [4Xgap> cls := AsUnionOfFewClasses(S1);[0X
    [4X[ (0,5)+(1,3)Z+(0,10)Z, (1,3)+(2,6)Z+(0,10)Z, (2,6)+(6,8)Z+(0,10)Z,[0X
    [4X  (4,2)+(6,8)Z+(0,10)Z, (0,10)+(6,8)Z+(0,30)Z, (0,20)+(6,8)Z+(0,30)Z ][0X
    [4Xgap> Union(cls) = S1;[0X
    [4Xtrue[0X
    [4Xgap> S3 := S1*[[3,5],[2,4]];[0X
    [4X<union of 35 residue classes (mod (2,46)Z+(0,180)Z)>[0X
    [4Xgap> Display(S1:AsGrid);[0X
    [4X    *    *    *    *    *    *    *    *    *    *    *    *    *    *[0X
    [4X *    *    *    *    *    *    *    *    *    *    *    *    *    *    *[0X
    [4X   *    *    *    *    *    *    *    *    *    *    *    *    *    *[0X
    [4X*    *    *    *    *    *    *    *    *    *    *    *    *    *    *[0X
    [4X  *    *    *    *    *    *    *    *    *    *    *    *    *    *    *[0X
    [4X    *    *    *    *         *    *    *    *    *         *    *    *[0X
    [4X *    *    *    *    *    *    *    *    *    *    *    *    *    *    *[0X
    [4X   *    *    *    *    *    *    *    *    *    *    *    *    *    *[0X
    [4X*    *    *    *    *    *    *    *    *    *    *    *    *    *    *[0X
    [4X  *    *    *    *    *    *    *    *    *    *    *    *    *    *    *[0X
    [4X    *    *    *    *    *    *    *    *    *    *    *    *    *    *[0X
    [4X *    *    *         *    *    *    *    *         *    *    *    *    *[0X
    [4X   *    *    *    *    *    *    *    *    *    *    *    *    *    *[0X
    [4X*    *    *    *    *    *    *    *    *    *    *    *    *    *    *[0X
    [4X  *    *    *    *    *    *    *    *    *    *    *    *    *    *    *[0X
    [4X    *    *    *    *    *    *    *    *    *    *    *    *    *    *[0X
    [4X *    *    *    *    *    *    *    *    *    *    *    *    *    *    *[0X
    [4X   *         *    *    *    *    *         *    *    *    *    *[0X
    [4X*    *    *    *    *    *    *    *    *    *    *    *    *    *    *[0X
    [4X  *    *    *    *    *    *    *    *    *    *    *    *    *    *    *[0X
    [4X    *    *    *    *    *    *    *    *    *    *    *    *    *    *[0X
    [4X *    *    *    *    *    *    *    *    *    *    *    *    *    *    *[0X
    [4X   *    *    *    *    *    *    *    *    *    *    *    *    *    *[0X
    [4X     *    *    *    *    *         *    *    *    *    *         *    *[0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  
  [1X1.4 The categories and families of residue class unions[0X
  
  [1X1.4-1 IsResidueClassUnion[0m
  
  [2X> IsResidueClassUnion( [0X[3XU[0X[2X ) ___________________________________________[0Xfilter
  [2X> IsResidueClassUnionOfZ( [0X[3XU[0X[2X ) ________________________________________[0Xfilter
  [2X> IsResidueClassUnionOfZxZ( [0X[3XU[0X[2X ) ______________________________________[0Xfilter
  [2X> IsResidueClassUnionOfZ_pi( [0X[3XU[0X[2X ) _____________________________________[0Xfilter
  [2X> IsResidueClassUnionOfGFqx( [0X[3XU[0X[2X ) _____________________________________[0Xfilter
  [6XReturns:[0X  [10Xtrue[0m  if [3XU[0m is a residue class union, a residue class union of ℤ, a
            residue   class   union   of ℤ^2,  a  residue  class  union  of  a
            semilocalization  of ℤ  or  a  residue class union of a polynomial
            ring  in one variable over a finite field, respectively, and [10Xfalse[0m
            otherwise.
  
  Often  the  same methods can be used for residue class unions of the ring of
  integers  and of its semilocalizations. For this reason, there is a category
  [10XIsResidueClassUnionOfZorZ_pi[0m  which  is  the union of [10XIsResidueClassUnionOfZ[0m
  and  [10XIsResidueClassUnionOfZ_pi[0m. The internal representation of residue class
  unions   is  called  [10XIsResidueClassUnionResidueListRep[0m.  There  are  methods
  available for [10XExtRepOfObj[0m and [10XObjByExtRep[0m.
  
  [1X1.4-2 ResidueClassUnionsFamily[0m
  
  [2X> ResidueClassUnionsFamily( [0X[3XR[0X[2X ) ____________________________________[0Xfunction
  [2X> ResidueClassUnionsFamily( [0X[3XR, fixedreps[0X[2X ) _________________________[0Xfunction
  [6XReturns:[0X  The  family  of  residue  class  unions or the family of unions of
            residue   classes   with  fixed  representatives  of  the  ring [3XR[0m,
            depending on whether [3Xfixedreps[0m is present and [10Xtrue[0m or not.
  
  The  ring [3XR[0m can be retrieved as [10XUnderlyingRing(ResidueClassUnionsFamily([3XR[0m[10X))[0m.
  There  is  no  coercion  between  residue  class unions or unions of residue
  classes  with  fixed  representatives  which  belong  to different families.
  Unions  of  residue  classes with fixed representatives are described in the
  next chapter.
  
