#!/usr/local/bin/perl -w # productA.p writes an alist matrix # corresponding to a product code. # # used in hawaii project to make product codes from gallager codes, and in tanner project # to make product codes for hamming (tpc) # # usage (in code/hawaii/product) # productA.p N0=4 M0=2 R=2 < 4.2 > 4.2.R2.A # A2dat.p < 4.2.R2.A # # see also A2dat.p # $seed=123 ; $N = 0 ; $M = 0 ; $R = 2 ; # number of repetitions $N0 = 21 ; # size of constituent code. $M0 = 21 ; # number of constraints given by constituent code. $verbose = 0 ; $randomorder = 0 ; # if this is 1, then each list of points n addressed by the first # point in each face[r] with r>=2 is randomly ordered for all their checks. eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; # the final block length $N = $N0 ** $R ; # the number of constraints that will be written # is the number of constraints per tiny block # times the number of tiny blocks $facesize = ($N0 ** ($R-1)) ; $M = $R * $M0 * $facesize ; if ( $randomorder ) { srand ( $seed ) ; } print STDERR "===== productA.p ===== (c) DJCM Nov 1998 =====\n" ; print STDERR "N0 = $N0, M0 = $M0, Reps = $R => facesize = $facesize, N = $N, M = $M\n" ; # make the numbers on the R faces of the cube. $face[1] = join( ' ' , 1..$facesize ) ; # which is 1..N0 here $dn[1] = $facesize ; if ( $R == 2 ) { # for the first face, the lead numbers are 1,2,3...N0**(R-1) by defn. $face[2] = "" ; foreach $n ( 1..$N0 ) { $num = ($n-1) * $N0 + 1 ; $face[2] .= "$num " ; } $dn[2] = 1 ; } elsif ( $R == 3 ) { $face[2] = "" ; foreach $n ( 1..$N0 ) { foreach $m ( 1..$N0 ) { $num = ($n-1) * $N0**2 + $m ; $face[2] .= "$num " ; } } $dn[2] = $N0 ; $face[3] = "" ; foreach $n ( 1..$N0 ) { foreach $m ( 1..$N0 ) { $num = ($n-1) * $N0**2 + ($m-1) * $N0 + 1 ; $face[3] .= "$num " ; } } $dn[3] = 1 ; } if ( $verbose > 4 ) { print STDERR "The $R faces of the codeword cube are labelled:\n" ; foreach $r ( 1..$R ) { print STDERR "$r (increment by $dn[$r]):\n"; print STDERR $face[$r]."\n" ; } } # make ordered lists of points for each of the checked sets foreach $r ( 1..$R ) { @f = split ( '\s+' , $face[$r] ) ; foreach $nnn ( @f ) { $c = 0 ; $n = $nnn ; if ( $randomorder && ($r > 1 ) ) { @perm = &permutation ( $N0-1 , 0 ) ; } foreach $m ( 1 .. $N0 ) { if ( $randomorder && ($r > 1 ) ) { $getn{$r,$nnn,$perm[$c]} = $n ; } else { $getn{$r,$nnn,$c} = $n ; } $n += $dn[$r] ; $c ++ ; } print STDERR " face $r, start $nnn -> " if ($verbose>=2) ; foreach $c ( 0 .. $N0-1 ) { print STDERR "$getn{$r,$nnn,$c} " if ($verbose>=2) ; } print STDERR "\n" if ($verbose>=2) ; } } $m = 0 ; while(<>) { @a = split ; if ( $#a > 1 ) { # was > 2 foreach $r ( 1..$R ) { @f = split ( '\s+' , $face[$r] ) ; foreach $nnn ( @f ) { $m ++ ; if ( $verbose > 3 ) { print STDERR "constraint $m: going up from $nnn in steps of $dn[$r]\n" ; } # $n = $nnn ; $c = 0 ; foreach $b ( @a ) { if ( $b ) { $n = $getn{$r,$nnn,$c} ; &mnadd($m,$n); if ( $verbose > 3 ) { print STDERR ":$n:" ; } } # $n += $dn[$r] ; $c ++ ; } if ( $verbose > 3 ) { print STDERR "\n" ; } } } } else { print "ignoring blank line" ; } } $Mwritten = $m ; print STDERR "done N = $N ; Mwritten = $m (expected $M)\n" ; $M = $Mwritten ; if ( 1 ) { $head = "$N $M\n" ; print STDERR "Writing code.\n" ; # find $t (max) and $tr (max) $t = 0 ; $tr = 0 ; for ( $n = 1 ; $n <= $N ; $n ++ ) { if ( $numnlist[$n] > $t ) { $t = $numnlist[$n] ; } } for ( $m = 1 ; $m <= $M ; $m ++ ) { if ( $nummlist[$m] > $tr ) { $tr = $nummlist[$m] ; } } $head .= "$t $tr\n" ; # append zeros to short lines for ( $n = 1 ; $n <= $N ; $n ++ ) { $thisn = $numnlist[$n] ; $head .= "$thisn " ; while ( $thisn < $t ) { $nlist[$n] .= "0\t" ; $thisn ++ ; } } $head .= "\n" ; for ( $m = 1 ; $m <= $M ; $m ++ ) { $thism = $nummlist[$m] ; $head .= "$thism " ; while ( $thism < $tr ) { $mlist[$m] .= "0\t" ; $thism ++ ; } } $head .= "\n" ; print $head ; # 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] ; } } sub mnadd { local($m,$n)=@_; # put m and n on each others lists $nlist[$n] .= $m."\t" ; $mlist[$m] .= $n."\t" ; $numnlist[$n] ++ ; $nummlist[$m] ++ ; } # usage @perm = &permutation ( $Q , 0 ) ; sub permutation { # returns a permn of 0..Q offset by offset eg 0/1 local ( $Q , $offset ) = @_ ; for ( local($q) = 0 ; $q <= $Q ; $q ++ ) { $qs[$q] = $q + $offset ; } for ( local($n) = 0 ; $n <= $Q ; $n ++ ) { # $from = $n ; $q = int ( rand($#qs+1) ) ; $to[$n] = splice ( @qs , $q , 1 ) ; print "to[$n] = $to[$n]\n" if ( $verbose >=3 ) ; } print "Here is a perm $Q:\n" if ( $verbose >=3 ) ; print join ( ':' , @to ) , "\n" if ( $verbose >=3 ) ; return ( @to ) ; }