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