Jump to content

Kronecker product: Difference between revisions

→‎{{header|REXX}}: added the REXX language.
(→‎{{header|Perl 6}}: Added Perl 6 solution)
(→‎{{header|REXX}}: added the REXX language.)
Line 747:
(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>
 
=={{header|REXXl}}==
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≡max width of matric elements.*/
aMat= 2x2 1 2 3 4; bMat= 2x2 0 5 6 7
call makeMat 'A', aMat; call makeMat 'B', bMat
call KronMat KP
w=0; say; say copies('░', 50); say
aMat= 3x3 0 1 0 1 1 1 0 1 0; bMat= 3x4 1 1 1 1 1 0 0 1 1 1 1 1
call makeMat 'A', aMat; call makeMat 'B', bMat
call KronMat KP
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
KronMat: parse arg what; #=0; parse var @.a.shape aRows aCols
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*/
end /*cA*/
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
#=0; do r=1 for row /* [↓] bump item#; get item; max width*/
do c=1 for col; #=#+1; _=word(elements, #); w=max(w, length(_) )
@.what.r.c=_
end /*c*/ /* [↑] define an element of WHAT matrix*/
end /*r*/
call showMat what, size; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
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 input:}}
<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>
 
=={{header|zkl}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.