Kronecker product: Difference between revisions

m (→‎{{header|REXX}}: added/changed comments and whitespace, changed indentations.)
Line 403:
{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|AWK}}==
<lang AWK>
# syntax: GAWK -f KRONECKER_PRODUCT.AWK
BEGIN {
A[++a] = "1 2" ; B[++b] = "0 5"
A[++a] = "3 4" ; B[++b] = "6 7"
main("sample 1",1234)
A[++a] = "0 1 0" ; B[++b] = "1 1 1 1"
A[++a] = "1 1 1" ; B[++b] = "1 0 0 1"
A[++a] = "0 1 0" ; B[++b] = "1 1 1 1"
main("sample 2",3)
exit(0)
}
function main(desc,option) {
#
# option: allows complete flexibility of output; they may be combined
# 1 show A and B matrix
# 2 show A x B
# 3 show product
# 4 show Arow,Acol x Brow,Bcol
#
printf("%s\n\n",desc)
if (option ~ /[1234]/) {
a_rows = show_array(A,"A",option)
b_rows = show_array(B,"B",option)
if (option ~ /2/) { prn("A x B",2) }
if (option ~ /3/) { prn("Product",3) }
if (option ~ /4/) { prn("Arow,Acol x Brow,Bcol",4) }
}
else {
print("nothing to print")
}
print("")
a = b = 0 # reset
delete A
delete B
}
function prn(desc,option, a_cols,b_cols,w,x,y,z,AA,BB) {
printf("%s:\n",desc)
for (w=1; w<=a_rows; w++) {
a_cols = split(A[w],AA," ")
for (x=1; x<=b_rows; x++) {
b_cols = split(B[x],BB," ")
printf("[ ")
for (y=1; y<=a_cols; y++) {
for (z=1; z<=b_cols; z++) {
if (option ~ /2/) { printf("%sx%s ",AA[y],BB[z]) }
if (option ~ /3/) { printf("%2s ",AA[y] * BB[z]) }
if (option ~ /4/) { printf("%s,%sx%s,%s ",w,y,x,z) }
}
}
printf("]\n")
}
}
}
function show_array(arr,desc,option, i,n) {
for (i in arr) {
n++
}
if (option ~ /1/) {
printf("Matrix %s:\n",desc)
for (i=1; i<=n; i++) {
printf("[ %s ]\n",arr[i])
}
}
return(n)
}
</lang>
{{out}}
<pre>
sample 1
 
Matrix A:
[ 1 2 ]
[ 3 4 ]
Matrix B:
[ 0 5 ]
[ 6 7 ]
A x B:
[ 1x0 1x5 2x0 2x5 ]
[ 1x6 1x7 2x6 2x7 ]
[ 3x0 3x5 4x0 4x5 ]
[ 3x6 3x7 4x6 4x7 ]
Product:
[ 0 5 0 10 ]
[ 6 7 12 14 ]
[ 0 15 0 20 ]
[ 18 21 24 28 ]
Arow,Acol x Brow,Bcol:
[ 1,1x1,1 1,1x1,2 1,2x1,1 1,2x1,2 ]
[ 1,1x2,1 1,1x2,2 1,2x2,1 1,2x2,2 ]
[ 2,1x1,1 2,1x1,2 2,2x1,1 2,2x1,2 ]
[ 2,1x2,1 2,1x2,2 2,2x2,1 2,2x2,2 ]
 
sample 2
 
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|FreeBASIC}}==
477

edits