uplets.pl

#!/usr/bin/perl 

# uplets of a given permutation
# emilbarton 2013

## modules:
######################################################################
use strict;
use warnings FATAL => "all";

## args and globals
######################################################################
if ((@ARGV)< 1 || $ARGV[0] =~ /help|usage/){ &Usage() };
my $TARGET = $ARGV[0]; my @TARGET = split //, $TARGET;
my @UPLETS; my $MODE = 0;
if ((@ARGV) > 2 ) { if (int($ARGV[2])) { $MODE = $ARGV[2] } }
my $SIGNS; my $SEEN = "";
my $INDEX = "0123456789ABCDEFGHIJKLMN";
my @INDEX = split //,$INDEX;
our 	%Digh;
for 	(my $n = 0; $n < scalar(@INDEX); $n++){ $Digh{$INDEX[$n]} = $n };

## permutation check:
######################################################################
my $LEN = (@TARGET); 
die "invalid length" if ($LEN > (@INDEX) );
for (my $i = 0; $i < $LEN; $i++) { $SIGNS = $SIGNS.$INDEX[$i] }  
foreach (@TARGET) {
	my $char = $_; 
	die "invalid sign in $TARGET: $char" if ($SIGNS !~ /$char/i);
	die "repeated signs not allowed" if ($SEEN =~ /$char/i);
	$SEEN .= $char;
}

## uplets:
######################################################################

@UPLETS = &Partel($TARGET);
print "@UPLETS \n";

## subroutines
######################################################################

### dodecad:
sub Dodecad { 
  my ($n) = @_; 
	my $res = $n;
	if (exists $Digh{$n}){ $res = $Digh{$n} }
	else { die "$n is no valid arg." }
  return $res;
} ## END Dodecad().
    

### partel:
sub Partel { 
  my ($row) = @_;
  my @perm = split //, $row;
  my @partel;
  for (my $n = 0; $n < (@perm); $n++) {
    my $U = "";
    my $j = &Dodecad($perm[$n]);
    my $J = $perm[$n];
    next if ("@partel" =~ /$J/);
    if ($J =~ /$INDEX[$n]/){ push @partel,$J }
    else {
      while ($U !~ /$J/){
        $U .= $J;
        $J = $perm[$j];
        $j = &Dodecad($J);
      }
      push @partel,$U;
    }	
  }		
   return @partel;
} ## END Partel().

### usage:
sub Usage {
	my $data;
	while (<DATA>) { $data .= $_ };
	die "Usage:\n$data";
} ## END Usage()

__DATA__

This program can be used to compute the elementary partition (or uplets) 
of a permutation. 

Try:

  $ ./uplets.pl target 

  Example:
  
  $ ./uplets.pl 73206514 
  746130 2 5 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s