#!/usr/local/bin/perl -w # matrix2tex.p # # writes latex files for trellises (noiseless channel coding) # # stdin: an S*S matrix # stdout: the matrix formatted in tex # if there are extra lines given then they are formatted as eigenvectors # and eigenvalues # # assuming matlab saved thus: # save rw5.dat rw5 el ll er lr -ascii # # matrix, eleft, # eigs, eright, eigs. # # example: # $killleadingzero = 1 ; # 1 kills in matrices and vectors. (not in eigenvalues) $verbose = 0 ; $changefg = 9999 ; $sizefromfirstrow = 1 ; # $format = "%4.1g" ; $iformat = "%8.0f" ; $fformat = "%8.2f" ; # format for vector entries $mfformat = "%8.2f" ; # format for matrix entries $cformat = "%5.1f" ; $gformat = "%8.2g" ; $efformat = "%8.2f" ; # formats for eigenvalues $egformat = "%8.2g" ; # $printzeroes = 1 ; $times='\!\times\!' ; $times = '\cdot' ; $mseparator="\\," ; eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; @A = <> ; foreach $a ( 0..$#A ) { $A[$a] =~ s/\s([\+\-])\s/$1/g ; # complex numbers from a + bi to a+bi $A[$a] =~ s/^\s+//g ; # kill leading space } if ( $sizefromfirstrow ) { @firstrow = split( '\s+' , $A[0] ) ; $S = $#firstrow + 1 ; } else { $S = $#A + 1 ; } print STDERR ".Read in $#A+1 rows for a $S * $S matrix\n" ; # if ( $#A < $S + 1 ) { print STDERR "not enough input\n" ; exit (0); } #open ( O , "> $matrixfile " ) ; #print STDERR '\input{',$matrixfile,"\}\n"; ## write the matrix print "%%%% \n" ; print "%%%% written by matrix2tex.p \n" ; print "%%%% \n" ; print "%%%% beginning of matrix \n" ; print "%%%% \n" ; print " \$ \n" ; print "%%%% \n" ; $Sm = $S - 1 ; print "\\left[\n\\begin{array}{\@{$mseparator}*{$Sm}{c\@{$mseparator$mseparator}}c\@{$mseparator}}\n" ; for ( $s = 0 ; $s <= $S-1 ; $s ++ ) { # $row = ($S-1) - $s ; $row = $s ; @c = split ( '\s+' , $A[$row] ) ; if ( $#c < $S-1 ) { print STDERR "problem row $row : not enough stuff $#c\n" ; } if ( $#c > $S-1 ) { print STDERR "row $row : discarding excess entries $#c\n" ; } # to is row, from is col for ( $sfrom = 0 ; ($sfrom <= $S-1) ; $sfrom ++ ) { $tmp = $S-1-$sfrom ; $tmp = $sfrom ; if ( $tmp <= $#c) { $edge = $c[$tmp] ; } else { $edge = 0 ; } &echeck ; if ( !($edge == 0) || $printzeroes ) { &e2m($mfformat,$gformat,$iformat,$cformat) ; &killzero if ($killleadingzero) ; print "$M " ; } if ( $sfrom < $S - 1 ) { print " & " ; } else { print "\\\\ \n" ; } } } print "\\end{array}\n" ; print "\\right]\n" ; if ( $s < $#A ) { # get the eigs # create heads of eigenvectors for ( $sfrom = 0 ; ($sfrom <= $S-1) ; $sfrom ++ ) { $eleft[$sfrom] = '\left[ \begin{array}{@{}r@{}}'."\n" ; $eright[$sfrom] = '\left[ \begin{array}{@{}r@{}}'."\n" ; $allnegativel[$sfrom] = 1 ; $allnegativer[$sfrom] = 1 ; } # keep going for another S rows # these are left eigenvectors for ( $i=0 ; ($s <= $#A)&&($i < $S) ; $i ++ , $s ++ ) { $row = $s ; @c = split ( '\s+' , $A[$row] ) ; if ( $#c < $S-1 ) { print STDERR "problem row $row : not enough stuff $#c\n" ; } if ( $#c > $S-1 ) { print STDERR "row $row : discarding excess entries $#c\n" ; } # to is row, from is col for ( $sfrom = 0 ; ($sfrom <= $S-1) ; $sfrom ++ ) { $tmp = $sfrom ; if ( $tmp <= $#c) { $edge = $c[$tmp] ; } else { $edge = 0 ; } &echeck ; if ( !$ereal || $edge > 0.0 ) { $allnegativel[$sfrom] = 0 ; } &e2m($fformat,$gformat,$iformat,$cformat) ; &killzero if ($killleadingzero) ; $eleft[$sfrom] .= "$M " ; if ( $i < $S - 1 ) { $eleft[$sfrom] .= "\\\\ \n" ; } else { $eleft[$sfrom] .= " \n" ; } } } # these are left eigenvectors' eigenvalues for ( $i=0 ; ($s <= $#A)&&($i < $S) ; $i ++ , $s ++ ) { $row = $s ; @c = split ( '\s+' , $A[$row] ) ; if ( $#c < $S-1 ) { print STDERR "problem row $row : not enough stuff $#c\n" ; } if ( $#c > $S-1 ) { print STDERR "row $row : discarding excess entries $#c\n" ; } $tmp = $i ; if ( $tmp <= $#c) { $edge = $c[$tmp] ; } else { $edge = 0 ; } &echeck ; &e2m($efformat,$egformat,$iformat,$cformat) ; $lambdal[$i] = $M ; } # these are right eigenvectors for ( $i=0 ; ($s <= $#A)&&($i < $S) ; $i ++ , $s ++ ) { $row = $s ; @c = split ( '\s+' , $A[$row] ) ; if ( $#c < $S-1 ) { print STDERR "problem row $row : not enough stuff $#c\n" ; } if ( $#c > $S-1 ) { print STDERR "row $row : discarding excess entries $#c\n" ; } # to is row, from is col for ( $sfrom = 0 ; ($sfrom <= $S-1) ; $sfrom ++ ) { $tmp = $sfrom ; if ( $tmp <= $#c) { $edge = $c[$tmp] ; } else { $edge = 0 ; } &echeck ; if ( !$ereal || $edge > 0.0 ) { $allnegativer[$sfrom] = 0 ; } &e2m($fformat,$gformat,$iformat,$cformat) ; &killzero if ($killleadingzero) ; $eright[$sfrom] .= "$M " ; if ( $i < $S - 1 ) { $eright[$sfrom] .= "\\\\ \n" ; } else { $eright[$sfrom] .= " \n" ; } } } # these are right eigenvectors' eigenvalues for ( $i=0 ; ($s <= $#A)&&($i < $S) ; $i ++ , $s ++ ) { $row = $s ; @c = split ( '\s+' , $A[$row] ) ; if ( $#c < $S-1 ) { print STDERR "problem row $row : not enough stuff $#c\n" ; } if ( $#c > $S-1 ) { print STDERR "row $row : discarding excess entries $#c\n" ; } $tmp = $i ; if ( $tmp <= $#c) { $edge = $c[$tmp] ; } else { $edge = 0 ; } &echeck ; &e2m($efformat,$egformat,$iformat,$cformat) ; $lambdar[$i] = $M ; # print STDERR "setting lambdar[$s] = $M \n" ; } print "%%%% \n" ; print "%%%% DIVIDER between matrix and eigs\n" ; print "%%%% \n" ; print "\$ & \$ \n" ; print "%%%% \n" ; # reorder the eigenvalues @ordered = ( 0 .. $S-1 ) ; # print @ordered ; @elperm = sort bylaml @ordered ; @erperm = sort bylamr @ordered ; print "%% sort order left eigs: \n%" ; print @elperm ; print "\n" ; print "%% sort order right eigs: \n%" ; print @erperm ; print "\n" ; # tabulate all eigs print '\begin{array}{*{'.$S.'}{c}}'."\n" ; for ( $sf = 0 ; ($sf <= $S-1) ; $sf ++ ) { $l = $elperm[$sf] ; $r = $erperm[$sf] ; if (!( $lambdal[$l] eq $lambdar[$r] )) { print STDERR "Warning, mismatched eigenvalues $sf,$l,$r:\n" ; print STDERR " $lambdal[$l] <==> $lambdar[$r] \n" ; } print "%%%% left eigenvalue $sf ($l)\n" ; print $lambdal[$l] ; if ( $sf < $S - 1 ) { print " & " ; } else { print " \\\\ \n" ; } print "%%%% right eigenvalue $sf ($r)\n%%%%" ; print $lambdar[$r] ; if ( $sf < $S - 1 ) { print " & " ; } else { print "\\\\ \n" ; } } for ( $sf = 0 ; ($sf <= $S-1) ; $sf ++ ) { $l = $elperm[$sf] ; $r = $erperm[$sf] ; if ( $allnegativel[$l] == 1 ) { # flip sign of eigs $flips = ( $eleft[$l] =~ s/([^\n]\s*)\-/$1 /g ) ; print STDERR "..eigleft $sf($l), flipped sign of $flips elements\n" ; } if ( $allnegativer[$r] == 1 ) { $flips = ( $eright[$r] =~ s/([^\n]\s*)\-/$1 /g ) ; print STDERR "..eigright $sf($r), flipped sign of $flips elements\n" ; } $eleft[$l] .= "\\end{array}\n" . "\\right]\n" ; $eright[$r] .= "\\end{array}\n" . "\\right]\n" ; print "%%%% left eigenvector $sf($l)\n" ; print $eleft[$l] ; print "%%%% right eigenvector $sf($r)\n" ; print $eright[$r] ; if ( $sf < $S - 1 ) { print " & " ; } else { print " \\\\ \n" ; } } print "\\end{array} \n" ; print "%%%% \n" ; print "%%%% end of eigs \n" ; print "%%%% \n" ; } print " \$ \n" ; print "%%%% \n" ; # close (O) ; # end exit(0); sub e2m { # e2m($fformat,$gformat,$iformat,$cformat); local($fformat,$gformat,$iformat,$cformat) = @_ ; if ( $edge =~ /([-]*[^\+\-]+)([\+\-])([^\+\-i]+)i/ ) { $real = $1 ; $sign = $2 ; $imag = $3 ; print STDERR " complex number! $real $sign $imag\n" if ( $verbose) ; $M = sprintf ( $cformat , $real ) ; $M .= "\\!$sign\\!" ; $M .= sprintf ( $cformat , $imag ) ; $M .= "i " } elsif ( !($ereal) ) { print STDERR "warning, complex, but no match found $edge\n" ; } else { if ( $edge < $changefg ) { if ( !($edge - int($edge) ) ) { # integer $M = sprintf ( $iformat , $edge ) ; } else { $M = sprintf ( $fformat , $edge ) ; } } else { $M = sprintf ( $gformat , $edge ) ; $M =~ s/([-1234567890\.]+)e(-*)[\+0]*([1234567890]+)/\$$1$times 10^\{$2$3\}\$/ ; } } } sub echeck { if ( $edge =~ /i/ ) { $ereal = 0 ; } else { $ereal = 1 ; } } sub bylaml { $lambdal[$b] <=> $lambdal[$a] ; } sub bylamr { $lambdar[$b] <=> $lambdar[$a] ; } sub killzero { $M =~ s/0\././g; }