#!/usr/bin/perl
#
# The Ising model on a two-dimensional torus with L lines and C columns.
# As in "count_colors", it's more efficient to take L <= C.
# The result is a polynomial in f, and the coefficient a_k of f^k is the
# number of combinations such that there are k edges between vertices of
# different spins. So high values of k correspond to high energies, and
# the sum of all a_k is 2^(L.C).
#
die "Usage: $0 <lines> <columns>\n" unless ($#ARGV == 1);
$lines = $ARGV[0];
$columns = $ARGV[1];
print <<"EOF" ;
Up = Down = 1;
Cell =	Up	* L!1.D!1.(R!1+f.R!2).(U!1+f.U!2) +
	Down	* L!2.D!2.(f.R!1+R!2).(f.U!1+U!2);	
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";
