#!/usr/local/bin/perl -w # permarand.p uses random perms. # - from permalistgen # # makes alist matrices based on permutations. # # bizarre version which makes a top row of "w" big I's # and a bottom row of P's. # # the max weight per row is e.g. 24 ("l") # the max weight per col is 2 # # Nl defines the size of one of the Ps # # write them to code/GHC, please # $seed = 0 ; $l = 24 ; $w = 3 ; # weight per row of the gallager part $real = 1 ; eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; print STDERR "setting t to 2\n" ; $t = 2 ; srand ( $seed ) ; $N = int ( $Nl * $l ) ; $NNN = $N / $w ; $M = $Nl + $NNN ; if ( $N * $M <= 0 ) { print "usage: \n permarand.p w=3 l=24 Nl=100 seed=0 > alist.file (l should be divisible by w)\n" ; exit(0); } if($real) { $head = $N." ".$M."\n" ; $head .= $t." ".$l."\n" ; $head .= "$t "x$N ; $head .= "\n" ; $head .= "$w "x$NNN ; # top N/3 rows have weight 3 $head .= "$l "x$Nl ; # the rest are weight 24 $head .= "\n" ; print $head ; } # make null lists for ( $n = 1 ; $n <= $N ; $n ++ ) { $nlist[$n] = "" ; $numnlist[$n] = 0 ; } for ( $m = 1 ; $m <= $M ; $m ++ ) { $mlist[$m] = "" ; $nummlist[$m] = 0 ; } 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) ; } } } # the first N/3 rows for ( $nl = 1 ; $nl <= $NNN ; $nl ++ ) { $n = $nl ; $m = $nl ; for ( $ww = 1 ; $ww <= $w ; $ww ++ , $n += $NNN ) { # put m and n on each others lists $nlist[$n] .= $m."\t" ; $mlist[$m] .= $n."\t" ; $numnlist[$n] ++ ; $nummlist[$m] ++ ; } } # register the identity as the first randperm for ( $nl = 1 ; $nl <= $Nl ; $nl ++ ) { $forbid[$nl] = $nl ; } $failed=0; $nrandperms = 1 ; $nnn = 1 ; for ( # define what the offset of our square is $moff = $NNN , $noff = 0 , $ll = 1 ; $ll <= $l ; $ll ++ , $noff += $Nl ) { # invent a perm print STDERR "Starting perm $nrandperms+1\n" ; &nullperm(); for ( $q = 1 ; $q <= $Nl ; $q ++ ) { $qs[$q-1] = $q ; } for ( $nl = 1 ; $nl <= $Nl ; $nl ++ ) { $from = $nl ; # select at random from list ALOOP: for ( $a = 1 ; $a <= 1000 ; $a ++ ) { $valid = 1 ; $q = int ( rand($#qs+1) ) ; $to = $qs[$q] ; if ( !$real) { print STDOUT "$nl($#qs): new sample is $q , $to \n" ; } # need to check if this clashes with an existing permutation. BLOOP: for ( $b = 1 , $testn = $nnn - $Nl ; $b <= $nrandperms ; $b++ , $testn -= $Nl ) { if ( $forbid[$testn] == $to ) { # print STDERR "$nl: [$q] $to X\n" ; if (!$real){ print STDOUT "sampling from $#qs+1 : " , join ( ' ' ,@qs ) , "\n" ; print STDOUT " selecting " , int ( rand($#qs+1) ) , int ( rand($#qs+1) ) , int ( rand($#qs+1) ) , int ( rand($#qs+1) ) , "\n" ; } $valid = 0; last BLOOP ; } } if ( $valid || ( $nl == $Nl ) ) { # last ALOOP ; } # } $to = splice ( @qs , $q , 1 ) ; # delete selection from the list. if ( !$valid ) { print STDERR "accepted invalid $nl: $to\n" ; $failed ++ ; } $forbid[$nnn] = $to ; $nnn ++ ; $p[$from] = $to ; $fto[$to] ++ ; # print STDERR $nl , "->" , $to , " " ; if(!$real){ print STDOUT "." ; } else { print STDERR "." ; } } if(!$real){ print STDOUT "\n" ; } else{ print STDERR "\n" ; } &checkperm(); # put in this perm &permadd(); $nrandperms ++ ; } print STDERR "failures: $failed. Writing code.\n" ; # append zeros to short lines for ( $n = 1 ; $n <= $N ; $n ++ ) { while ( $numnlist[$n] < $t ) { $nlist[$n] .= "0\t" ; $numnlist[$n] ++ ; } } for ( $m = 1 ; $m <= $M ; $m ++ ) { while ( $nummlist[$m] < $l ) { $mlist[$m] .= "0\t" ; $nummlist[$m] ++ ; } } 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" ; $numnlist[$n] ++ ; $nummlist[$m] ++ ; } } # 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] ; }