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).
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.