#----------------------------- |
use PDL; |
# $a and $b are both pdl objects |
$c = $a * $b ; |
#----------------------------- |
sub mmult { |
my ( $m1 , $m2 ) = @_ ; |
my ( $m1rows , $m1cols ) = matdim( $m1 ); |
my ( $m2rows , $m2cols ) = matdim( $m2 ); |
unless ( $m1cols == $m2rows ) { # raise exception |
die "IndexError: matrices don't match: $m1cols != $m2rows" ; |
} |
my $result = []; |
my ( $i , $j , $k ); |
for $i (range( $m1rows )) { |
for $j (range( $m2cols )) { |
for $k (range( $m1cols )) { |
$result ->[ $i ][ $j ] += $m1 ->[ $i ][ $k ] * $m2 ->[ $k ][ $j ]; |
} |
} |
} |
return $result ; |
} |
sub range { 0 .. ( $_ [0] - 1) } |
sub veclen { |
my $ary_ref = $_ [0]; |
my $type = ref $ary_ref ; |
if ( $type ne "ARRAY" ) { die "$type is bad array ref for $ary_ref" } |
return scalar (@ $ary_ref ); |
} |
sub matdim { |
my $matrix = $_ [0]; |
my $rows = veclen( $matrix ); |
my $cols = veclen( $matrix ->[0]); |
return ( $rows , $cols ); |
} |
#----------------------------- |
use PDL; |
$a = pdl [ |
[ 3, 2, 3 ], |
[ 5, 9, 8 ], |
]; |
$b = pdl [ |
[ 4, 7 ], |
[ 9, 3 ], |
[ 8, 1 ], |
]; |
$c = $a x $b ; # x overload |
#----------------------------- |
# mmult() and other subroutines as above |
$x = [ |
[ 3, 2, 3 ], |
[ 5, 9, 8 ], |
]; |
$y = [ |
[ 4, 7 ], |
[ 9, 3 ], |
[ 8, 1 ], |
]; |
$z = mmult( $x , $y ); |
#----------------------------- |