#!/usr/local/bin/perl -w # Make Arithmetic coding piccy. # ac.p > ac/ac.tex # ac.p sub=1 unit=40 > ac/ac_sub.tex # ac.p only_show_data=1 > ac/ac_data.tex # # to make one with f=0.1 fixed... # ac.p abbang=0 checkheight=1 > ac/ac0.1.tex $com = "%"; # $outfile="ac.tex"; $in="in"; $unit="5.75"; $width="3.4"; $sub=0; $subbot=0.59; $subtop=0.63; $thebot=0.0; $thetop=1.0; $only_show_data = 0 ; $di = 2 ; $dj = 2 ; $dk = 2 ; $dm = 1 ; $dn = 3 ; $do = 2 ; $dq = 2 ; $dr = 2 ; $abbang = 1 ; # produces the old original picture # the alternative is an a-b mix with ratio defined here $aweight=5; # 5:1 $checkheight = 0 ; # whether to see if a space is too small to write in $heightthresh = 0.04 ; # defn of too small. # How to override min and max values with command line eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; $global[1] = 1 ; $global[2] = 1 ; $global[3] = 1 ; if ( $sub ) { $thebot=$subbot; $thetop=$subtop; } $theht = $thetop - $thebot ; $othetop = 1.0 - $thetop ; $heightthresh *= $theht ; # rescale definition of too small # make plot have height 1 and width $wun $wun=$width/$unit ; $wunh = $wun/2; print "\\setlength{\\unitlength}{$unit$in}\n" ; print "\\begin{picture}($wun,$theht)(-$wunh,$othetop)\n"; print "\\thinlines\n"; if ( $abbang ) { %th=(1,'a',2,'b',3,'!'); $I=3; $p[$I] = 0.15 ; $stop=1; $adapt = 1 ; $just="r"; } else { %th=(1,'a',2,'b'); $I = 2 ; $stop=0; $adapt=0; $just="r"; $global[1] = $aweight ; $global[2] = 1 ; } if ( $sub ) { # sub-picture $left[1] = -0.5 ; $right[1] = 0.0 ; $left[2] = -0.49 ; $right[2] = 0.0 ; $left[3] = -0.48 ; $right[3] = 0.0 ; $left[4] = -0.42 ; $right[4] = 0.0 ; $left[5] = -0.28 ; $right[5] = 0.0 ; $left[6] = -0.10 ; $right[6] = 0.0 ; $do_is = 0 ; $do_js = 0 ; $do_ks = 1 ; $do_ms = 1 ; $do_n = 1 ; $do_ns = 1 ; $do_o = 1 ; $do_os = 0 ; $do_q = 0; $eps=0.01*5.75/$unit; if ( $abbang ) { $istop=0; $jstop=0; $kstop=0 ;$mstop=0 ; $nstop=0 ; $ostop=1; $qstop=1; # whether ! is not shown } else { $istop=0; $jstop=0; $kstop=0 ;$mstop=0 ; $nstop=0 ; $ostop=0 ; $qstop=0; } } else { $left[1] = -0.5 ; $right[1] = 0.0 ; $left[2] = -0.45 ; $right[2] = 0.0 ; $left[3] = -0.35 ; $right[3] = 0.0 ; $left[4] = -0.23 ; $right[4] = 0.0 ; $left[5] = -0.10 ; $right[5] = 0.0 ; $left[6] = -0.04 ; $right[6] = 0.0 ; $eps=0.015; if ( $abbang ) { $do_is = 1 ; $do_js = 1 ; $do_ks = 1 ; $do_ms = 1 ; $do_n = 1 ; $do_ns = 0 ; $do_o = 0 ; $istop=0; $jstop=0; $kstop=0 ;$mstop=($only_show_data) ? 0 : 1 ; $nstop=1 ; $ostop=1; $qstop=1; # whether ! is not shown } else { $do_is = 1 ; $do_js = 1 ; $do_ks = 1 ; $do_ms = 1 ; $do_n = 1 ; $do_ns = 0 ; $do_o = 1 ; $do_q = 1 ; $do_qs = 0 ; $do_r = 1 ; $istop=0; $jstop=0; $kstop=0 ;$mstop=0 ; $nstop=0 ; $ostop=0 ; $qstop=1; } } &doit () ; %th=(1,'0',2,'1'); $global[1] = 1 ; $global[2] = 1 ; $global[3] = 1 ; $only_show_data = 0 ; $I=2; $stop=0; $adapt = 0 ; $just = "l" ; $istop=0; $jstop=0; $kstop=0 ;$mstop=0 ; $nstop=0 ; $ostop=0; $qstop=0; $rstop=0; $sstop=0; if ( $sub ) { $right[1] = 0.5 ; $left[1] = 0.01 ; $right[2] = 0.49 ; $left[2] = 0.01 ; $right[3] = 0.49 ; $left[3] = 0.01 ; $right[4] = 0.45 ; $left[4] = 0.01 ; $right[5] = 0.42 ; $left[5] = 0.01 ; $right[6] = 0.37 ; $left[6] = 0.01 ; $right[7] = 0.3 ; $left[7] = 0.01 ; $right[8] = 0.21 ; $left[8] = 0.01 ; $right[9] = 0.03 ; $left[9] = 0.01 ; $do_is = 0 ; $do_js = 0 ; $do_ks = 0 ; $do_ms = 0 ; $do_n = 1 ; $do_ns = 1 ; $do_o = 1 ; $do_os = 0 ; $do_q = 1 ; $do_qs = 0 ; $do_r = 1 ; $do_rs = 1 ; $do_s = 1 ; $do_ss = 0 ; } else { $right[1] = 0.5 ; $left[1] = 0.01 ; $right[2] = 0.45 ; $left[2] = 0.01 ; $right[3] = 0.38 ; $left[3] = 0.01 ; $right[4] = 0.28 ; $left[4] = 0.01 ; $right[5] = 0.15 ; $left[5] = 0.01 ; $right[6] = 0.04 ; $left[6] = 0.01 ; $right[7] = 0.02 ; $left[7] = 0.01 ; $do_is = 1 ; $do_js = 1 ; $do_ks = 1 ; $do_ms = 1 ; $do_n = 1 ; $do_ns = 1 ; $do_o = 1 ; $do_os = 0 ; $do_q = 1 ; $do_qs = 0 ; } &doit () ; printf ( "\\end{picture}\n" ) ; exit(0); sub doit { $mid[$I] = 1.0 - $p[$I]/2.0 ; $cum[$I] = 1.0 ; for ( $ii = 1 ; $ii < $I ; $ii ++ ) { $c[$ii] = 1 ; } &boogie () ; $l = 1 ; $bot[$l]=0; $height[$l]=1.0; &line( 0 ) ; for ( $i=1 ; $i<=$I ; $i++ ) { $is=$th{$i} ; if ( $only_show_data ) { printf STDERR "P ( %s ) = %6.3f ; " , $is , $p[$i] ; print STDERR "\n" if ( $i == $I ) ; } &symbol( $is , $i ) if ( $do_is && ($i <= $I-$istop) ) ; &line ( $i ) ; } for ( $i=1 ; $i<= $I-$stop ; $i++ ) { next if ( $only_show_data && ( $i != $di ) ) ; $is=$th{$i} ; # SECOND SYMBOL &boogie () ; $l++ ; $bot[$l] = $cum[$i-1] ; $height[$l] = $p[$i] ; $c[$i] ++ ; &boogie () ; for ( $j=1 ; $j <=$I ; $j ++ ) { $js=$is.$th{$j} ; if ( $only_show_data ) { printf STDERR "P ( %s | %s ) = %6.3f " , $js , $is , $p[$j] ; print STDERR "\n" if ( $j == $I ) ; } &symbol( $js , $j ) if ( $do_js && ($j <= $I-$jstop) ) ; &line ( $j ) if ( $j < $I ) ; } for ( $j=1 ; $j <= $I-$stop ; $j ++ ) { next if ( $only_show_data && ( $j != $dj ) ) ; $js=$is.$th{$j} ; # THIRD SYMBOL &boogie () ; $l++ ; $bot[$l] = $bot[$l-1] + $cum[$j-1]*$height[$l-1] ; $height[$l] = $p[$j] * $height[$l-1] ; $c[$j] ++ ; &boogie () ; for ( $k=1 ; $k <=$I ; $k ++ ) { $ks=$js.$th{$k} ; if ( $only_show_data ) { printf STDERR "P ( %s | %s ) = %6.3f " , $ks , $js , $p[$k] ; print STDERR "\n" if ( $k == $I ) ; } &symbol( $ks , $k ) if ( $do_ks && ($k <= $I-$kstop) ) ; &line ( $k ) if ( $k < $I ) ; } for ( $k=1 ; $k <= $I-$stop ; $k ++ ) { next if ( $only_show_data && ( $k != $dk ) ) ; $ks=$js.$th{$k} ; # FOURTH SYMBOL &boogie () ; $l++ ; $bot[$l] = $bot[$l-1] + $cum[$k-1]*$height[$l-1] ; $height[$l] = $p[$k] * $height[$l-1] ; $c[$k] ++ ; # &boogie () ; for ( $m=1 ; $m <=$I ; $m ++ ) { $ms=$ks.$th{$m} ; if ( $only_show_data ) { printf STDERR "P ( %s | %s ) = %6.3f " , $ms , $ks , $p[$m] ; print STDERR "\n" if ( $m == $I ) ; } &symbol( $ms , $m ) if ( $do_ms && ($m <= $I-$mstop) ) ; &line ( $m ) if ( $m <$I) ; } if ( $do_n ) { for ( $m=1 ; $m <=$I-$stop ; $m ++ ) { next if ( $only_show_data && ( $m != $dm ) ) ; $ms=$ks.$th{$m} ; # FIFTH &boogie () ; $l++ ; $bot[$l] = $bot[$l-1] + $cum[$m-1]*$height[$l-1] ; $height[$l] = $p[$m] * $height[$l-1] ; $c[$m] ++ ; # &boogie () ; for ( $n=1 ; $n <=$I ; $n ++ ) { $ns=$ms.$th{$n} ; if ( $only_show_data ) { printf STDERR "P ( %s | %s ) = %6.3f " , $ns , $ms , $p[$n] ; print STDERR "\n" if ( $n == $I ) ; } &symbol( $ns , $n ) if ( $do_ns && ($n <= $I-$nstop) ) ; &line ( $n ) if ( $n < $I ) ; } if ( $do_o ) { for ( $n=1 ; $n <=$I-$stop ; $n ++ ) { next if ( $only_show_data && ( $n != $dn ) ) ; $ns=$ms.$th{$n} ; # SIXTH &boogie () ; $l++ ; $bot[$l] = $bot[$l-1] + $cum[$n-1]*$height[$l-1] ; $height[$l] = $p[$n] * $height[$l-1] ; $c[$n] ++ ; # &boogie () ; for ( $o=1 ; $o <=$I ; $o ++ ) { $os=$ns.$th{$o} ; &symbol( $os , $o ) if ( $do_os && ($o <= $I-$ostop) ) ; &line ( $o ) if ( $o < $I ) ; } if ( $do_q ) { for ( $o=1 ; $o <=$I-$stop ; $o ++ ) { next if ( $only_show_data && ( $o != $do ) ) ; $os=$ns.$th{$o} ; # SEVENTH &boogie () ; $l++ ; $bot[$l] = $bot[$l-1] + $cum[$o-1]*$height[$l-1] ; $height[$l] = $p[$o] * $height[$l-1] ; $c[$o] ++ ; # &boogie () ; for ( $q=1 ; $q <=$I ; $q ++ ) { $qs=$os.$th{$q} ; &symbol( $qs , $q ) if ( $do_qs && ($q <= $I-$qstop) ) ; &line ( $q ) if ( $q < $I ) ; } if ( $do_r ) { for ( $q=1 ; $q <=$I-$stop ; $q ++ ) { next if ( $only_show_data && ( $q != $dq ) ) ; $qs=$os.$th{$q} ; # EIGHT &boogie () ; $l++ ; $bot[$l] = $bot[$l-1] + $cum[$q-1]*$height[$l-1] ; $height[$l] = $p[$q] * $height[$l-1] ; $c[$q] ++ ; # &boogie () ; for ( $r=1 ; $r <=$I ; $r ++ ) { $rs=$qs.$th{$r} ; &symbol( $rs , $r ) if ( $do_rs && ($r <= $I-$rstop) ) ; &line ( $r ) if ( $r < $I ) ; } if ( $do_s ) { for ( $r=1 ; $r <=$I-$stop ; $r ++ ) { next if ( $only_show_data && ( $r != $dr ) ) ; $rs=$qs.$th{$r} ; # NINE &boogie () ; $l++ ; $bot[$l] = $bot[$l-1] + $cum[$r-1]*$height[$l-1] ; $height[$l] = $p[$r] * $height[$l-1] ; $c[$r] ++ ; # &boogie () ; for ( $s=1 ; $s <=$I ; $s ++ ) { $ss=$rs.$th{$s} ; &symbol( $ss , $s ) if ( $do_ss && ($s <= $I-$sstop) ) ; &line ( $s ) if ( $s < $I ) ; } $c[$r] -- ; $l -- ; } } $c[$q] -- ; $l -- ; } } $c[$o] -- ; $l -- ; } } $c[$n] -- ; $l -- ; } } $c[$m] -- ; $l -- ; } } $c[$k] -- ; $l -- ; } $c[$j] -- ; $l -- ; } $c[$i] -- ; $l-- ; } } sub symbol { local ( $sym , $ii ) = @_ ; # put symbol in middle of interval if ( !$checkheight || ($height[$l] > $heightthresh ) ) { $theheight = $mid[$ii]*$height[$l]+$bot[$l] ; if ( !$sub || ( $theheight <= $thetop && $theheight >= $thebot ) ) { printf "%s %s at %9.4f,%9.4f\n" , $com , $sym , $left[$l+1] , $mid[$ii]*$height[$l]+$bot[$l]; printf "\\put(%9.4f,%9.4f){\\makebox(0,0)[%s]{\$%s\$}}\n" , (($just eq "r") ? $left[$l+1]*$wun-$eps : $right[$l+1]*$wun+$eps ) , 1.0 - ($theheight) ,$just, $sym ; } } } sub line { local ( $ii ) = @_ ; # put line at end of interval $theheight = $cum[$ii]*$height[$l]+$bot[$l] ; if ( !$sub || ( $theheight <= $thetop && $theheight >= $thebot ) ) { printf "%s line %9.4f" , $com, $cum[$ii]*$height[$l]+$bot[$l] ; printf " from %9.4f to %9.4f \n" ,$left[$l], $right[$l] ; printf "\\put(%9.4f,%9.4f){\\line(1,0){%9.4f}}\n" , $left[$l]*$wun, 1.0 - ( $cum[$ii]*$height[$l]+$bot[$l]), ($right[$l] - $left[$l] )*$wun; } } sub boogie { if ( $adapt ) { for ( $ii = 1 , $c[0] = 0 ; $ii < $I ; $ii ++ ) { $c[0] += $c[$ii] ; } $c[0] /= ( 1.0 - $p[$I] ) ; # for ( $ii = 1 , $cum[0] = 0 ; $ii < $I ; $ii ++ ) { $p[$ii] = $c[$ii] / $c[0]; $cum[$ii] = $cum[$ii-1] + $p[$ii] ; $mid[$ii] = $cum[$ii-1] + $p[$ii] * 0.5 ; # print STDERR "cum[$ii] = $cum[$ii], " ; } # print STDERR "\n" ; } else { for ( $ii = 1 , $c[0] = 0 ; $ii <= $I ; $ii ++ ) { $c[0] += ($c[$ii]=$global[$ii]) ; } # was 1 for ( $ii = 1 , $cum[0] = 0 ; $ii <= $I ; $ii ++ ) { $p[$ii] = $c[$ii] / $c[0]; $cum[$ii] = $cum[$ii-1] + $p[$ii] ; $mid[$ii] = $cum[$ii-1] + $p[$ii] * 0.5 ; } } }