#!/usr/local/bin/perl -w # reads in a channel and writes out an extended channel # # # example usage ( in itp/extended ) # # ~/bin/extended.p out=9317.2 N=2 < c/9317 # ~/bin/extended.p out=9317.5 N=5 < c/9317 # ~/bin/extended.p out=z.5 N=5 < c/z # ~/bin/extended.p out=bsc.5 N=5 < c/bsc # # SEE ALSO # itp/extended/RUNME # $displace = 1 ; # whether to turn 1,2,3,4 into 0,1,2,3 $erasure = 1 ; # whether to use erasure symbol in ternary output channels $xoffset=20 ; # offset of labels $yoffset=0 ; # offset of labels $fontsize=10; # label size $labels=1 ; # whether to add labels $h=8 ; # box size $factor = 1.05 ; # factor by which hinton max number is scaled up $v = 2 ; $N = 2 ; # number of repetitions $out = "c" ; $prefix = "ec/" ; $psprefix = "ps/" ; $texprefix = "tex/" ; $labelprefix = "labels/" ; $tex = 0 ; eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; # read in a channel in the format # 0.9 0.5 # 0.1 0.45 # 0.0 0.05 $ofile = $prefix.$out ; $texfile = $texprefix.$out.".tex" ; $tstring = "" ; $Ts = "" ; $psfile = $psprefix.$out.".ps" ; $labelfile = $labelprefix.$out ; open ( O , "> $ofile" ) ; @a = <> ; $J = $#a ; # number of outputs $ j = 0 ; foreach $aa ( @a ) { $j ++ ; $i = 0 ; $aa =~ s/^\s+// ; @b = split ( '\s+', $aa ) ; if ( $j == 1 ) { $I = $#b ; } elsif ( $#b != $I ) { print STDERR "Warning $#b != $I , row $j\n" ; } foreach $bb ( @b ) { $i ++ ; $q{$j*100+$i} = $bb ; } } $I ++ ; $J ++ ; foreach $i ( 1..$I ) { $sum[$i] = 0.0 ; foreach $j ( 1..$J ) { $sum[$i] += $q{$j*100+$i} ; } if ( $v > 2 ) { print "Input $i " ; print "sum = $sum[$i]\n" ; } } foreach $i ( 1..$I ) { print "Input $i -> " if ($v>1) ; foreach $j ( 1..$J ) { $q{$j*100+$i} /= $sum[$i] ; printf "%5.2g " , $q{$j*100+$i} ; } print "\n" if ($v>1) ; } # number of extended inputs $EI = $I ** $N ; # run over extended outputs $EJ = $J ** $N ; if ($tex) { open ( T , "> $texfile" ) ; print T '\left[ \begin{array}{*{'.$EI.'}{c}}'."\n" ; } $pmax = 0.0 ; for ( $ej=$EJ-1;$ej >= 0;$ej-- ) { foreach $ei ( 0..$EI-1 ) { $thisei = $ei ; $thisej = $ej ; # find the prob: $p = 1.0 ; foreach $n ( 1..$N ) { $i = ( $thisei % $I ) + 1; $thisei = int ( $thisei / $I ) ; $j = ( $thisej % $J ) + 1; $thisej = int ( $thisej / $J ) ; $p *= $q{$j*100+$i} ; } printf O "%10.2g " , $p ; if ($tex) { $tstring .= sprintf "%10.2g " , $p ; $tstring .= ( $ei < $EI-1 ) ? sprintf "& " : sprintf "\\\\ \n" ; if ( $ei == $EI-1 ) { # we have to reverse the row order for the tex file $Ts = $tstring.$Ts ; $tstring = "" ; } } if ( $p > $pmax ) { $pmax = $p ; } } print O "\n" ; } $pmaxs = sprintf "%.2g" , $pmax*$factor ; close (O) ; if ($tex) { print T $Ts ; print T '\end{array}'."\n" ; print T '\right] '."\n" ; close (T) ; } if ($labels ) { foreach $i ( 1..$I ) { $inputalphabet{$i} = $i - $displace ; } foreach $j ( 1..$J ) { $outputalphabet{$j} = $j - $displace ; } if ( ($J==3) && $erasure ) { $outputalphabet{2} = "?" ; $outputalphabet{3} = "1" ; } if ( ($J==27) ) { foreach $i ( 1..$I-1 ) { $inputalphabet{$i} = chr(ord('A')+$i-1) ; $outputalphabet{$i} = $inputalphabet{$i} ; } $i = $I ; $inputalphabet{$i} = '-'; $outputalphabet{$i} = '-'; } # write labels file open ( O , "> $labelfile" ) ; for ( $ej=$EJ-1;$ej >= 0;$ej-- ) { $thisej = $ej ; $lab = "" ; foreach $n ( 1..$N ) { $j = ( $thisej % $J ) + 1; $thisej = int ( $thisej / $J ) ; $lab .= $outputalphabet{$j} ; } print O $lab,"\n" ; } foreach $ei ( 0..$EI-1 ) { $thisei = $ei ; $lab = "" ; foreach $n ( 1..$N ) { $i = ( $thisei % $I ) + 1; $thisei = int ( $thisei / $I ) ; $lab .= $inputalphabet{$i} ; } # print O $lab,"\n" ; } close(O) ; } $laborient = ( $N == 1 ) ? 0 : 90 ; $command = " hintond.p h=$h zero=0.0 range=$pmaxs rows=$EJ background=1.0 pos=0.0 o=$psfile labels=$labels labelfile=$labelfile xoffset=$xoffset yoffset=$yoffset fontsize=$fontsize laborient=$laborient $EI < $ofile" ; print $command , "\n" ; system ($command) ; print " gv $psfile \n" ;