Tell me more ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Is it possible to define arrays in GAP ? If not what is the replacement ?

share|improve this question
3  
You should ask this on the GAP mailing list. – Mariano Suárez-Alvarez Mar 11 at 18:50
From the FAQ: "We welcome questions about: Software that mathematicians use". (Note: the current answer misses an important way to generate arrays in GAP.) – Douglas S. Stones Mar 31 at 15:39
2  
@DouglasS.Stones, I am aware of the FAQ, but this is not even a mathematical question related to GAP, it is a programming question which can be answered by reading GAP's documentation, regarding a part of GAP which is not related in an way to our subject. A lot of mathematicians use tar but asking how to create a tar file preserving SELinux security contexts here would be off-topic! (and also answerable by reading the fine manual :-) ) – Mariano Suárez-Alvarez Apr 2 at 18:44
1  
Also, I'd like to note that GAP's manual is over 1000 pages long, and finding even very basic things can take a considerable effort. Searching it for the word array results in many irrelevant hits. A more realistic approach is using GAP's help function, e.g. ?Determinant, to find help, but even then ?array does not produce anything helpful. – Douglas S. Stones Apr 2 at 19:45
1  
+1 to @MarianoSuárez-Alvarez - I agree that for such type of questions GAP Forum or GAP Support would be the most appropriate. – Alexander Konovalov Apr 21 at 18:20
show 2 more comments

closed as off topic by Math Gems, Micah, Arkamis, Sasha, Lord_Farin Apr 22 at 16:32

Questions on Mathematics Stack Exchange are expected to relate to math within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

2 Answers

gap> a := [1, 2, 3];
[ 1, 2, 3 ]
share|improve this answer
what about multi-dimension arrays? is it possible without assigning the values directly? – Algo Mar 11 at 19:03
1  
Multi-dimensional array = array of arrays. – Jim Mar 11 at 19:04
what about multi-dimension arrays? is it possible without assigning the values directly? – Algo Mar 11 at 19:05
1  
I mean how to define it with large dimensions - it would be so difficult to set all the values by the declaration - – Algo Mar 11 at 19:07
You can assign to a position in the list without initializing the list to be that size, so using a for loop to do that isn't that hard if you know what the dimension is ahead of time. If you don't then maybe a hash table is a better option: gap-system.org/Manuals/doc/ref/chap28.html#X867203C5877489A2 – Jim Mar 11 at 20:53

Yes it is possible. GAP is rather versitile, so there's probably a number of ways of generating arrays, many of which I won't be able to address here since I'm not yet familiar with them. But I'll give some ways I use frequently.

It is important to know that arrays in GAP are indexed starting from 1. This means there is no 0-th element.

Method 1: We can input arrays directly by typing them. A simple example:

gap> A:=[1,2,3,4,5];
[ 1, 2, 3, 4, 5 ]

In the above example, there is a shorthand:

gap> A:=[1..5];
[ 1 .. 5 ]

or as a more complicated example:

gap> [2,4..20];
[ 2, 4 .. 20 ]

Here's a 2D example:

gap> B:=[[1,2,3],[4,5,6],[7,8,9]];
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]

and we can generate arrays of arbitrarily high dimensions by taking arrays of arrays of ... of arrays. However, this input method can be time consuming if you have to type in arrays manually. Fortunately, there are other ways.

Method 2: Adding an entry to a cell.

gap> C:=[];
[  ]
gap> C[5]:=B;
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
gap> C[10]:=2^10;
1024
gap> C;
[ ,,,, [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ],,,,, 1024 ]

In the above example, we have 1D array of length 10 where the 5-th entry is the matrix $B$ and the 10-th entry is $2^{10}$. The other entries are empty.

Combining this method with for loops can make generating large matrices easier:

gap> D:=[];
[  ]
gap> for i in [1..3] do
>   D[i]:=[];
>   for j in [1..3] do
>     D[i][j]:=3*i+j;
>   od;
> od;
gap> D;
[ [ 4, 5, 6 ], [ 7, 8, 9 ], [ 10, 11, 12 ] ]

However, there is an even more succinct method.

Method 3: Defining arrays via List:

gap> M:=List([1..10],i->i^2);
[ 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 ]

This is extremely handy for generating integer sequences for Sloane's OEIS.

Or we can combine these to generate 2D arrays:

gap> N:=List([1..3],i->List([1..3],j->3*i+j));
[ [ 4, 5, 6 ], [ 7, 8, 9 ], [ 10, 11, 12 ] ]

The $3i+j$ function can be replaced by an internal function, e.g.:

gap> O:=List([1..3],i->List([1..3],j->Random([1..100])));
[ [ 97, 71, 66 ], [ 7, 88, 6 ], [ 98, 95, 36 ] ]

or a user-defined function, e.g.:

gap> f:=function(i,j)
>   if(IsPrimeInt(i+j)) then
>     return 1;
>   else
>    return 0;
>   fi;
> end;
function( i, j ) ... end
gap> P:=List([1..3],i->List([1..3],j->f(i,j)));
[ [ 1, 1, 0 ], [ 1, 0, 1 ], [ 0, 1, 0 ] ]

Method 4: Defining arrays from arrays.

There are several methods for modifying arrays to give other arrays.

gap> Filtered([1..20],i->i^2 mod 3=1);
[ 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20 ]

In the above example, we start with the array [1..20] and keep only the elements $i$ for which $i^2 \equiv 1 \pmod 3$.

gap> Filtered([1..20],IsPrimeInt);
[ 2, 3, 5, 7, 11, 13, 17, 19 ]

The above is equivalent to Filtered([1..20],i->IsPrimeInt(i));

Method 5: Multiplication tables. Since GAP is somewhat groups focused, they should get a mention. We can obtain matrices as the multiplication table of groups. For example:

gap> G:=Random(AllSmallGroups(8));
<pc group of size 8 with 3 generators>
gap> MultiplicationTable(G);
[ [ 1, 2, 3, 4, 5, 6, 7, 8 ], [ 2, 4, 5, 6, 7, 1, 8, 3 ], [ 3, 5, 1, 7, 2, 8, 4, 6 ], [ 4, 6, 7, 1, 8, 2, 3, 5 ], 
  [ 5, 7, 2, 8, 4, 3, 6, 1 ], [ 6, 1, 8, 2, 3, 4, 5, 7 ], [ 7, 8, 4, 3, 6, 5, 1, 2 ], [ 8, 3, 6, 5, 1, 7, 2, 4 ] ]
gap> StructureDescription(G);
"C4 x C2"
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.