# Cayley table group visualization

how can I make graphics like this? random colors. I got a script in GAP that prints rows of numbers but I want it colored just random colors

G:=Units(Integers mod 2^3);
n:=Order(G);
M:=MultiplicationTable(G);

for i in [1..n] do
for j in [1..n] do
Print(M[i][j]," ");
od;
Print("\n");
od;
Print("\n");


I'd like to see group table so I can think about the group better

 1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1

-
See Wolfram website in mathworld.wolfram.com/FiniteGroupC2xC2.html – MathOverview Jan 10 '13 at 22:33

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

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

-
Do the packages you noted above work under windows? Or they work under Linux? – Babak S. Jan 9 '13 at 7:55
I run Ubuntu Linux, so these methods work for that OS. The LaTeX/tikz method will surely work on Windows (you just need to install latex and tikz, which can be done reasonably easy via TeXnicCenter and MikTeX). I've not run the R method on Windows, but I'd be quite surprised if it were significantly different than on Linux. – Douglas S. Stones Jan 9 '13 at 8:04

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:

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

\usepackage{tikz}
\usetikzlibrary{matrix}

-
+1, yours looks better :) – Alexander Gruber Jan 9 '13 at 1:24

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:

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.

-
this is really really nice! sadly there are too few colors for bigger groups. – ryu jin Jan 10 '13 at 23:03

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

.

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.

-

If you have access to Maple, there is a built-in command to do this. You can create the Cayley table fairly directly from the GAP matrix in Maple as follows.

M := [ [ 1, 2, 3, 4 ], [ 2, 1, 4, 3 ], [ 3, 4, 1, 2 ], [ 4, 3, 2, 1 ] ]:
m := Matrix( M ):
with( Magma ):
CayleyColourTable( m );


This produces an image something like this:

Then you can use options to tweak the output.

-

I know I'm jumping in quite late here, but for posterity, here is what I used in Sage 6.4:

C=AbelianGroup([2,2])
M=C.cayley_table().table()
matrix_plot(M)


Matrix plot has various colour options. Try

matrix_plot(M,options={'cmap':'winter'})

-

You might find interesting a computer app called Group Explorer.

The app provides visualizations of 59 groups.

All visualizations can be exported via clipboard.

Here are some features of multiplication table visualizations:

-