Kronecker product: Difference between revisions
Content added Content deleted
m (→JS ES6) |
m (→{{header|REXX}}: added whitespace, added a couple of comments.) |
||
Line 2,721: | Line 2,721: | ||
A little extra coding was added to make the matrix glyphs and elements alignment look nicer. |
A little extra coding was added to make the matrix glyphs and elements 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= |
w= 0 /*W: max width of any matrix element. */ |
||
aMat= 2x2 1 2 3 4 /*define A matrix size and elements.*/ |
aMat= 2x2 1 2 3 4 /*define A matrix size and elements.*/ |
||
bMat= 2x2 0 5 6 7 /* " B " " " " */ |
bMat= 2x2 0 5 6 7 /* " B " " " " */ |
||
Line 2,727: | Line 2,727: | ||
call makeMat 'B', bMat /* " B " " " */ |
call makeMat 'B', bMat /* " B " " " */ |
||
call KronMat 'Kronecker product' /*calculate the Kronecker product. */ |
call KronMat 'Kronecker product' /*calculate the Kronecker product. */ |
||
w=0; |
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.*/ |
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 " " " " */ |
bMat= 3x4 1 1 1 1 1 0 0 1 1 1 1 1 /* " B " " " " */ |
||
Line 2,736: | Line 2,736: | ||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
KronMat: parse arg what; parse var @.a.shape aRows aCols |
KronMat: parse arg what; parse var @.a.shape aRows aCols |
||
#=0; |
#= 0; 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; |
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*/ |
||
end /*cA*/ |
end /*cA*/ |
||
Line 2,750: | Line 2,750: | ||
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 |
||
#=0; do r=1 for row /* [↓] bump item#; get item; max width*/ |
#=0; do r=1 for row /* [↓] bump item#; get item; max width*/ |
||
do c=1 for col; |
do c=1 for col; #= #+1; _= word(elements, #); w= max(w, length(_)) |
||
@.what.r.c=_ |
@.what.r.c=_ |
||
end /*c*/ /* [↑] define an element of WHAT matrix*/ |
end /*c*/ /* [↑] define an element of WHAT matrix*/ |
||
Line 2,756: | Line 2,756: | ||
call showMat what, size; return |
call showMat what, size; return |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
showMat: parse arg what, size .; z='┌'; parse var size row "X" col; |
showMat: parse arg what, size .; z= '┌'; parse var size row "X" col; $=left('', 6) |
||
say; say $ copies('═',7) "matrix" what copies('═',7) |
say; say $ copies('═',7) "matrix" what copies('═',7) |
||
do r=1 for row; _= '│' |
do r=1 for row; _= '│' /*start with long vertical bar*/ |
||
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; z=z '┐'; say $ $ z; end /*show the top part of matrix.*/ |
if r==1 then do; z=z '┐'; say $ $ z; end /*show the top part of matrix.*/ |
||
say $ $ _ '│' |
say $ $ _ '│' /*append a long vertical bar. */ |
||
end /*r*/ |
end /*r*/ |
||
say $ $ translate(z, '└┘', "┌┐"); return /*show the bot part of matrix.*/</lang> |
say $ $ translate(z, '└┘', "┌┐"); return /*show the bot part of matrix.*/</lang> |