我曾尝试使用PDL以更快的方式乘法数组,但是使用单元素的方法会出错。示例代码为
use PDL::Core;
use PDL::Primitive;
my $m = [ [ 2 ] ];
pdl($m) x pdl($m);
错误是:
Undefined subroutine &PDL::mult called at /home/user/perl5/lib/perl5/x86_64-linux-gnu-thread-multi/PDL/Primitive.pm line 263.
Primitive.pm
源代码为:
sub PDL::matmult {
my ($a,$b,$c) = @_;
$b = pdl($b) unless eval { $b->isa('PDL') };
$c = PDL->null unless eval { $c->isa('PDL') };
while($a->getndims < 2) {$a = $a->dummy(-1)}
while($b->getndims < 2) {$b = $b->dummy(-1)}
return ($c .= $a * $b) if( ($a->dim(0)==1 && $a->dim(1)==1) ||
($b->dim(0)==1 && $b->dim(1)==1) );
if($b->dim(1) != $a->dim(0)) {
barf(sprintf("Dim mismatch in matmult of [%dx%d] x [%dx%d]: %d != %d",$a->dim(0),$a->dim(1),$b->dim(0),$b->dim(1),$a->dim(0),$b->dim(1)));
}
PDL::_matmult_int($a,$b,$c);
$c;
}
*matmult = \&PDL::matmult;
似乎只有一个元素时未定义乘法。这是错误吗?是解决该问题的适当方法吗?