#!/usr/local/bin/perl -w # primperma # obtained from # permalist # # makes alist matrices based on permutations derived from PRIMITIVE ELEMENTS. # # write them to code/GHC, please # # reads in # l - number of cols in constituent code, e.g. 16 # t - weight per column e.g. 2 # Nl - block length divided by l # # - outputs an alist corresponding to a set of permutation matrices thus: # # 1 1 1 1 1 1 1 1 # 1 P Q R S T U V # # 1 1 1 1 1 1 1 1 # 1 P Q R S T U V # 1 W X Y Z A B C # # the perms are constructed assuming that Nl+1 is a prime. # # here are some primes # # 101 # 211 # 223 # 227 # 307 # 401 # 503 # 1009 # 1277 # 2003 # 2131 # 2137 # 3001 # 4001 # 5003 # 10007 $seed = 0 ; eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; srand ( $seed ) ; $N = $Nl * $l ; $M = $Nl * $t ; if ( $N * $M <= 0 ) { print "usage: \n permalist.p l=16 t=2 Nl=100 seed=0 > alist.file\n" ; print " permalist.p l=16 t=2 Nl=400 > alist.file\n (Nl+1 prime)\n" ; exit(0); } $head = $N." ".$M."\n" ; $head .= $t." ".$l."\n" ; $head .= "$t "x$N ; $head .= "\n" ; $head .= "$l "x$M ; $head .= "\n" ; print $head ; # make null lists for ( $n = 1 ; $n <= $N ; $n ++ ) { $nlist[$n] = "" ; } for ( $m = 1 ; $m <= $M ; $m ++ ) { $mlist[$m] = "" ; } sub nullperm { for ( $nl = 1 ; $nl <= $Nl ; $nl ++ ) { $fto[$nl] = 0 ; } } sub checkperm { for ( $nl = 1 ; $nl <= $Nl ; $nl ++ ) { if ( $fto[$nl] != 1 ) { print STDERR "perm $q not valid $nl\n" ; for ( $n = 1 ; $n <= $Nl ; $n ++ ) { print STDERR "$n to $p[$n]\n" ; } exit(0) ; } } } # invent a perm &nullperm(); for ( $nl = 1 ; $nl <= $Nl ; $nl ++ ) { $from = $nl ; $to = $nl ; $p[$from] = $to ; $pi[$to] = $from ; # the inverse, in case it is useful $fto[$to] ++ ; } &checkperm(); for ( # define what the offset of our square is $moff = 0 , $noff = 0 , $ll = 1 ; $ll <= $l ; $ll ++ , $noff += $Nl ) { # put in this perm &permadd(); } for ( $q = 0 ; $q <= $Nl ; $q ++ ) { $usedq[$q] = 0 ; } $usedq[1] = 1 ; $usedq[0] = 1 ; $usedq[$Nl] = 1 ; for ( $tt = 2 ; $tt <= $t ; $tt ++ ) { # do the same for the next row bottom left ... $moff += $Nl ; $noff = 0 ; # identity perm for ( $nl = 1 ; $nl <= $Nl ; $nl ++ ) { $from = $nl ; $to = $nl ; $p[$from] = $to ; $pi[$to] = $from ; # the inverse, in case it is useful } # put in this perm &permadd(); # OK, now we need some interesting perms # # one idea is to assume that Nl+1 is prime. # then let i -> q i mod (Nl+1) for ( # define what the offset of our square is $noff = $Nl , $ll = 2 ; $ll <= $l ; $ll ++ , $noff += $Nl ) { do { # find a q that has not been used and that is primitive do { $q = int ( rand (( $Nl -1 )/2.0) ) + 1 ; } while ( $usedq[$q] ) ; $usedq[$q] ++ ; print STDERR "--- $q --- \t" ; # invent a perm &nullperm(); $primitive = 1 ; $from = 1 ; # attempt to generate the whole group by multiplying by q for ( $nl = 1 ; $nl <= $Nl ; $nl ++ ) { $to = ( $from * $q ) % ( $Nl + 1 ) ; $p[$from] = $to ; $pi[$to] = $from ; # the inverse, in case it is useful $fto[$to] ++ ; $from = $to ; if (( $to == 1 ) && ( $nl != $Nl ) ) { print STDERR " not primitive\n" ; $primitive = 0 ; last; } } } while ( !$primitive ) ; print STDERR "primitive!\n" ; # put in this perm &checkperm(); &permadd(); } } sub permadd { for ( $nl = 1 ; $nl <= $Nl ; $nl ++ ) { $n = $noff + $nl ; $m = $p[$nl] + $moff ; # put m and n on each others lists $nlist[$n] .= $m."\t" ; $mlist[$m] .= $n."\t" ; } } # finish lists for ( $n = 1 ; $n <= $N ; $n ++ ) { $nlist[$n] =~ s/\t$/\n/ ; print $nlist[$n] ; } for ( $m = 1 ; $m <= $M ; $m ++ ) { $mlist[$m] =~ s/\t$/\n/ ; print $mlist[$m] ; }