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; KP= 'Kronecker product' /*W≡maxW: max width of matricany elementsmatrix element. */
aMat= 2x2 1 2 3 4; bMat= 2x2 0 5 6 7 /*define A matrix size and elements.*/
bMat= 2x2 0 5 6 7 /* " B " " " " */
call makeMat 'A', aMat; call makeMat 'B', bMat
call makeMat 'A', aMat /*construct A matrix from elements.*/
call KronMat KP
w=0;call makeMat 'B', bMat say; /* " say copies('░', 50); B " " " say*/
aMat=call 3x3KronMat 'Kronecker 0product' 1 0 1 1 1 0 1 0; bMat= 3x4 1 1 1 1/*calculate 1the 0 0Kronecker 1 1produect. 1 1 1*/
w=0; say; say copies('░', 55); say /*display a fence between the 2 outputs*/
call makeMat 'A', aMat; call makeMat 'B', bMat
aMat= 3x3 0 1 0 1 1 1 0 1 0 /*define A matrix size and elements.*/
call KronMat KP
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) 'matrix' what 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; z=z '┐'; say $ $ z; end /*show the top part of matrix.*/
say $ $ _ '│'
end /*r*/
say $ $ translate(z, '└┘', "┌┐"); return /*show the bot part of matrix.*/</lang>
{{out|output|text=&nbsp; when using the default inputinputs:}}
<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>