Error in Computing the Two-Fold Sumset of a List in the GAP System

32 views Asked by At

I am attempting to write a code in the GAP System that will allow me to construct the two-fold sumset of a set with itself. Essentially, this is just the set of elements that can be written as a sum of two elements of a given set. Explicitly, if S is the set (or list) in question, then its two-fold sumset is the set (or list) S + S.

In the GAP System, I have devised the following code to achieve this construction. I am going to provide a test set for demonstrative purposes, but the user should be able to provide the test set in general.

A := [8, 11, 15, 16, 17, 19, 20, 22, 23, 24, 25, 
      26, 27, 28, 30, 31, 32, 33, 34, 35, 36, 37];
L := [ ];
for i in A do 
  for j in A do 
    Add(L, i + j); 
  od; 
od;

I expect this code to add all possible pairs of elements of A and collect them in an (initially empty) list L; however, when I implement the code, the element 22 = 11 + 11 is mysteriously absent from L!

I admit that I am a novice programmer and perhaps this construction could be implemented in a more efficient manner, but this is the first thing that came to mind. I would appreciate any insight or advice on this matter. I thank everyone in advance for their time and consideration.

1

There are 1 answers

4
Olexandr Konovalov On BEST ANSWER
  1. Actually, 22 is there:
gap> 22 in L;
true
gap> Position(L,22);
24
  1. Depending on the purpose, you might want to use AddSet instead of Add to eliminate duplicates, or at least sort the resulting list L
gap> L1 := [ ];;
gap> for i in A do for j in A do AddSet(L1, i + j); od; od;
gap> L1=Set(L); # just to check
true
  1. Again, depending on the purpose, and commutativity of the operation (are you dealing with lists of integers, or it's just an example, while you may be dealing with some other kinds of objects?), you may also want further optimisation, to avoid calculating b+a after you have already calculated a+b (you said you want "set (or list) S + S" so it's not exactly clear).
gap> L2 := [ ];;
gap> for i in [1..Length(A)] do
>      for j in [i..Length(A)] do
>        AddSet(L2, A[i] + A[j]);
>      od;
>    od;
gap> L2=Set(L);
true