#!/usr/local/bin/perl -w # # computes expected weight enumerator function using the # MacKay ensemble in which t bits are flipped in each column. # (not necessarily distinct) # # enumeratore.p differs from enumerator in that it does # exact evaluation of choose # # enumeratore2.p uses the 2nd ensemble in which 1 bit is flipped per # column of length M/t, and there are M/t such columns # # usage: # enumeratore.p M=222 N=1998 t=3 | gnuplot $wmin=1 ; # set to 1 or zero; defines smallest weight considered # when finding A(w) $t=3; $N=1998; $M=222; $verbose = 1 ; $o="" ; $write=1; $logs=0 ; # whether to offer a plot command using log(A) or A. eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; if ($o eq "" ) { $M2 = $M + 1 - $t ; $o="Am2/$N.$M2.$t" ; } if ( !(int($M/$t) == $M/$t ) ) { print STDERR "$M not divisible by $t\n" ; exit(0); } $trueM = $M ; $M = $M/$t ; # new meaning for M if ( $write ) { open (O , "> $o" ) ; print STDERR "Computing eigenvalues for random walk\n" ; $comb = 1 ; foreach $j (0 .. int($M/2)+1 ) { $l[$j] = 1.0 - 2.0 * $j / $M ; if ( $j > 0 ) { $comb = $comb * ( $M + 1 - $j ) / $j ; } $c[$j] = $comb ; printf STDERR "%4d\t%10f\t%10g\t%10g\t%10g\n" , $j , $l[$j] , $c[$j] , log($c[$j])/log(2.0) , &lchoose($M,$j) if ($verbose>2) ; if ( $c[$j] < 0 ) { print STDERR "overflow\n" ; exit(0);} } $twoM = 2**(-$M) ; print STDERR "Writing to $o\n" ; print O "# w p00 p00^t log(A(w))=log( (N:w) p00^t ) logArandom=log(N:w)2^-M log(N:w) A(w)\n" ; $lchooseNw = 0.0 ; foreach $w ( $wmin .. $N ) { if ( $w > 0 ) { $lchooseNw += log ( ($N + 1 - $w ) *1.0/ $w ) / log(2.0) ; } $r = $w ; $poo = 0.0 ; if ( !($r % 2) ) { foreach $j (0..$M/2.0) { $factor = ( $j == $M/2.0 ) ? 1.0 : 2.0 ; $poo += $c[$j] * ( $l[$j]**$r ) * $factor ; } # p_00 = poo * 2^{-M} $poo = $poo * $twoM ; $lArandom = $lchooseNw - $trueM ; $lA = $lchooseNw + $t * log( $poo )/log(2.0) ; printf O "%4d\t%10.5g\t%10.5g\t%10.5g\t%10.5g\t%10.5g\t%10.5g\t%10.5g\n" , $w , $poo , $poo**$t , $lA , $lArandom , $lArandom + $trueM , 2.0**$lA , 2.0**$lArandom ; } else { printf O "# %4d p_00($r) = 0\n" if ( $verbose > 2 ) ; } } } print "# gnuplot\n set xrange[0:$N]\n" ; if ( $logs ) { print "plot '$o' u 1:4 t 'A(w;$t)' w l \ " ; print ", '$o' u 1:5 t 'Arand(w)' w l \ " ; print ", 0 t '' w l " ; } else { print "set logs y\n" ; print "plot '$o' u 1:7 t 'A(w;$t)' w l \ " ; print ", 1 t '' w l 2 " ; } print "\n" ; sub lchoose { # returns log_2 (N choose w), approximately local ($N,$w) = @_ ; return ( $N * &H2($w*1.0/$N) - 0.5 * log(1.0*$N)/log(2.0) ) ; } sub H2 { # binary entropy # usage ( $f ) local ( $f ) = @_ ; if ( $f < 0.0 || $f > 1.0 ) { print STDERR "ERROR in H2 ($f)\n" ; return 0.0 ; } elsif ( $f == 0.0 || $f == 1.0 ) { return 0.0 ; } else { local ( $ans ) = - ( $f * log ( $f ) + (1.0 - $f) * log (1.0 - $f ) ) ; return $ans / log(2.0) ; } }