#!/usr/bin/perl
#
# This perl script takes two arguments, L and C, and generates a script
# which can be piped into samuel. It counts the number of ways to colour
# a LxC torus with three colours so that adjacent cells have different
# colours. Obviously the result is symmetric in L and C, but for efficiency
# reasons it's better to have L <= C. For example, for a 4x5 torus,
# one would write
#
#	./count_colors 4 5 | ./samuel -tZ
#
# Can you understand how things are computed? Each cell is represented as
# a tensor of order 4 (one index for each side) which is contracted with
# the tensors of the adjacent cells. Look closely...
#
die "Usage: $0 <lines> <columns>\n" unless ($#ARGV == 1);
$lines = $ARGV[0];
$columns = $ARGV[1];
print <<"EOF" ;
Red = Green = Blue = 1;

Cell =	Red	* ((L!2+L!3).R!1 * (D!2+D!3).U!1) +
	Green	* ((L!1+L!3).R!2 * (D!1+D!3).U!2) +
	Blue	* ((L!1+L!2).R!3 * (D!1+D!2).U!3) ;

Nb = 1;
EOF
foreach $x (0 .. $columns-1) {
    foreach $y (0 .. $lines-1) {
    	$X = $x+1; $X = 0 if ($X == $columns);
    	$Y = $y+1; $Y = 0 if ($Y == $lines);
	print <<"EOL";
Nb = Nb * (Cell,L=>H[$x,$y],R=>H[$X,$y],D=>V[$x,$y],U=>V[$x,$Y]);
EOL
    }
}
print "Nb;\n";
