Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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).

In reply to Re^2: Kronecker Product by etj
in thread Kronecker Product by choroba

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-16 21:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found