You could also write a GAP script to output the matrix to R. For example:

MatrixToR:=function(M)
  local nrow,ncol;
  nrow:=Size(M);
  ncol:=Size(M[1]);
  Print("A = matrix(c(");
  for i in [1..nrow] do
    for j in [1..ncol] do
      if(i=nrow and j=ncol) then Print(M[i][j]); continue; fi;
      Print(M[i][j],", ");
    od;
  od;
  Print("),nrow=",nrow,",ncol=",ncol,",byrow=TRUE)\n");
end;;

So, if we run this

MatrixToR(MultiplicationTable(Random(AllSmallGroups(12))));

it produces:

A = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 2, 1, 5, 6, 3, 4, 9, 10, 7, 8, 12, 11, 3, 5, 1, 7, 2, 9, 4, 11, 6, 12, 8, 10, 4, 6, 7, 8, 9, 10, 11, 1, 12, 2, 3, 5, 5, 3, 2, 9, 1, 7, 6, 12, 4, 11, 10, 8, 6, 4, 9, 10, 7, 8, 12, 2, 11, 1, 5, 3, 7, 9, 4, 11, 6, 12, 8, 3, 10, 5, 1, 2, 8, 10, 11, 1, 12, 2, 3, 4, 5, 6, 7, 9, 9, 7, 6, 12, 4, 11, 10, 5, 8, 3, 2, 1, 10, 8, 12, 2, 11, 1, 5, 6, 3, 4, 9, 7, 11, 12, 8, 3, 10, 5, 1, 7, 2, 9, 4, 6, 12, 11, 10, 5, 8, 3, 2, 9, 1, 7, 6, 4),nrow=12,ncol=12,byrow=TRUE)

which can be input into R an plotted in colour using the plotrix function color2D.matplot, as follows

require(plotrix)
color2D.matplot(A)

which will plot

Final result

(You may need to run install.packages("plotrix") to install the R package.)


I don't believe there is such a feature in GAP (or, at least, I'm not aware of one). But it would be possible to write a script to output LaTeX code. For example:

MatrixToLaTeX:=function(M)
  local i,j,m,n;
  m:=Size(M);
  n:=Size(M[1]);
  Print("\\begin{tikzpicture}\n\\tikzset{square matrix/.style={\n    matrix of nodes,\n    column sep=-\\pgflinewidth,\n    row sep=-\\pgflinewidth,\n    nodes={draw,\n      minimum height=#1,\n      anchor=center,\n      text width=#1,\n      align=center,\n      inner sep=0pt\n      },\n    },\n  square matrix/.default=1.2cm\n}\n\n");
  Print("\\matrix[square matrix]\n{\n");
  for i in [1..m] do
    for j in [1..n-1] do
      Print("|[fill=blue!",10+8*M[i][j],"]| & ");
    od;
    Print("|[fill=blue!",10+8*M[i][n],"]| \\\\\n");
  od;
  Print("};\n\n\\end{tikzpicture}\n");
end;;

which, when we run

gap> MatrixToLaTeX(MultiplicationTable(Random(AllSmallGroups(12))));

produces the tikz input:

\begin{tikzpicture}
\tikzset{square matrix/.style={
    matrix of nodes,
    column sep=-\pgflinewidth,
    row sep=-\pgflinewidth,
    nodes={draw,
      minimum height=#1,
      anchor=center,
      text width=#1,
      align=center,
      inner sep=0pt
      },
    },
  square matrix/.default=1.2cm
}

\matrix[square matrix]
{
|[fill=blue!18]| & |[fill=blue!26]| & |[fill=blue!34]| & |[fill=blue!42]| & |[fill=blue!50]| & |[fill=blue!58]| & |[fill=blue!66]| & |[fill=blue!74]| & |[fill=blue!82]| & |[fill=blue!90]| & |[fill=blue!98]| & |[fill=blue!106]| \\
|[fill=blue!26]| & |[fill=blue!18]| & |[fill=blue!50]| & |[fill=blue!58]| & |[fill=blue!34]| & |[fill=blue!42]| & |[fill=blue!82]| & |[fill=blue!90]| & |[fill=blue!66]| & |[fill=blue!74]| & |[fill=blue!106]| & |[fill=blue!98]| \\
|[fill=blue!34]| & |[fill=blue!50]| & |[fill=blue!18]| & |[fill=blue!66]| & |[fill=blue!26]| & |[fill=blue!82]| & |[fill=blue!42]| & |[fill=blue!98]| & |[fill=blue!58]| & |[fill=blue!106]| & |[fill=blue!74]| & |[fill=blue!90]| \\
|[fill=blue!42]| & |[fill=blue!90]| & |[fill=blue!66]| & |[fill=blue!74]| & |[fill=blue!106]| & |[fill=blue!26]| & |[fill=blue!98]| & |[fill=blue!18]| & |[fill=blue!50]| & |[fill=blue!58]| & |[fill=blue!34]| & |[fill=blue!82]| \\
|[fill=blue!50]| & |[fill=blue!34]| & |[fill=blue!26]| & |[fill=blue!82]| & |[fill=blue!18]| & |[fill=blue!66]| & |[fill=blue!58]| & |[fill=blue!106]| & |[fill=blue!42]| & |[fill=blue!98]| & |[fill=blue!90]| & |[fill=blue!74]| \\
|[fill=blue!58]| & |[fill=blue!74]| & |[fill=blue!82]| & |[fill=blue!90]| & |[fill=blue!98]| & |[fill=blue!18]| & |[fill=blue!106]| & |[fill=blue!26]| & |[fill=blue!34]| & |[fill=blue!42]| & |[fill=blue!50]| & |[fill=blue!66]| \\
|[fill=blue!66]| & |[fill=blue!106]| & |[fill=blue!42]| & |[fill=blue!98]| & |[fill=blue!90]| & |[fill=blue!50]| & |[fill=blue!74]| & |[fill=blue!34]| & |[fill=blue!26]| & |[fill=blue!82]| & |[fill=blue!18]| & |[fill=blue!58]| \\
|[fill=blue!74]| & |[fill=blue!58]| & |[fill=blue!98]| & |[fill=blue!18]| & |[fill=blue!82]| & |[fill=blue!90]| & |[fill=blue!34]| & |[fill=blue!42]| & |[fill=blue!106]| & |[fill=blue!26]| & |[fill=blue!66]| & |[fill=blue!50]| \\
|[fill=blue!82]| & |[fill=blue!98]| & |[fill=blue!58]| & |[fill=blue!106]| & |[fill=blue!74]| & |[fill=blue!34]| & |[fill=blue!90]| & |[fill=blue!50]| & |[fill=blue!18]| & |[fill=blue!66]| & |[fill=blue!26]| & |[fill=blue!42]| \\
|[fill=blue!90]| & |[fill=blue!42]| & |[fill=blue!106]| & |[fill=blue!26]| & |[fill=blue!66]| & |[fill=blue!74]| & |[fill=blue!50]| & |[fill=blue!58]| & |[fill=blue!98]| & |[fill=blue!18]| & |[fill=blue!82]| & |[fill=blue!34]| \\
|[fill=blue!98]| & |[fill=blue!82]| & |[fill=blue!74]| & |[fill=blue!34]| & |[fill=blue!58]| & |[fill=blue!106]| & |[fill=blue!18]| & |[fill=blue!66]| & |[fill=blue!90]| & |[fill=blue!50]| & |[fill=blue!42]| & |[fill=blue!26]| \\
|[fill=blue!106]| & |[fill=blue!66]| & |[fill=blue!90]| & |[fill=blue!50]| & |[fill=blue!42]| & |[fill=blue!98]| & |[fill=blue!26]| & |[fill=blue!82]| & |[fill=blue!74]| & |[fill=blue!34]| & |[fill=blue!58]| & |[fill=blue!18]| \\
};

\end{tikzpicture}

which we add to a LaTeX document and compile to get:

Compiled output

To use this, we need the following in the LaTeX preamble:

\usepackage{tikz}
\usetikzlibrary{matrix}

You can do something like what you're asking for directly in GAP if you're running in a terminal that can interpret colour escapes. I don't know whether this will work in Windows, but it seems to work okay on UNIX.

Define some colours:

FGC := [ "30m", "31m", "32m", "33m", "34m", "35m", "36m", "37m" ];; # foreground colours
BGC := [ "40m", "41m", "42m", "43m", "44m", "45m", "46m", "47m" ];; # background colours

For your small example, we only need the foreground colours, but you could handle a larger group by using various combinations of foreground and background colours. Modify your loop as follows.

for i in [1..n] do
for j in [1..n] do
    k := M[ i ][ j ];
    Print( "\033[", FGC[ k ] ); # set colour
    Print( k );                 # print number
    Print( "\033[0m" );         # reset
    Print( " " );               # space
od;
Print( "\n" );
od;;

I get something like this in my terminal:

Terminal snapshot

It's not as nice as the graphical solutions, but it does give you something without having to move your data to another package. You might be able to develop this idea into something more complete.


I don't know how you could do it in GAP, but if you take it out of GAP and put it into Mathematica in the form {{1,2,3,4},{2,1,4,3},{3,4,1,2},{4,3,2,1}} (which should be easy) then you can use MatrixPlot. For example,

A = {{1,2,3,4},{2,1,4,3},{3,4,1,2},{4,3,2,1}};
MatrixPlot[A, ColorFunction -> "Rainbow", Frame -> False]

yields

Rainbow.

You can see the full set of preset color schemes here. If you don't like those you can make your own without much extra work.