use strict;
use warnings;
my (@firstfactor, @secondfactor, @product);
my $file1 = 'matrixin1.txt';
open( FILE1, $file1 ) or die "Can't open file '$file1': $!";
while(
chomp;
my @row1 = split;
push @firstfactor, \@row1;
}
close( FILE1 );
my $file2 = 'matrixin2.txt';
open( FILE2, $file2 ) or die "Can't open file '$file2': $!";
while(
chomp;
my @row2 = split;
push @secondfactor, \@row2;
}
close( FILE2 );
@product = getproduct (\@firstfactor, \@secondfactor);
for my $i (0..$#product)
{
for my $j (0..$#{$product[0]})
{
print "$product[$i][$j]","\t";
}
print "\n";
}
sub getproduct {
my ($a, $b) = @_;
my @product;
my $arow=$#$a;
my $acol=$#{$$a[0]};
my $brow=$#$b;
my $bcol=$#{$$b[0]};
$acol == $brow or die "The matrices can't be multiplied!\n";
for my $i (0..$arow) {
for my $j (0..$bcol) {
for my $k (0..$acol) {
$product[$i][$j] += $$a[$i][$k] * $$b[$k][$j];
}
}
}
return @product;
}
No comments:
Post a Comment