Kronecker product: Difference between revisions
Content added Content deleted
(→{{header|Fortran}}: Or, don't produce array AB as such.) |
(Add SuperCollider example) |
||
Line 1,549: | Line 1,549: | ||
[0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0] |
[0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0] |
||
[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0] |
[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0] |
||
</pre> |
|||
=={{header|SuperCollider}}== |
|||
<lang SuperCollider>// the iterative version is derived from the javascript one here: |
|||
( |
|||
f = { |a, b| |
|||
var m = a.size; |
|||
var n = a[0].size; |
|||
var p = b.size; |
|||
var q = b[0].size; |
|||
var rtn = m * p; |
|||
var ctn = n * q; |
|||
var res = { 0.dup(ctn) }.dup(rtn); |
|||
m.do { |i| |
|||
n.do { |j| |
|||
p.do { |k| |
|||
q.do { |l| |
|||
res[p*i+k][q*j+l] = a[i][j] * b[k][l]; |
|||
} |
|||
} |
|||
} |
|||
}; |
|||
res |
|||
}; |
|||
) |
|||
// Like APL/J, SuperCollider has applicative operators, so here is a shorter version. |
|||
// the idea is to first replace every element of b with its product with all of a |
|||
// and then reshape the matrix appropriately |
|||
// note that +++ is lamination: [[1, 2, 3], [4, 5, 6]] +++ [100, 200] returns [ [ 1, 2, 3, 100 ], [ 4, 5, 6, 200 ] ]. |
|||
( |
|||
f = { |a, b| |
|||
a.collect { |x| |
|||
x.collect { |y| b * y }.reduce('+++') |
|||
}.reduce('++') |
|||
} |
|||
) |
|||
// to apply either of the two functions: |
|||
( |
|||
x = f.( |
|||
[ |
|||
[0, 1, 0], |
|||
[1, 1, 1], |
|||
[0, 1, 0] |
|||
], |
|||
[ |
|||
[1, 1, 1, 1], |
|||
[1, 0, 0, 1], |
|||
[1, 1, 1, 1] |
|||
] |
|||
) |
|||
) |
|||
</lang> |
|||
Results in: |
|||
<pre> |
|||
[ |
|||
[ 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0 ], |
|||
[ 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0 ], |
|||
[ 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0 ], |
|||
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ], |
|||
[ 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1 ], |
|||
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ], |
|||
[ 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0 ], |
|||
[ 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0 ], |
|||
[ 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0 ] |
|||
] |
|||
</pre> |
</pre> |
||