http://qs1969.pair.com?node_id=11144907


in reply to Re: Kronecker Product
in thread Kronecker Product

I was inspired to copy the recently (as of 2.077) added dupN to make inflateN, which does the converse of dupN, suitable for Kronecker products.
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] ]
The above will broadcast nicely over any number of dimensions, not just 2. inflateN has been added and will be in the next PDL release (2.081).