Kronecker product: Difference between revisions

(Frink)
Line 1,544:
 
=={{header|Frink}}==
The Frink library [https://frinklang.org/fsp/colorize.fsp?f=Matrix.frink Matrix.frink] contains an implementation of Kronecker product. TheHowever, the following example demonstrates calculating the Kronecker product and typesetting the equations using Frink'smultidimensional arrays and no layoutexternal routineslibraries.
<lang frink>usea Matrix.frink= [[1,2],[3,4]]
ab = new Matrix[ [[10,25],[36,4] 7]]
b = new Matrix[ [[0,5],[6,7] ]]
println[formatProd[a,b]]
 
c = new Matrix[ [[0,1,0],[1,1,1],[0,1,0] ]]
d = new Matrix[ [[1,1,1,1],[1,0,0,1],[1,1,1,1]] ]
println[formatProd[c,d]]
 
formatProd[a,b] := formatTable[[[a.formatMatrix[a], "\u2297", b.formatMatrix[b], "=", a.formatMatrix[KroneckerProduct[a,b].formatMatrix[]]]]</lang>
 
KroneckerProduct[a, b] :=
{
[m,n] = a.dimensions[]
[p,q] = b.dimensions[]
rows = m p
cols = n q
n = new array[[rows, cols], 0]
for i=0 to rows-1
for j=0 to cols-1
n@i@j = a@(i div p)@(j div q) * b@(i mod p)@(j mod q)
return n
}</lang>
{{out}}
<pre>
│ 0 5 0 10│
│1 2│ │0 5│ ┌ ┐6 7 12 14│
│3│1 4│ 2│ │6 7│ =│0 5│ 06 7 15 12 0 20│14│
= │18 21 24 28│
│3 4│ │6 7│ 0 15 0 20│
│18 21 │024 0 0 0 1 1 1 1 0 0 0 0│28│
│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 1 0│ │1 1 1 1│ │1 1 1 1 1 1 1 │0 0 0 0 1 1 1 1 1│ 0 0 0 0│
│ │
│1 1 1│ ⊗ │1 0 0 1│ = │1 0 0 1 1 0 0 1 1 0 0 1│
│0 1 0│ │1 1 1 1│ │1 1 1 1 1 1 1 1 1 │0 0 0 0 1 0 0 1 1│ 0 0 0 0│
│0 0 0 0 1 1 1 1 0 0 0 0│
│0 0 0 0 1 0 01 1 1 0 0 0 0│
│0 0 0 0 1 1 1 1 0 0 0 0│
│0 1 0│ │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│
│ │ │ │ │ │
│0 1 0│ │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 0 0 1 0 0 0 0│
│ │
│0 0 0 0 1 1 1 1 0 0 0 0│
└ ┘
</pre>
 
490

edits