Perl-Sensitive Sunglasses PerlMonks

### Matrix Multiplication Problem

by RolandGunslinger (Curate)
 on Sep 02, 2004 at 17:48 UTC Need Help??

RolandGunslinger has asked for the wisdom of the Perl Monks concerning the following question:

I have a small script to multiply two matrices together (and no this is not homework, I'm just puttering around with Perl). I have a problem with it that I have been unable to resolve.
matrix_count_rows_cols(\$r_mat1) is consistently returning 0 rows and 0 columns. Not sure why. Any ideas? (Please keep in mind I'm still pretty green when it comes to Perl). Thanks in advance

```use strict 'vars';
use strict 'subs';
use warnings;

my @matrix1=(
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
);
my @matrix2=(
[2, 4, 6],
[1, 3, 5],
[7, 8, 9]
);
print "matrix1=\n";
for (my \$i=0;\$i le 2;\$i++) {
for (my \$j=0;\$j le 2;\$j++) {
print \$matrix1[\$i][\$j]," ";
}
print "\n";
}
print "matrix2=\n";
for (my \$i=0;\$i le 2;\$i++) {
for (my \$j=0;\$j le 2;\$j++) {
print \$matrix2[\$i][\$j]," ";
}
print "\n";
}
my @product=matrix_multiply(@matrix1,@matrix2);
print "product=\n";
for (my \$i=0;\$i le 2;\$i++) {
for (my \$j=0;\$j le 2;\$j++) {
print \$product[\$i][\$j]," ";
}
print "\n";
}
sub matrix_multiply {
my (\$r_mat1,\$r_mat2)=@_;
my (\$r_product);
my (\$r1,\$c1)=matrix_count_rows_cols(\$r_mat1);
my (\$r2,\$c2)=matrix_count_rows_cols(\$r_mat2);

print \$c1,\$c2,"\n";
print \$r1,\$r2,"\n";

die "matrix 1 has \$c1 columns and matrix 2 has \$r2 rows>"
. " Cannot multiply\n" unless (\$c1==\$r2);
for (my \$i=0;\$i<\$r1;\$i++) {
for (my \$j=0;\$j<\$c2;\$j++) {
my \$sum=0;
for (my \$k=0;\$k<\$c1;\$k++) {
\$sum+=\$r_mat1->[\$i][\$k]*\$r_mat2->[\$k][\$j];
}
\$r_product->[\$i][\$j]=\$sum;
}
}
\$r_product;
}

sub matrix_count_rows_cols {
my (\$r_mat)=@_;
my \$num_rows=@\$r_mat;
my \$num_cols=@{\$r_mat->[0]};
(\$num_rows,\$num_cols);
}

Replies are listed 'Best First'.
Re: Matrix Multiplication Problem
by Roy Johnson (Monsignor) on Sep 02, 2004 at 17:59 UTC
my @product=matrix_multiply(@matrix1,@matrix2); should be my @product=matrix_multiply(\@matrix1,\@matrix2);

Also, your comparisons are should use < instead of le. And matrix_multiply should doesn't return an array, but an array ref.

Caution: Contents may have been coded under pressure.
Thanks Roy, that did it!
Re: Matrix Multiplication Problem
by csuhockey3 (Curate) on Sep 02, 2004 at 18:02 UTC
Re: Matrix Multiplication Problem
by ikegami (Pope) on Sep 02, 2004 at 17:58 UTC

One small problem:

```my @product = matrix_multiply(@matrix1, @matrix2);

should be

```my @product = @{ matrix_multiply(\@matrix1, \@matrix2) };

The former is the same as:

```my @product = matrix_multiply(
\$matrix1[0],
\$matrix1[1],
\$matrix1[2],
\$matrix2[0],
\$matrix2[1],
\$matrix2[2]
);
# @product has only one element.
# \$product[0] is a ref to the resulting matrix
Re: Matrix Multiplication Problem
by cyocum (Curate) on Sep 02, 2004 at 23:28 UTC

You may also want to look into Perl Data Language which is specifically made for working with matrices.

And one can have fun parallelizing PDL with the powerful Many-core Engine (MCE) for Perl. The read me file contains benchmark results performing matrix multiplication across many cores.

Create A New User
Node Status?
node history
Node Type: perlquestion [id://388017]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2021-01-19 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
The STEM quote I most wish I'd made is:

Results (197 votes). Check out past polls.

Notices?