ALBD
view release on metacpan or search on metacpan
utils/datasetCreator/squaring/squareMatrix_perl.pl view on Meta::CPAN
#squares a matrix from file and writes the result to file
#use strict;
#use warnings;
#use Getopt::Long;
my $DEBUG = 0;
my $HELP = '';
my %options = ();
#GetOptions( 'debug' => \$DEBUG,
# 'help' => \$HELP,
# 'inputFile=s' => \$options{'inputFile'},
# 'outputFile=s' => \$options{'outputFile'},
#);
#TODO add stuff for help and debug
$options{'inputFile'} = shift;
$options{'outputFile'} = shift;
#input checking
(exists $options{'inputFile'}) or die ("inputFile must be specified\n");
#ensure the input file can be read
open IN, $options{'inputFile'} or
die ("unable to open input file: $options{inputFile}\n");
close IN;
(exists $options{'outputFile'}) or die ("outputFile must be specified\n");
#clear the output file and ensure it can be made
open OUT, '>'.$options{'outputFile'} or
die ("unable to open output file: $options{outputFile}\n");
close OUT;
#read in the matrix
my $matrixRef = fileToSparseMatrix($options{'inputFile'});
#loop over the rows of the B matrix
my %product = ();
my $count = 1;
my $total = scalar keys %{$matrixRef};
my $dumpThreshold = 20000; #dump to file every 20,000 keys
my $keyCount = 0;
foreach my $key0 (keys %{$matrixRef}) {
#loop over row
foreach my $key1 (keys %{$matrixRef}) {
#loop over column
foreach my $key2 (keys %{${$matrixRef}{$key1}}) {
#update values
if (exists ${${$matrixRef}{$key0}}{$key1}) {
#update
if (!exists ${$product{$key0}}{$key2}) {
${$product{$key0}}{$key2} = 0;
$keyCount++;
}
${$product{$key0}}{$key2} +=
${${$matrixRef}{$key0}}{$key1} *
${${$matrixRef}{$key1}}{$key2};
}
}
#output if needed
if ($keyCount > $dumpThreshold) {
&outputMatrix(\%product, $options{'outputFile'});
$keyCount = 0;
}
}
print STDERR "done with row: $count/$total\n";
$count++;
}
( run in 0.976 second using v1.01-cache-2.11-cpan-39bf76dae61 )