#!/usr/local/bin/perl -w # Ashorten.p reads an alist and writes an narrower alist matrix # in which some columns have been superposed. # standard method is to take weight 3 columns only and # combine them to make the required weights. # # Ashorten2.p is a half-written mess in which I started rewriting the # cocked up Ashorten.p to rectify its terrible mlist handling # then I realised I could just write a Acleanup.p program that # sorts out an Alist from the nlists, and use it after Ashorten.p ## # usage # Ashorten.p N\[6]=3 N\[9]=3 inA > outA # # 99 12 13 # $minstartweight = 3 ; # what weight columns are permitted to be merged $maxstartweight = 4 ; # $seed=123 ; $verbose = 1 ; $randomorder = 0 ; # if this is 1, then each list of points n addressed by the first # point in each face[r] with r>=2 is randomly ordered for all their checks. for($t=0;$t<=50;$t++){$N[$t]=0;} eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^([\w\[\]]+)=(.*)/ && shift ; print STDERR "--------Ashorten.p----------- DJCM 99/12/24\n" ; print STDERR "Will create the following columns:\n" ; $num = 0 ; for($t=50;$t>=0;$t--){ if ($N[$t]>0) { print STDERR "weight $t:\t $N[$t]\n"; $weightlist[$num] = $t ; $num ++ ; } } if ( !$num ) { print STDERR "Fatal error\n - command line apparently contained no requested column numbers like N\\[6]=10\n" ; exit; } # first, read the old alist in $_ = <>; ($L,$M) = split ; print STDERR "------------\nRead in $L $M\n" ; $N = $L ; &set_up_alist($M,$N) ; sub set_up_alist { local($M,$N) = @_ ; # for ( $n = 1 ; $n <= $N ; $n ++ ) { $nlist[$n] = "" ; $numnlist[$n] = 0 ; } for ( $m = 1 ; $m <= $M ; $m ++ ) { $mlist[$m] = "" ; $nummlist[$m] = 0 ; } } $_ = <>; # max numbers $_ = <>; # @oldnumnlist = split ; $_ = <>; # @oldnummlist = split ; for ( $l = 1 ; $l <= $L ; $l ++ ) { $_ = <>; @y = split ; for ( $u = 0 ; $u <= $#y ; $u ++ ){ $ty = $y[$u] ; if ( $ty > 0 ) { $m = $ty ; $n = $l ; &mnadd($m,$n); } } } @oldmlist = @mlist ; @oldnlist = @nlist ; @oldnummlist = @nummlist ; @oldnumnlist = @numnlist ; # forget undef @mlist ; undef @nlist ; undef @nummlist ; undef @numnlist ; &set_up_alist($M,$N) ; $newN = 0 ; sub directtransfer { #(oldn,newn) local($n,$newN) = @_ ; @nlistsource = split ( "\t" , $oldnlist[$n] ) ; foreach $m (@nlistsource) { &mnadd($m,$newN) if ($m >0) ; } } # establish range of mergable columns $nrangestart = 0 ; $nrangeend = $N ; for ( $n = 1 ; $n <= $N ; $n ++ ) { if ( ( $oldnumnlist[$n] < $minstartweight ) || ( $oldnumnlist[$n] > $maxstartweight ) ) { # then this column is not mergable, and we can copy it over now $nrangestart = $n+1 ; # $newN ++ ; directtransfer($n,$newN) ; } else { $n=$N ; } # stop this loop, because we have found a valid start } for ( $n = $N ; $n >= 1 ; $n -- ) { if ( ( $oldnumnlist[$n] < $minstartweight ) || ( $oldnumnlist[$n] > $maxstartweight ) ) { # then this column is not mergable. copy it over now, as above $nrangeend = $n-1 ; # @nlistsource = split ( "\t" , $nlist[$n] ) ; $newN ++ ; foreach $m (@nlistsource) { &mnadd($m,$newN) if ($m >0) ; } } else { $n=-1 ; } # stop this loop, we have found a valid end } if ( $nrangeend - $nrangestart < 1 ) { print STDERR "insufficient range of columns permitted by minstartweight and maxstartweight values. fatal error.\n" ; exit(0); } if ( $randomorder ) { srand ( $seed ) ; } foreach $targetweight ( @weightlist ) { print STDERR "doing weight $targetweight " ; for ( $doing = 1 ; $doing <= $N[$targetweight] ; $doing ++ ) { print STDERR "." ; # ensure we have a valid sink column while (( $numnlist[$sinkn] > $maxstartweight )|| ( $numnlist[$sinkn] < $minstartweight )) { # this particular column is ineligible for merging, so write it # straight. $newN ++ ; if ($sinkn > $nrangeend) { print STDERR "run out of columns\n" ; print STDERR "need to add jump to source code here\n" ; exit(0) ; } } $sourcen = $sinkn + 1 ; # start adding stuff to this column while ( ( $numnlist[$sinkn] <= $targetweight - $minstartweight ) && ($sourcen <= $nrangeend) ) { $nlistsink = $nlist[$sinkn] ; # @nlistsink = split ( "\t" , $nlist[$sinkn] ) ; while (( $numnlist[$sourcen] > $maxstartweight )|| ( $numnlist[$sourcen] < $minstartweight )) { $sourcen ++ ; } # while adding this, check # for clash of this candidate source with existing sink contents @nlistsource = split ( "\t" , $nlist[$sourcen] ) ; foreach $m2 (@nlistsource) { print STDERR "considering m=$m2\n" if ($verbose>4); if ( !($nlistsink =~ m/\b$m2\b/) ) { # $m2 not on sink list &mnadd($m2,$sinkn) ; } else { print STDERR "c" ; # clash } &mnsubtract($m2,$sourcen) ; # remove from the mlist only } # clean out those columns $nlist[$sourcen]="" ; $numnlist[$sourcen]=0; $newN -- ; # and nominate the next source column $sourcen ++ ; } # end of adding to this sink, think of a new sink: $sinkn = $sourcen ; } print STDERR "\n" ; } print STDERR "done N = $newN ; M = $M\n" ; if ( 1 ) { $head = "$newN $M\n" ; print STDERR "Writing code.\n" ; # find $t (max) and $tr (max) $t = 0 ; $tr = 0 ; for ( $n = 1 ; $n <= $N ; $n ++ ) { if ( $numnlist[$n] > $t ) { $t = $numnlist[$n] ; } } for ( $m = 1 ; $m <= $M ; $m ++ ) { if ( $nummlist[$m] > $tr ) { $tr = $nummlist[$m] ; } } $head .= "$t $tr\n" ; # append zeros to short lines for ( $n = 1 ; $n <= $N ; $n ++ ) { $thisn = $numnlist[$n] ; if ( $thisn ) { $head .= "$thisn " ; while ( $thisn < $t ) { $nlist[$n] .= "0\t" ; $thisn ++ ; } } # otherwise it is a dead column } $head .= "\n" ; for ( $m = 1 ; $m <= $M ; $m ++ ) { $thism = $nummlist[$m] ; $head .= "$thism " ; while ( $thism < $tr ) { $mlist[$m] .= "0\t" ; $thism ++ ; } } $head .= "\n" ; print $head ; # print STDERR $head ; # finish lists for ( $n = 1 ; $n <= $N ; $n ++ ) { $thisn = $numnlist[$n] ; if ( $thisn ) { $nlist[$n] =~ s/\t$/\n/ ; print $nlist[$n] ; } } for ( $m = 1 ; $m <= $M ; $m ++ ) { $mlist[$m] =~ s/\t$/\n/ ; print $mlist[$m] ; } } sub mnadd { local($m,$n)=@_; # put m and n on each others lists $nlist[$n] .= $m."\t" ; $mlist[$m] .= $n."\t" ; $numnlist[$n] ++ ; $nummlist[$m] ++ ; } # remove from the mlist only sub mnsubtract { local($m,$n)=@_; $count = ( $mlist[$m] =~ s/\b$n\t// ) ; if ( $count ) { $nummlist[$m] -- ; } else { print STDERR "error, unable to remove $n from mlist $m\n $mlist[$m]\n" ; } }