note
etj
I was inspired to copy the recently (as of 2.077) added <c>dupN</c> to make <c>inflateN</c>, which does the converse of <c>dupN</c>, suitable for Kronecker products.
<c>
sub PDL::inflateN {
my ($this, @times) = @_;
return $this->copy if !grep $_ != 1, @times;
my $sl = join ',', map "*$_,:", @times;
$this = $this->slice($sl);
$this = $this->clump($_, $_+1) for 0..$#times;
$this;
}
pdl> p $a = pdl '[[1 2][3 4]]'
[
[1 2]
[3 4]
]
pdl> p $b = pdl '[[5 6][7 8][9 10]]'
[
[ 5 6]
[ 7 8]
[ 9 10]
]
pdl> p $a->inflateN($b->dims)
[
[1 1 2 2]
[1 1 2 2]
[1 1 2 2]
[3 3 4 4]
[3 3 4 4]
[3 3 4 4]
]
pdl> p $b->dupN($a->dims)
[
[ 5 6 5 6]
[ 7 8 7 8]
[ 9 10 9 10]
[ 5 6 5 6]
[ 7 8 7 8]
[ 9 10 9 10]
]
sub kron { my ($x,$y) = @_; $x->inflateN($y->dims) * $y->dupN($x->dims) }
pdl> p kron($a,$b)
[
[ 5 6 10 12]
[ 7 8 14 16]
[ 9 10 18 20]
[15 18 20 24]
[21 24 28 32]
[27 30 36 40]
]
</c>
The above will broadcast nicely over any number of dimensions, not just 2. <c>inflateN</c> has been added and will be in the next PDL release (2.081).
11144888
11144901