Anonymous user
Kronecker product: Difference between revisions
m
→{{header|REXX}}: added/changed comments and whitespace, changed indentations.
m (added whitespace to the task's preamble, spelled out a number..) |
m (→{{header|REXX}}: added/changed comments and whitespace, changed indentations.) |
||
Line 939:
A little extra coding was added to make the matrix glyphs and element alignment look nicer.
<lang rexx>/*REXX program calculates the Kronecker product of two arbitrary size matrices. */
w=0
aMat= 2x2 1 2 3 4
bMat= 2x2 0 5 6 7 /* " B " " " " */
call makeMat 'A', aMat /*construct A matrix from elements.*/
w=0; say; say copies('░', 55); say /*display a fence between the 2 outputs*/
aMat= 3x3 0 1 0 1 1 1 0 1 0 /*define A matrix size and elements.*/
bMat= 3x4 1 1 1 1 1 0 0 1 1 1 1 1 /* " B " " " " */
call makeMat 'A', aMat /*construct A matrix from elements.*/
call makeMat 'B', bMat /* " B " " " */
call KronMat 'Kronecker product' /*calculate the Kronecker produect. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 952 ⟶ 956:
parse var @.b.shape bRows bCols
do rA=1 for aRows
do rB=1 for bRows; #=#+1; ##=0; _=
do cA=1 for aCols; x=@.a.rA.cA
do cB=1 for bCols; y=@.b.rB.cB; ##=##+1; xy=x*y; _=_ xy
@.what.#.##=xy; w=max(w, length(xy) )
end /*cB*/
Line 960 ⟶ 964:
end /*rB*/
end /*rA*/
call showMat what, aRows*bRows || 'X' || aRows*bCols; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
makeMat: parse arg what, size elements; arg , row 'X' col .; @.what.shape= row col
Line 971 ⟶ 975:
/*──────────────────────────────────────────────────────────────────────────────────────*/
showMat: parse arg what, size .; z='┌'; parse var size row 'X' col; $=left('', 6)
say; say $ copies('═',7)
do r=1 for row; _= '│'
do c=1 for col; _=_ right(@.what.r.c, w); if r==1 then z=z left('',w)
end /*c*/
if r==1 then do;
say $ $ _ '│'
end /*r*/
say $ $ translate(z, '└┘', "┌┐"); return
{{out|output|text= when using the default
<pre>
═══════ matrix A ═══════
┌ ┐
│ 1 2 │
│ 3 4 │
└ ┘
═══════ matrix B ═══════
┌ ┐
│ 0 5 │
│ 6 7 │
└ ┘
═══════ matrix Kronecker product ═══════
┌ ┐
│ 0 5 0 10 │
│ 6 7 12 14 │
│ 0 15 0 20 │
│ 18 21 24 28 │
└ ┘
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
═══════ matrix A ═══════
┌ ┐
│ 0 1 0 │
│ 1 1 1 │
│ 0 1 0 │
└ ┘
═══════ matrix B ═══════
┌ ┐
│ 1 1 1 1 │
│ 1 0 0 1 │
│ 1 1 1 1 │
└ ┘
═══════ matrix Kronecker product ═══════
┌ ┐
│ 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>
|