Reputation: 355
I have a subroutine that works with two. Can I call the subroutine within the subroutine?
Upvotes: 0
Views: 412
Reputation: 183381
The simplest way is a bit of recursion — just change this:
my $ref1 = $_[0];
my $ref2 = $_[1];
to this:
my $ref1 = shift;
my $ref2 = shift;
and this:
return @product;
to this:
if(@_ > 0)
{ return &matrix(\@product, @_); }
else
{ return @product; }
But the most efficient way is to start by examining the dimensions of the various arrays and thereby determining the best order in which to perform the multiplications. (Array multiplication, though not commutative, is associative, and if not all the arrays have the same dimensions, then A(BC)
can be much more expensive, or much less expensive, than (AB)C
. For example, if the dimensions are 1-by-100, 100-by-1, and 1-by-100, then (AB)C
creates a 1-by-1 matrix as an intermediate step, whereas A(BC)
creates a 100-by-100 matrix as an intermediate step.)
Upvotes: 3