#!/usr/local/bin/perl # reads an alist and finds codewords of the matrix # with low weight. # # Note this does not find all codewords. If the word is a bushy one, # it can't be picked up. (this problem probably only kicks in # for large weight words, however, in my interests) # # wordfind0.p uses a plain "." concatenation of the syndrome # wordfind.p uses a fancier ascii coding? # # it seemed to not work on rubik/alistperm # try wordfind0.p instead $lazy = 0 ; # whether syndrome representation is verbose (lazy=1) or compact (0) $w=4 ; # weight to aim for $verbose = 0 ; $logintermediates = 1 ; # whether to hash the syndromes of all paths, not just full length $evenonly = 0 ; # only log the syndromes of paths of even length (appropriate if all columns have odd weight) # not implemented because I hate not having a % operator $logends = 1; # whether to hash the endpoints of paths too, so as to detect # cycles (NOT DONE YET) $really=0; eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; if($really==0){ print STDERR "Sure?\n" ; print STDERR "try wordfind0.p instead?\n" ; exit(0); } # see also A 2 dat . p sub bynumber { $a <=> $b; } #for ( $weight = 0 ; $weight <= $w+1 ; $weight ++ ) { $cws{0} = 0 ; # cws counts the codewords by weight, with [0] being the total $maxweight = 0 ; ##################################################3 # Read in the code ################################################## $_ = <>; ($L,$M) = split ; print STDOUT "N,M = $L , $M\n" ; $_ = <>; ($maxnumnlist,$maxnummlist) = split ; $_ = <>; @numnlist = split ; $_ = <>; @nummlist = split ; for ( $l = 1 ; $l <= $L ; $l ++ ) { $_ = <>; $nlist[$l] = $_ ; if ($verbose >= 3 ) { @y = split ( '\s+' , $nlist[$l] ) ; for ( $u = 0 ; $u <= $#y ; $u ++ ){ print $y[$u] , "\t" ; } print "-\n" ; } } # for ( $l = 1 ; $l <= $M ; $l ++ ) { $_ = <>; $mlist[$l] = $_ ; if ($verbose >= 3 || ($l == $M) ) { @y = split ( '\s+' , $mlist[$l] ) ; for ( $u = 0 ; $u <= $#y ; $u ++ ){ print $y[$u] , "\t" ; } print "-\n" ; } } # ######################################33 lists are read in. print "check last line if you want....\n" ; if ((!$lazy) && ($M > 220)) { print STDERR "WARNING!!!!! M = $M > 220 ; untested code. switch lazy=1?\n" ; # exit(0); } if ((!$lazy) && $verbose) { foreach $t ( 0 .. $M ) { print "$t -> " ; $tmp = chrmap($t) ; print "$tmp\n" ; } } # $length = $w/2 ; # length means the number of n-columns that are added if ( $length * 2 < $w ) { $length ++ ; } print "making paths of length up to $length\n" ; # to give the syndrome. ##################################################3 # recursively make paths ################################################## $pathno = 0 ; $collisions = 0 ; for ( $n = 1 ; $n <= $L ; $n ++ ) { $syndrome=" " ; $x="." ; # x is the current list of codeword bits $pathdetails = "" ; &makepathsfrom( $x , $n , $length , $syndrome , $pathdetails , 0 ) ; } sub makepathsfrom { # # x : codeword so far # n : which node to expand # length : how much further to keep going # syndrome : what the syndrome is # pathdetails : a redundant thing useful for debugging # pathlength : length done so far ( local($xl) , local($n) , local($length) , local($syndrome) , local ($details) , local ($pathlength) ) = @_ ; local(@y) = split ( '\s+' , $nlist[$n] ) ; local($nlistlength) = $numnlist[$n-1] ; local($wordstring)="$xl$n."; local($detstring)="$details.$n"; $pathlength ++ ; # Keep the syndrome up to date. append thus: (except delete duplicates) # $syndrome = "$syndrome $nlist[$n]" ; for ( local($u) = 1 ; $u <= $nlistlength ; $u ++ ) { local($m) = $y[$u-1] ; if ( ! ( $syndrome =~ s/\s$m\s/ / ) ) { $syndrome = "$syndrome$m " ; } # else { print "ping!\n" ; } } print "-> syndrome = $syndrome\n" if ( $verbose >= 3) ; # # final actions when a path is complete: # if ( $length == 1 || $logintermediates ) { # should check for even-ness too. # sort the syndrome, and register it $pathno ++ ; local($sortedsyndrome) = &syndrome_rep($syndrome) ; local($sortedpath) = join('.',sort(split('\.',$wordstring))) ; if ( $verbose >= 1 ) { print "$pathno: " ; print "$wordstring " ; print "$detstring" ; if($verbose>=2){ print "\nsorted word: $sortedpath\n" ; print "syndrome: $syndrome\n" ; print "sorted z:" ; } print " $sortedsyndrome\n" ; } if ( exists( $beenhere{$sortedsyndrome} ) ) { # we may have just found two equivalent paths, so let's check. # (eg path and reversed path) if ( !($beenhere{$sortedsyndrome} eq $sortedpath)) { # we have found a codeword. let's check if it is short and original. $cw=$beenhere{$sortedsyndrome}.$sortedpath."." ; $cw =~ s/\./::/g ; while ( $cw =~ /(:\d+:)(.*)(\1)/ ) { # spot repetitions $remove= $1 ; print "cleaning up $cw ($remove)\n" if ($verbose >=3 ) ; $count = ( $cw =~ s/$remove//g ) ; if ( int ( $count / 2 ) * 2 != $count ) { $cw=$cw.$remove ; } } $cw =~ s/::/./g ; $cw =~ s/^\.// ; # remove lead and trail "." $cw =~ s/\.\Z// ; $sortedcw = join('.',sort bynumber (split('\.',$cw))) ; if ( ! exists( $iscw{$sortedcw} ) ) { print "CODEWORD $sortedcw\n" ; $iscw{$sortedcw} = 1 ; $cws{0} ++ ; $weight = ( $cw =~ s/\.//g ) + 1 ; print "(weight = $weight)\n" if ($verbose>3) ; if (!exists($cws{$weight})) { $cws{$weight}=0; } $cws{$weight} ++ ; if ( $weight > $maxweight ) { $maxweight = $weight ; } } print "COLLISION at $sortedsyndrome between ", $beenhere{$sortedsyndrome}, " and $sortedpath\n" if ( $verbose ); $collisions ++ ; } else { print "unimportant between ", $beenhere{$sortedsyndrome}, " and $sortedpath at $sortedsyndrome \n" if ( $verbose >= 2 ) ; } } else { $beenhere{$sortedsyndrome}=$sortedpath ; } } # # actions involved in extending a path # if ( $length > 1 ) { for ( local($u) = 1 ; $u <= $nlistlength ; $u ++ ) { local($m) = $y[$u-1] ; print $m , "\t" if ($verbose >= 3) ; local(@x) = split ( '\s+' , $mlist[$m] ) ; for ( local($l) = 1 ; $l <= $nummlist[$m-1] ; $l ++ ) { local($nn) = $x[$l-1] ; # need to check if this $nn is already on the list. if ( ! ( $wordstring =~ /\.$nn\./ )) { &makepathsfrom( $wordstring , $nn , $length-1 , $syndrome , $detstring."($m)" , $pathlength ) ; } } } } return ; } $tr = $maxnummlist ; $t = $maxnumnlist ; $tot = 0 ; # total number of paths if all are logged. if ( $length >= 1 ) { $E = $L ; $tot += $E ; } if ( $length >= 2 ) { $E = $L * $t * ($tr-1) ; $tot += $E ; } for ( $l = 3 ; $l <= $length ; $l ++ ) { $E = $E * ( ($tr-1) * ($t-1) ) ; $tot += $E ; } if ( $logintermediates ) { $E = $tot ; } print "made $pathno paths (expected about $E). $collisions Collisions\n" ; print "Codewords found: (searched [not complete] up to weight $w)\n" ; for ( $weight = 0 ; $weight <= $w ; $weight ++ ) { print "$weight\t" ; } print "\n" ; $cumulative = 0 ; for ( $weight = 0 ; $weight <= $w ; $weight ++ ) { if (!exists($cws{$weight})) { $cws{$weight} = "0" ; } print "$cws{$weight}" ; if ($weight > 0 ) { $cumulative += $cws{$weight} ; $cum{$weight} = $cumulative ; } else { print "cws:" ; } print "\t" ; } print "\n" ; for ( $weight = 0 ; $weight <= $w ; $weight ++ ) { if ($weight > 0 ) { print "$cum{$weight}\t" ; } else { print "cums:\t" ; } } print "\n" ; sub syndrome_rep { local( $syndrome ) = @_ ; local(@tmp) = sort(split(' ',$syndrome)) ; if ( !$lazy ) { foreach $t ( 0 .. $#tmp ) { # print "$tmp[$t] -> " ; $tmp[$t] = &chrmap(($tmp[$t]-1)) ; # print "$tmp[$t]\n" ; } } if ( $lazy ) { local($ans) = join(' ',@tmp); } else { local($ans) = join('',@tmp) ; } return ($ans) ; } sub chrmap { local($t) = @_ ; if ( $M < 220 ) { # surely this should be "<"? return &onechrmap($t) ; } else { local($n) = $t / 220 ; local($d) = $t % 220 ; return &onechrmap($n).&onechrmap($d) ; } } sub onechrmap { local($t) = @_ ; return chr(32 + $t) ; }