Kronecker product: Difference between revisions
Added Easylang
Alextretyak (talk | contribs) (Added 11l) |
(Added Easylang) |
||
(36 intermediate revisions by 24 users not shown) | |||
Line 1:
{{Wikipedia}}
{{
Line 49:
{{trans|Python}}
<
V b1 = [[0, 5], [6, 7]]
Line 82:
V result2 = kronecker(a2, b2)
L(elem) result2
print(elem)</
{{out}}
Line 103:
=={{header|360 Assembly}}==
<
KRONECK CSECT
USING KRONECK,R13 base register
Line 244:
R DS (NR*NR)F r(nr,nr)
YREGS
END KRONECK</
{{out}}
<pre>
Line 261:
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|Action!}}==
The user must type in the monitor the following command after compilation and before running the program!<pre>SET EndProg=*</pre>
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang="action!">CARD EndProg ;required for ALLOCATE.ACT
INCLUDE "D2:ALLOCATE.ACT" ;from the Action! Tool Kit. You must type 'SET EndProg=*' from the monitor after compiling, but before running this program!
DEFINE PTR="CARD"
DEFINE MATRIX_SIZE="4"
TYPE Matrix=[
BYTE width,height
PTR data]
PTR FUNC CreateEmpty(BYTE w,h)
Matrix POINTER m
m=Alloc(MATRIX_SIZE)
m.width=w
m.height=h
m.data=Alloc(2*w*h)
RETURN (m)
PTR FUNC Create(BYTE w,h INT ARRAY a)
Matrix POINTER m
m=CreateEmpty(w,h)
MoveBlock(m.data,a,2*w*h)
RETURN (m)
PROC Destroy(Matrix POINTER m)
Free(m.data,2*m.width*m.height)
Free(m,MATRIX_SIZE)
RETURN
PTR FUNC Product(Matrix POINTER m1,m2)
Matrix POINTER m
BYTE x1,x2,y1,y2,i1,i2,i
INT ARRAY a1,a2,a
m=CreateEmpty(m1.width*m2.width,m1.height*m2.height)
a1=m1.data
a2=m2.data
a=m.data
i=0
FOR y1=0 TO m1.height-1
DO
FOR y2=0 TO m2.height-1
DO
FOR x1=0 TO m1.width-1
DO
FOR x2=0 TO m2.width-1
DO
i1=y1*m1.width+x1
i2=y2*m2.width+x2
a(i)=a1(i1)*a2(i2)
i==+1
OD
OD
OD
OD
RETURN (m)
PROC PrintMatrix(Matrix POINTER m BYTE x,y,colw)
BYTE i,j
CHAR ARRAY tmp(10)
INT ARRAY d
d=m.data
FOR j=0 TO m.height-1
DO
Position(x,y+j)
IF j=0 THEN
Put($11)
ELSEIF j=m.height-1 THEN
Put($1A)
ELSE
Put($7C)
FI
FOR i=0 TO m.width-1
DO
StrI(d(j*m.width+i),tmp)
Position(x+1+(i+1)*colw+i-tmp(0),y+j)
Print(tmp)
OD
Position(x+1+m.width*colw+(m.width-1),y+j)
IF j=0 THEN
Put($05)
ELSEIF j=m.height-1 THEN
Put($03)
ELSE
Put($7C)
FI
OD
RETURN
PROC Test1()
Matrix POINTER m1,m2,m3
INT ARRAY a1=[1 2 3 4],a2=[0 5 6 7]
m1=Create(2,2,a1)
m2=Create(2,2,a2)
m3=Product(m1,m2)
PrintMatrix(m1,0,2,1)
Position(5,3) Put('x)
PrintMatrix(m2,6,2,1)
Position(11,3) Put('=)
PrintMatrix(m3,12,1,2)
Destroy(m1)
Destroy(m2)
Destroy(m3)
RETURN
PROC Test2()
Matrix POINTER m1,m2,m3
INT ARRAY a1=[0 1 0 1 1 1 0 1 0],
a2=[1 1 1 1 1 0 0 1 1 1 1 1]
m1=Create(3,3,a1)
m2=Create(4,3,a2)
m3=Product(m1,m2)
PrintMatrix(m1,0,7,1)
Position(7,8) Put('x)
PrintMatrix(m2,8,7,1)
Position(17,8) Put('=)
PrintMatrix(m3,2,11,1)
Destroy(m1)
Destroy(m2)
Destroy(m3)
RETURN
PROC Main()
AllocInit(0)
Put(125) ;clear the screen
Test1()
Test2()
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Kronecker_product.png Screenshot from Atari 8-bit computer]
<pre>
┌ 0 5 0 10┐
┌1 2┐ ┌0 5┐ │ 6 7 12 14│
└3 4┘x└6 7┘=│ 0 15 0 20│
└18 21 24 28┘
┌0 1 0┐ ┌1 1 1 1┐
│1 1 1│x│1 0 0 1│=
└0 1 0┘ └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│
│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>
Line 267 ⟶ 434:
{{works with|Ada|Ada|83}}
<
with Ada.Integer_Text_IO;
Line 321 ⟶ 488:
Ada.Text_IO.New_Line;
end loop;
end Kronecker_Product;</
{{out}}
<pre> 0 5 0 10
Line 340 ⟶ 507:
=={{header|ALGOL 68}}==
<
# multiplies in-place the elements of the matrix a by the scaler b #
OP *:= = ( REF[,]INT a, INT b )REF[,]INT:
Line 402 ⟶ 569:
)
END
</syntaxhighlight>
{{out}}
<pre>
Line 421 ⟶ 588:
</pre>
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">kprod ← ⊃ (,/ (⍪⌿ (⊂[3 4] ∘.×)))</syntaxhighlight>
{{out}}
<pre style="line-height: normal;"> a←2 2⍴ 1 2 3 4
b←2 2⍴ 0 5 6 7
a b (a kprod b)
┌───┬───┬───────────┐
│1 2│0 5│ 0 5 0 10│
│3 4│6 7│ 6 7 12 14│
│ │ │ 0 15 0 20│
│ │ │18 21 24 28│
└───┴───┴───────────┘
x ← 3 3⍴ 0 1 0 1 1 1 0 1 0
y ← 3 4⍴ 1 1 1 1 1 0 0 1 1 1 1 1
x y (x kprod y)
┌─────┬───────┬───────────────────────┐
│0 1 0│1 1 1 1│0 0 0 0 1 1 1 1 0 0 0 0│
│1 1 1│1 0 0 1│0 0 0 0 1 0 0 1 0 0 0 0│
│0 1 0│1 1 1 1│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|AppleScript}}==
<
-- kprod :: [[Num]] -> [[Num]] -> [[Num]]
Line 609 ⟶ 805:
on unlines(xs)
intercalate(linefeed, xs)
end unlines</
{{Out}}
<pre>{0, 5, 0, 10}
Line 625 ⟶ 821:
{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|Arturo}}==
{{trans|Kotlin}}
<syntaxhighlight lang="rebol">define :matrix [X][
print: [
result: ""
loop this\X 'arr [
result: result ++ "[" ++
(join.with:" " map to [:string] arr 'item -> pad item 3) ++
"]\n"
]
return result
]
]
kroneckerProduct: function [a,b][
M: size a\X, N: size first a\X
P: size b\X, Q: size first b\X
result: to :matrix @[new array.of:M*P array.of:N*Q 0]
loop 0..dec M 'i [
loop 0..dec N 'j [
loop 0..dec P 'k [
loop 0..dec Q 'l [
result\X\[k + i * P]\[l + j * Q]: a\X\[i]\[j] * b\X\[k]\[l]
]
]
]
]
return result
]
A1: to :matrix [[[1 2] [3 4]]]
B1: to :matrix [[[0 5] [6 7]]]
print "Matrix A:"
print A1
print "Matrix B:"
print B1
print "Kronecker Product:"
print kroneckerProduct A1 B1
A2: to :matrix [[[0 1 0] [1 1 1] [0 1 0]]]
B2: to :matrix [[[1 1 1 1] [1 0 0 1] [1 1 1 1]]]
print "Matrix A:"
print A2
print "Matrix B:"
print B2
print "Kronecker Product:"
print kroneckerProduct A2 B2</syntaxhighlight>
{{out}}
<pre>Matrix A:
[ 1 2]
[ 3 4]
Matrix B:
[ 0 5]
[ 6 7]
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]
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|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">KroneckerProduct(a, b){
prod := [], r:= 1, c := 1
for i, aa in a
for j, bb in b
{
for k, x in aa
for l, y in bb
prod[R , C++] := x * y
r++, c:= 1
}
return prod
}</syntaxhighlight>
Examples:<syntaxhighlight lang="autohotkey">a := [[1, 2], [3, 4]]
b := [[0, 5], [6, 7]]
P := KroneckerProduct(a, b)
a :=[[0,1,0], [1,1,1], [0,1,0]]
b := [[1,1,1,1], [1,0,0,1], [1,1,1,1]]
Q := KroneckerProduct(a, b)
; show results
for row, obj in P
{
for col, v in obj
result .= v "`t"
result .= "`n"
}
result .= "`n"
for row, obj in Q
{
for col, v in obj
result .= v "`t"
result .= "`n"
}
MsgBox % result
return
</syntaxhighlight>
{{out}}
<pre>0 5 0 10
6 7 12 14
0 15 0 20
18 21 24 28
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|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f KRONECKER_PRODUCT.AWK
BEGIN {
Line 693 ⟶ 1,035:
return(n)
}
</syntaxhighlight>
{{out}}
<pre>
Line 733 ⟶ 1,075:
[ 0 0 0 0 1 1 1 1 0 0 0 0 ]
</pre>
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">arraybase 1
dim a(2, 2)
a[1,1] = 1 : a[1,2] = 2 : a[2,1] = 3 : a[2,2] = 4
dim b(2, 2)
b[1,1] = 0 : b[1,2] = 5 : b[2,1] = 6 : b[2,2] = 7
call kronecker_product(a, b)
print
dim x(3, 3)
x[1,1] = 0 : x[1,2] = 1 : x[1,3] = 0
x[2,1] = 1 : x[2,2] = 1 : x[2,3] = 1
x[3,1] = 0 : x[3,2] = 1 : x[3,3] = 0
dim y(3, 4)
y[1,1] = 1 : y[1,2] = 1 : y[1,3] = 1 : y[1,4] = 1
y[2,1] = 1 : y[2,2] = 0 : y[2,3] = 0 : y[2,4] = 1
y[3,1] = 1 : y[3,2] = 1 : y[3,3] = 1 : y[3,4] = 1
call kronecker_product(x, y)
end
subroutine kronecker_product(a, b)
ua1 = a[?][]
ua2 = a[][?]
ub1 = b[?][]
ub2 = b[][?]
for i = 1 to ua1
for k = 1 to ub1
print "[";
for j = 1 to ua2
for l = 1 to ub2
print rjust(a[i, j] * b[k, l], 2);
if j = ua1 and l = ub2 then
print "]"
else
print " ";
endif
next
next
next
next
end subroutine</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">print
dim a(2, 2)
a(1,1) = 1 : a(1,2) = 2 : a(2,1) = 3 : a(2,2) = 4
dim b(2, 2)
b(1,1) = 0 : b(1,2) = 5 : b(2,1) = 6 : b(2,2) = 7
kronecker_product(a, b)
print
dim a(3, 3)
a(1,1) = 0 : a(1,2) = 1 : a(1,3) = 0
a(2,1) = 1 : a(2,2) = 1 : a(2,3) = 1
a(3,1) = 0 : a(3,2) = 1 : a(3,3) = 0
dim b(3, 4)
b(1,1) = 1 : b(1,2) = 1 : b(1,3) = 1 : b(1,4) = 1
b(2,1) = 1 : b(2,2) = 0 : b(2,3) = 0 : b(2,4) = 1
b(3,1) = 1 : b(3,2) = 1 : b(3,3) = 1 : b(3,4) = 1
kronecker_product(a, b)
end
sub kronecker_product(a, b)
local i, j, k, l
ua1 = arraysize(a(), 1)
ua2 = arraysize(a(), 2)
ub1 = arraysize(b(), 1)
ub2 = arraysize(b(), 2)
for i = 1 to ua1
for k = 1 to ub1
print "[";
for j = 1 to ua2
for l = 1 to ub2
print a(i, j) * b(k, l) using "##";
if j = ua1 and l = ub2 then
print "]"
else
print " ";
endif
next
next
next
next
end sub</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
=={{header|BQN}}==
<syntaxhighlight lang="bqn">KProd ← ∾·<⎉2 ×⌜</syntaxhighlight>
or
<syntaxhighlight lang="bqn">KProd ← ∾ ×⟜<</syntaxhighlight>
Example:
<syntaxhighlight lang="bqn">l ← >⟨1‿2, 3‿4⟩
r ← >⟨0‿5, 6‿7⟩
l KProd r</syntaxhighlight>
<pre>
┌─
╵ 0 5 0 10
6 7 12 14
0 15 0 20
18 21 24 28
┘
</pre>
([https://mlochbaum.github.io/BQN/try.html#code=S1Byb2Qg4oaQIOKIvsK3POKOiTLDl+KMnAoKbCDihpAgPuKfqDHigL8yLCAz4oC/NOKfqQpyIOKGkCA+4p+oMOKAvzUsIDbigL834p+pCgpsIEtQcm9kIHIK online REPL])
=={{header|C}}==
Entering and printing matrices on the console is tedious even for matrices with 4 or more rows and columns. This implementation reads and writes the matrices from and to files. Matrices are taken as double type in order to cover as many use cases as possible.
<syntaxhighlight lang="c">
#include<stdlib.h>
#include<stdio.h>
Line 813 ⟶ 1,275:
printf("\n\n\nKronecker product of the two matrices written to %s.",output);
}
</syntaxhighlight>
Input file :
Line 852 ⟶ 1,314:
=={{header|C sharp}}==
<
using System.Collections;
using System.Collections.Generic;
Line 905 ⟶ 1,367:
}
}</
{{out}}
<pre>
Line 924 ⟶ 1,386:
=={{header|C++}}==
<
#include <iomanip>
#include <iostream>
Line 1,022 ⟶ 1,484:
test2();
return 0;
}</
{{out}}
Line 1,049 ⟶ 1,511:
=={{header|D}}==
{{Trans|Go}}
<syntaxhighlight lang="d">
import std.stdio, std.outbuffer;
Line 1,103 ⟶ 1,565:
sample(C,D);
}
</syntaxhighlight>
Output:
Line 1,141 ⟶ 1,603:
| 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|EasyLang}}==
{{trans|Lua}}
<syntaxhighlight>
func[][] krpr a[][] b[][] .
for m = 1 to len a[][]
for p = 1 to len b[][]
r[][] &= [ ]
for n = 1 to len a[m][]
for q = 1 to len b[p][]
r[$][] &= a[m][n] * b[p][q]
.
.
.
.
return r[][]
.
print krpr [ [ 1 2 ] [ 3 4 ] ] [ [ 0 5 ] [ 6 7 ] ]
print krpr [ [ 0 1 0 ] [ 1 1 1 ] [ 0 1 0 ] ] [ [ 1 1 1 1 ] [ 1 0 0 1 ] [ 1 1 1 1 ] ]
</syntaxhighlight>
{{out}}
<pre>
[
[ 0 5 0 10 ]
[ 6 7 12 14 ]
[ 0 15 0 20 ]
[ 18 21 24 28 ]
]
[
[ 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|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// Kronecker product. Nigel Galloway: August 31st., 2023
open MathNet.Numerics
open MathNet.Numerics.LinearAlgebra
let m1,m2,m3,m4=matrix [[1.0;2.0];[3.0;4.0]],matrix [[0.0;5.0];[6.0;7.0]],matrix [[0.0;1.0;0.0];[1.0;1.0;1.0];[0.0;1.0;0.0]],matrix [[1.0;1.0;1.0;1.0];[1.0;0.0;0.0;1.0];[1.0;1.0;1.0;1.0]]
printfn $"{(m1.KroneckerProduct m2).ToMatrixString()}"
printfn $"{(m3.KroneckerProduct m4).ToMatrixString()}"
</syntaxhighlight>
{{out}}
<pre>
0 5 0 10
6 7 12 14
0 15 0 20
18 21 24 28
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|Factor}}==
{{works with|Factor|0.99 2020-01-23}}
<
{ { 1 2 } { 3 4 } }
Line 1,151 ⟶ 1,680:
{ { 0 1 0 } { 1 1 1 } { 0 1 0 } }
{ { 1 1 1 1 } { 1 0 0 1 } { 1 1 1 1 } }
[ kronecker-product . ] 2bi@</
{{out}}
<pre>
Line 1,171 ⟶ 1,700:
The plan is to pass the two arrays to a subroutine, which will return their Kronecker product as a third parameter. This relies on the expanded array-handling facilities introduced with F90, especially the ability of a subroutine to allocate an array of a size of its choosing, this array being a parameter to the subroutine. Some compilers offering the "allocate" statement do not handle this! Further features of the MODULE protocol of F90 allow arrays passed to a subroutine to have their sizes ascertained in the subroutine (via function UBOUND, ''etc.'') rather than this information being supplied via the programmer coding additional parameters. This is not all to the good: multi-dimensional arrays must therefore be the actual size of their usage rather than say A(100,100) but only using the first fifty elements (in one place) and the first thirty in another. Thus, for such usage the array must be re-allocated the correct size each time, and, the speed of access to such arrays is reduced - see [[Sequence_of_primorial_primes#Fixed-size_data_aggregates]] for an example. Similarly, suppose a portion of a large array is to be passed as a parameter, as is enabled by F90 syntax such as <code>A(3:7,9:12)</code> to select a 5x4 block: those elements will ''not'' be in contiguous memory locations, as is expected by the subroutine, so they will be copied into a temporary storage area that will become the parameter and their values will be copied back on return. Copy-in copy-out, instead of by reference. With large arrays, this imposes a large overhead. A further detail of the MODULE protocol when passing arrays is that if the parameter's declaration does not specify the lower bound, it will be treated as if it were one even if the actual array is declared otherwise - see [[Array_length#Fortran]] for example.
In older-style Fortran, the arrays would be of some "surely-big-enough" size, fixed at compile time, and there would be additional parameters describing the bounds in use for each invocation. Since no array-assignment statements were available, there would be additional DO-loops to copy each block of values. In all versions of Fortran, the ordering of array elements in storage is "column-major" so that the DATA statement appears to initialise the arrays with their transpose - see [[Matrix_transposition#Fortran]] for example. As a result, the default output order for an array, if written as just <code>WRITE (6,*) A</code> will be that of the transposed order, just as with the default order of the DATA statement's data. To show the desired order of A(''row'',''column''), the array must be written with explicit specification of the order of elements, as is done by subroutine SHOW: columns across the page, rows running down the page. <
CONTAINS !For the specific problem only.
SUBROUTINE KPRODUCT(A,B,AB) !AB = Kronecker product of A and B, both two-dimensional arrays.
Line 1,227 ⟶ 1,756:
CALL SHOW (6,AB)
END</
Output:
Line 1,251 ⟶ 1,780:
</pre>
An alternative approach is not to produce the array AB at all, just calculate its elements as needed. Using the array dimension variables as defined above, <
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
Line 1,304 ⟶ 1,833:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre>[ 0 5 0 10]
Line 1,320 ⟶ 1,849:
[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|Frink}}==
The Frink library [https://frinklang.org/fsp/colorize.fsp?f=Matrix.frink Matrix.frink] contains an implementation of Kronecker product. However, the following example demonstrates calculating the Kronecker product and typesetting the equations using multidimensional arrays and no external libraries.
<syntaxhighlight lang="frink">a = [[1,2],[3,4]]
b = [[0,5],[6,7]]
println[formatProd[a,b]]
c = [[0,1,0],[1,1,1],[0,1,0]]
d = [[1,1,1,1],[1,0,0,1],[1,1,1,1]]
println[formatProd[c,d]]
formatProd[a,b] := formatTable[[[formatMatrix[a], "\u2297", formatMatrix[b], "=", formatMatrix[KroneckerProduct[a,b]]]]]
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
}</syntaxhighlight>
{{out}}
<pre>
┌ ┐
│ 0 5 0 10│
┌ ┐ ┌ ┐ │ │
│1 2│ │0 5│ │ 6 7 12 14│
│ │ ⊗ │ │ = │ │
│3 4│ │6 7│ │ 0 15 0 20│
└ ┘ └ ┘ │ │
│18 21 24 28│
└ ┘
┌ ┐
│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│
┌ ┐ ┌ ┐ │ │
│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>
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Kronecker_product}}
'''Solution'''
Kronecker product is an intrinsec operation in Fōrmulæ
'''Test case 1'''
[[File:Fōrmulæ - Kronecker product 01.png]]
[[File:Fōrmulæ - Kronecker product 02.png]]
'''Test case 2'''
[[File:Fōrmulæ - Kronecker product 03.png]]
[[File:Fōrmulæ - Kronecker product 04.png]]
A function to calculate the Kronecker product can also be written:
[[File:Fōrmulæ - Kronecker product 05.png]]
[[File:Fōrmulæ - Kronecker product 06.png]]
[[File:Fōrmulæ - Kronecker product 02.png]]
=={{header|Go}}==
===Implementation===
<
import (
Line 1,395 ⟶ 2,009:
{1, 1, 1, 1},
})
}</
{{out}}
<pre>
Line 1,430 ⟶ 2,044:
===Library go.matrix===
<
import (
Line 1,460 ⟶ 2,074:
{1, 1, 1, 1},
})))
}</
{{out}}
<pre>
Line 1,480 ⟶ 2,094:
===Library gonum/matrix===
Gonum/matrix doesn't have the Kronecker product, but here's an implementation using available methods.
<
import (
Line 1,523 ⟶ 2,137:
1, 1, 1, 1,
}))))
}</
{{out}}
<pre>
Line 1,543 ⟶ 2,157:
=={{header|Haskell}}==
<
-------------------- KRONECKER PRODUCT -------------------
kroneckerProduct :: Num a => [[a]] -> [[a]] -> [[a]]
kroneckerProduct xs ys =
fmap (`f` ys) <$> xs
>>= fmap concat . transpose
where
f = fmap . fmap . (*)
--------------------------- TEST -------------------------
main :: IO ()
main =
mapM_
print
( kroneckerProduct
[[1, 2], [3, 4]]
[[0,
)
>> putStrLn []
>> mapM_
print
( kroneckerProduct
[[0, 1, 0], [1, 1, 1], [0, 1, 0]]
[[1, 1, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]]
)</syntaxhighlight>
{{Out}}
<pre>[0,5,0,10]
[6,7,12,14]
[0,15,0,20]
Line 1,583 ⟶ 2,207:
Explicit implementation:
<
Tacit:
<
these definitions are functionally equivalent.
Line 1,593 ⟶ 2,217:
Task examples:
<
N=: (+4**)i.2 2
P=: -.0 2 6 8 e.~i.3 3
Line 1,611 ⟶ 2,235:
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</
=={{header|Java}}==
<syntaxhighlight lang="java">
package kronecker;
Line 1,760 ⟶ 2,384:
}
</syntaxhighlight>
{{Output}}
Line 1,809 ⟶ 2,433:
====Version #1.====
{{Works with|Chrome}}
<
// matkronprod.js
// Prime function:
Line 1,827 ⟶ 2,451:
}
}
</
;Required tests:
<
<!-- KronProdTest.html -->
<html><head>
Line 1,849 ⟶ 2,473:
</head><body></body>
</html>
</
{{Output}} '''Console and page results'''
<pre>
Line 1,889 ⟶ 2,513:
{{trans|PARI/GP}}
{{Works with|Chrome}}
<
// matkronprod2.js
// Prime function:
Line 1,920 ⟶ 2,544:
}
}
</
;Required tests:
<
<!-- KronProdTest2.html -->
<html><head>
Line 1,941 ⟶ 2,565:
</head><body></body>
</html>
</
{{Output}} '''Console and page results'''
Line 1,951 ⟶ 2,575:
====ES6====
{{Trans|Haskell}}
(As JavaScript is now widely embedded in non-browser contexts, a non-HTML string value is returned here, rather than
<
'use strict';
Line 2,038 ⟶ 2,662:
// MAIN ---
console.log(main());
})();</
{{Out}}
<pre>[0,5,0,10]
Line 2,057 ⟶ 2,681:
=={{header|jq}}==
In this entry, matrices are JSON arrays of numeric arrays. For the sake of illustration, the ancillary functions, though potentially independently useful, are defined here as inner functions.
<
# element-wise multiplication of a matrix by a number, "c"
Line 2,074 ⟶ 2,698:
| reduce range(0; a|length) as $i ([];
. + reduce range(0; $m) as $j ([];
addblock( b | multiply(a[$i][$j]) ) ));</
Examples:
<syntaxhighlight lang="jq">
def left: [[ 1, 2], [3, 4]];
def right: [[ 0, 5], [6, 7]];
kprod(left;right)</
{{out}}
<pre>[[0,5,0,10],[6,7,12,14],[0,15,0,20],[18,21,24,28]]</pre>
<syntaxhighlight lang="jq">
def left: [[0, 1, 0], [1, 1, 1], [0, 1, 0]];
def right: [[1, 1, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]];
kprod(left;right)</
{{out}}
<pre>[[0,0,0,0,1,1,1,1,0,0,0,0],
Line 2,103 ⟶ 2,727:
=={{header|Julia}}==
<
# Julia has a builtin kronecker product function
Line 2,121 ⟶ 2,745:
for row in 1:size(k)[1]
println(k[row,:])
end</
{{out}}
Line 2,143 ⟶ 2,767:
=={{header|Kotlin}}==
{{trans|JavaScript (Imperative #2)}}
<
typealias Matrix = Array<IntArray>
Line 2,202 ⟶ 2,826:
r = kroneckerProduct(a, b)
printAll(a, b, r)
}</
{{out}}
Line 2,243 ⟶ 2,867:
=={{header|Lua}}==
<
function prod( a, b )
print( "\nPRODUCT:" )
Line 2,263 ⟶ 2,887:
b = { { 1, 1, 1, 1 }, { 1, 0, 0, 1 }, { 1, 1, 1, 1 } }
prod( a, b )
</syntaxhighlight>
{{out}}
<pre>
Line 2,284 ⟶ 2,908:
</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
KroneckerProduct[{{0, 1, 0}, {1, 1, 1}, {0, 1, 0}},
{{1, 1, 1, 1}, {1, 0, 0, 1}, {1, 1, 1, 1}}]//MatrixForm</
{{out}}
<pre>0 5 0 10
6 7 12 14
0 15 0 20
18 21 24 28
0 0 0 0 1 1 1 1 0 0 0 0
Line 2,306 ⟶ 2,926:
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|Maxima}}==
There is a built-in function kronecker autoloaded from linearalgebra package.
Here comes a naive implementation.
<syntaxhighlight lang="maxima">
/* Function to map first, second and so on, over a list of lists without recurring corresponding built-in functions */
auxkron(n,lst):=makelist(lst[k][n],k,1,length(lst));
/* Function to subdivide a list into lists of equal lengths */
lst_equally_subdivided(lst,n):=if mod(length(lst),n)=0 then makelist(makelist(lst[i],i,j,j+n-1),j,1,length(lst)-1,n);
/* Kronecker product implementation */
alternative_kronecker(MatA,MatB):=block(auxlength:length(first(args(MatA))),makelist(i*args(MatB),i,flatten(args(MatA))),
makelist(apply(matrix,%%[i]),i,1,length(%%)),
lst_equally_subdivided(%%,auxlength),
makelist(map(args,%%[i]),i,1,length(%%)),
makelist(auxkron(j,%%),j,1,auxlength),
makelist(apply(append,%%[i]),i,1,length(%%)),
apply(matrix,%%),
transpose(%%),
args(%%),
makelist(apply(append,%%[i]),i,1,length(%%)),
apply(matrix,%%));
</syntaxhighlight>
Another implementation that does not make use of auxkron
<syntaxhighlight lang="maxima">
altern_kronecker(MatA,MatB):=block(auxlength:length(first(args(MatA))),
makelist(i*args(MatB),i,flatten(args(MatA))),
makelist(apply(matrix,%%[i]),i,1,length(%%)),
lst_equally_subdivided(%%,auxlength),
makelist(apply(addcol,%%[i]),i,1,length(%%)),
map(args,%%),
apply(append,%%),
apply(matrix,%%));
</syntaxhighlight>
{{out}}<pre>
A:matrix([0,1,0],[1,1,1],[0,1,0])$
B:matrix([1,1,1,1],[1,0,0,1],[1,1,1,1])$
C:matrix([1,2],[3,4])$
D:matrix([0,5],[6,7])$
alternative_kronecker(A,B);
/* matrix(
[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]
) */
alternative_kronecker(C,D);
/* matrix(
[0, 5, 0, 10],
[6, 7, 12, 14],
[0, 15, 0, 20],
[18, 21, 24, 28]
) */
/* altern_kronecker gives the same outputs */
</pre>
=={{header|Nim}}==
Same as Kotlin but with a generic Matrix type.
<syntaxhighlight lang="nim">import strutils
type Matrix[M, N: static Positive; T: SomeNumber] = array[M, array[N, T]]
func kroneckerProduct[M, N, P, Q: static int; T: SomeNumber](
a: Matrix[M, N, T], b: Matrix[P, Q, T]): Matrix[M * P, N * Q, T] =
for i in 0..<M:
for j in 0..<N:
for k in 0..<P:
for l in 0..<Q:
result[i * P + k][j * Q + l] = a[i][j] * b[k][l]
proc `$`(m: Matrix): string =
for row in m:
result.add '['
let length = result.len
for val in row:
result.addSep(" ", length)
result.add ($val).align(2)
result.add "]\n"
when isMainModule:
const
A1: Matrix[2, 2, int] = [[1, 2], [3, 4]]
B1: Matrix[2, 2, int] = [[0, 5], [6, 7]]
echo "Matrix A:\n", A1
echo "Matrix B:\n", B1
echo "Kronecker product:\n", kroneckerProduct(A1, B1)
const
A2: Matrix[3, 3, int] = [[0, 1, 0], [1, 1, 1], [0, 1, 0]]
B2: Matrix[3, 4, int] = [[1, 1, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]]
echo "Matrix A:\n", A2
echo "Matrix B:\n", B2
echo "Kronecker product:\n", kroneckerProduct(A2, B2)</syntaxhighlight>
{{out}}
<pre>Matrix A:
[ 1 2]
[ 3 4]
Matrix B:
[ 0 5]
[ 6 7]
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]
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|Octave}}==
<
ans =
Line 2,329 ⟶ 3,095:
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</
=={{header|ooRexx}}==
{{trans|REXX}}
<syntaxhighlight lang="oorexx">/*REXX program multiplies two matrices together, */
/* displays the matrices and the result. */
Signal On syntax
amat=2x2 1 2 3 4 /* define A matrix size and elements */
bmat=2x2 0 5 6 7 /* " B " " " " */
a=.matrix~new('A',2,2,1 2 3 4) /* create matrix A */
b=.matrix~new('B',2,2,0 5 6 7) /* create matrix B */
a~show
b~show
c=kronmat(a,b)
c~show
Say ''
Say copies('|',55)
Say ''
a=.matrix~new('A',3,3,0 1 0 1 1 1 0 1 0) /* create matrix A */
b=.matrix~new('B',3,4,1 1 1 1 1 0 0 1 1 1 1 1) /* create matrix B */
a~show
b~show
c=kronmat(a,b)
c~show
Exit
kronmat: Procedure /* compute the Kronecker Product */
Use Arg a,b
rp=0 /* row of product */
Do ra=1 To a~rows
Do rb=1 To b~rows
rp=rp+1 /* row of product */
cp=0 /* column of product */
Do ca=1 To a~cols
x=a~element(ra,ca)
Do cb=1 To b~cols
y=b~element(rb,cb)
cp=cp+1 /* column of product */
xy=x*y
c.rp.cp=xy /* element of product */
End /* cb */
End /* ca */
End /* rb */
End /* ra */
mm=''
Do i=1 To a~rows*b~rows
Do j=1 To a~rows*b~cols
mm=mm C.i.j
End /*j*/
End /*i*/
c=.matrix~new('Kronecker product',a~rows*b~rows,a~rows*b~cols,mm)
Return c
/*--------------------------------------------------------------------*/
Exit:
Say arg(1)
Exit
Syntax:
Say 'Syntax raised in line' sigl
Say sourceline(sigl)
Say 'rc='rc '('errortext(rc)')'
Say '***** There was a problem!'
Exit
::class Matrix
/********************************************************************
* Matrix is implemented as an array of rows
* where each row is an arryay of elements.
********************************************************************/
::Attribute name
::Attribute rows
::Attribute cols
::Method init
expose m name rows cols
Use Arg name,rows,cols,elements
If words(elements)<>(rows*cols) Then Do
Say 'incorrect number of elements ('words(elements)')<>'||(rows*cols)
m=.nil
Return
End
m=.array~new
Do r=1 To rows
ro=.array~new
Do c=1 To cols
Parse Var elements e elements
ro~append(e)
End
m~append(ro)
End
::Method element /* get an element's value */
expose m
Use Arg r,c
ro=m[r]
Return ro[c]
::Method set /* set an element's value and return its previous */
expose m
Use Arg r,c,new
ro=m[r]
old=ro[c]
ro[c]=new
Return old
::Method show public /* display the matrix */
expose m name rows cols
z='+'
b6=left('',6)
Say ''
Say b6 copies('-',7) 'matrix' name copies('-',7)
w=0
Do r=1 To rows
ro=m[r]
Do c=1 To cols
x=ro[c]
w=max(w,length(x))
End
End
Say b6 b6 '+'copies('-',cols*(w+1)+1)'+' /* top border */
Do r=1 To rows
ro=m[r]
line='|' right(ro[1],w) /* element of first colsumn */ /* start with long vertical bar */
Do c=2 To cols /* loop for other columns */
line=line right(ro[c],w) /* append the elements */
End /* c */
Say b6 b6 line '|' /* append a long vertical bar. */
End /* r */
Say b6 b6 '+'copies('-',cols*(w+1)+1)'+' /* bottom border */
Return</syntaxhighlight>
{{out|output}}
<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|PARI/GP}}==
=== Version #1 ===
{{Works with|PARI/GP|2.9.1 and above}}
<
\\ Print title and matrix mat rows. 4/17/16 aev
matprows(title,mat)={print(title); for(i=1,#mat[,1], print(mat[i,]))}
Line 2,362 ⟶ 3,308:
matprows("Sample 2 result:",r);
}
</
{{Output}}
Line 2,385 ⟶ 3,331:
This version is from B. Allombert. 12/12/17
{{Works with|PARI/GP|2.9.1 and above}}
<
\\ Print title and matrix mat rows. aev
matprows(title,mat)={print(title); for(i=1,#mat[,1], print(mat[i,]))}
Line 2,404 ⟶ 3,350:
matprows("Sample 2 result:",r);
}
</
{{Output}}
Line 2,426 ⟶ 3,372:
=={{header|Perl}}==
<
use strict;
use warnings;
use PDL;
sub kron {
return $x->dummy(0)
->dummy(0)
->mult($y, 0)
->clump(0, 2)
->clump(1, 2)
}
my @mats = (
[pdl([[1, 2], [3, 4]])
[pdl([[0, 1, 0], [1, 1, 1], [0, 1, 0]]),
pdl([[1, 1, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]])],
);
for my $mat (@mats) {
print "A = $mat->[0]\n";
print "B = $mat->[1]\n";
print "kron(A,B) = " . kron($mat->[0], $mat->[1]) . "\n";
}</
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">kronecker</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">ar</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">ac</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]),</span>
<span style="color: #000000;">br</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">bc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ac</span><span style="color: #0000FF;">*</span><span style="color: #000000;">bc</span><span style="color: #0000FF;">),</span><span style="color: #000000;">ar</span><span style="color: #0000FF;">*</span><span style="color: #000000;">br</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">ia</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">ar</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">i0</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">ia</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">br</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">ja</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">ac</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">j0</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">ja</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">bc</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">ib</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">br</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i0</span><span style="color: #0000FF;">+</span><span style="color: #000000;">ib</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">jb</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">bc</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">j</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">j0</span><span style="color: #0000FF;">+</span><span style="color: #000000;">jb</span>
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">ia</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ja</span><span style="color: #0000FF;">]*</span><span style="color: #000000;">b</span><span style="color: #0000FF;">[</span><span style="color: #000000;">ib</span><span style="color: #0000FF;">,</span><span style="color: #000000;">jb</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">}},</span>
<span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">}},</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}},</span>
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}}</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">kronecker</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">),{</span><span style="color: #004600;">pp_Nest</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #004600;">pp_IntFmt</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d"</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">kronecker</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">),{</span><span style="color: #004600;">pp_Nest</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 2,511 ⟶ 3,453:
{0,0,0,0,1,1,1,1,0,0,0,0}}
</pre>
=={{header|Pike}}==
{{trans|Python}}
<syntaxhighlight lang="pike">array kronecker(array matrix1, array matrix2) {
array final_list = ({});
array sub_list = ({});
int count = sizeof(matrix2);
foreach(matrix1, array elem1) {
int counter = 0;
int check = 0;
while (check < count) {
foreach(elem1, int num1) {
foreach(matrix2[counter], int num2) {
sub_list = Array.push(sub_list, num1 * num2);
}
}
counter += 1;
final_list = Array.push(final_list, sub_list);
sub_list = ({});
check += 1;
}
}
return final_list;
}
int main() {
//Sample 1
array(array(int)) a1 = ({ ({1, 2}), ({3, 4}) });
array(array(int)) b1 = ({ ({0, 5}), ({6, 7}) });
//Sample 2
array(array(int)) a2 = ({ ({0, 1, 0}), ({1, 1, 1}), ({0, 1, 0}) });
array(array(int)) b2 = ({ ({1, 1, 1, 1}), ({1, 0, 0, 1}), ({1, 1, 1, 1}) });
array result1 = kronecker(a1, b1);
for (int i = 0; i < sizeof(result1); i++) {
foreach(result1[i], int result) {
write((string)result + " ");
}
write("\n");
}
write("\n");
array result2 = kronecker(a2, b2);
for (int i = 0; i < sizeof(result2); i++) {
foreach(result2[i], int result) {
write((string)result + " ");
}
write("\n");
}
return 0;
}</syntaxhighlight>
{{out}}
<pre>0 5 0 10
6 7 12 14
0 15 0 20
18 21 24 28
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|PureBasic}}==
<
DataSection
Matrix_A_B_Dimension_Bsp1:
Line 2,620 ⟶ 3,635:
Bsp2_Matrix_A_B:
Restore Matrix_A_B_Dimension_Bsp2
Return</
{{out}}
<pre>Matrix A:
Line 2,663 ⟶ 3,678:
In Python, the numpy library has the [https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.kron.html kron] function. The following is an implementation for "bare" lists of lists.
<
# Sample 1
Line 2,703 ⟶ 3,718:
result2 = kronecker(a2, b2)
for elem in result2:
print(elem)</
Result:
Line 2,726 ⟶ 3,741:
Code:
<
r = [[1, 2], [3, 4]]
s = [[0, 5], [6, 7]]
Line 2,744 ⟶ 3,759:
# Result 2
for row in kronecker(t, u):
print(row)</
===Version 3===
Line 2,756 ⟶ 3,771:
(Versions 2 and 3 produce the same output from the same test)
<
Line 2,801 ⟶ 3,816:
# Result 2
for row in kronecker(t, u):
print(row)</
{{Out}}
<pre>[0, 5, 0, 10]
Line 2,820 ⟶ 3,835:
=={{header|R}}==
R has built-in Kronecker product operator for a and b matrices: '''a %x% b'''.
<syntaxhighlight lang="r">
## Sample using:
a <- matrix(c(1,1,1,1), ncol=2, nrow=2, byrow=TRUE);
b <- matrix(c(0,1,1,0), ncol=2, nrow=2, byrow=TRUE);
a %x% b
</syntaxhighlight>
{{Output}}
<pre>
Line 2,841 ⟶ 3,856:
Uses typed racket, since the 'math/...' libraries are much more performant in that language.
<
(require math/array
Line 2,873 ⟶ 3,888:
(matrix [[1 1 1 1]
[1 0 0 1]
[1 1 1 1]])))</
{{out}}
Line 2,891 ⟶ 3,906:
(formerly Perl 6)
{{works with|rakudo|2017.01-34-g700a077}}
<syntaxhighlight lang="raku"
return (@a X @b).map: { .[0].list X* .[1].list };
}
Line 2,900 ⟶ 3,915:
.say for kronecker_product([ <0 1 0>, <1 1 1>, <0 1 0> ],
[ <1 1 1 1>, <1 0 0 1>, <1 1 1 1>]);
</syntaxhighlight>
{{out}}
<pre>(0 5 0 10)
Line 2,919 ⟶ 3,934:
=={{header|REXX}}==
A little extra coding was added to make the matrix glyphs and elements alignment look nicer.
<
w=
Call showMat what,arows*brows||'X'||arows*bcols
Say ''
Say copies('|',55)
Say ''
Call makeMat 'A',amat /* construct A matrix from elements */
Call KronMat 'Kronecker product' /* calculate the Kronecker product */
Call showMat what,arows*brows||'X'||arows*bcols
Exit
/*--------------------------------------------------------------------*/
makemat:
Parse Arg what,size elements /*elements: e.1.1 e.1.2 - e.rows cols*/
Parse Var size rows 'X' cols
x.what.shape=rows cols
n=0
Do r=1 To rows
Do c=1 To cols
n=n+1
element=word(elements,n)
w=max(w,length(element))
x.what.r.c=element
End
End
Call showMat what,size
Return
/*--------------------------------------------------------------------*/
kronmat: /* compute the Kronecker Product */
Parse Arg what
Parse Var x.a.shape arows acols
Parse Var x.b.shape brows bcols
rp=0
Do ra=1 To arows
Do rb=1 To brows
rp=rp+1
cp=0
Do ca=1 To acols
x=x.a.ra.ca
Do cb=1 To bcols
y=x.b.rb.cb
cp=cp+1 /* column of product */
xy=x*y
x.what.rp.cp=xy /* element of product */
w=max(w,length(xy))
End /* cB */
End /* cA */
End /* rB */
End /* rA */
Return
/*--------------------------------------------------------------------*/
showmat:
Parse Arg what,size .
Parse Var size rows 'X' cols
z='+'
b6=left('',6)
Say ''
Say b6 copies('-',7) 'matrix' what copies('-',7)
Say b6 b6 '+'copies('-',cols*(w+1)+1)'+'
Do r=1 To rows
line='|' right(x.what.r.1,w) /* element of first column */ /* start with long vertical bar */
Do c=2 To cols /* loop for other columns */
line=line right(x.what.r.c,w) /* append the elements */
End /* c */
Say b6 b6 line '|' /* append a long vertical bar. */
End /* r */
Say b6 b6 '+'copies('-',cols*(w+1)+1)'+'
Return
</syntaxhighlight>
{{out|output|text= when using the default inputs:}}
<pre>
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
=={{header|Ring}}==
<
# Project : Kronecker product
Line 3,064 ⟶ 4,110:
next
next
</syntaxhighlight>
Output:
<pre>
Line 3,081 ⟶ 4,127:
[0 0 0 1 0 0 0 0 0]
[0 0 0 1 1 1 0 0 0]
</pre>
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
≪ DUP SIZE LIST→ DROP 4 ROLL DUP SIZE LIST→ DROP → b p q a m n
≪ {} m p * + n q * + 0 CON
1 m p * '''FOR''' row
1 n q * '''FOR''' col
a {} row 1 - p / IP 1 + + col 1 - q / IP 1 + + GET
b {} row 1 - p MOD 1 + + col 1 - q MOD 1 + + GET
* {} row + col + SWAP PUT
'''NEXT NEXT'''
≫ ≫ '<span style="color:blue">KROKR</span>' STO
====HP-49 version====
≪ DUP SIZE LIST→ DROP 4 PICK SIZE LIST→ DROP → a b rb cb ra ca
≪ a SIZE b SIZE * 0 CON
0 ra 1 - '''FOR''' j
0 ca 1 - '''FOR''' k
{ 1 1 }
{ } j + k +
DUP2 ADD a SWAP GET UNROT
{ } ra + ca + * ADD
SWAP b * REPL
'''NEXT NEXT'''
≫ ≫ '<span style="color:blue">KROKR</span>' STO
[[1, 2], [3, 4]] [[0, 5], [6, 7]] <span style="color:blue">KROKR</span>
[[0, 1, 0], [1, 1, 1], [0, 1, 0]] [[1, 1, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]] <span style="color:blue">KROKR</span>
{{out}}
<pre>
2: [[ 0 5 0 10 ]
[ 6 7 12 14 ]
[ 0 15 0 20 ]
[ 18 21 24 28 ]]
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 ]
[ 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|Rust}}==
<syntaxhighlight lang="rust">fn main() {
let mut a = vec![vec![1., 2.], vec![3., 4.]];
let mut b = vec![vec![0., 5.], vec![6., 7.]];
let mut a_ref = &mut a;
let a_rref = &mut a_ref;
let mut b_ref = &mut b;
let b_rref = &mut b_ref;
let ab = kronecker_product(a_rref, b_rref);
println!("Kronecker product of\n");
for i in a {
println!("{:?}", i);
}
println!("\nand\n");
for i in b {
println!("{:?}", i);
}
println!("\nis\n");
for i in ab {
println!("{:?}", i);
}
println!("\n\n");
let mut a = vec![vec![0., 1., 0.],
vec![1., 1., 1.],
vec![0., 1., 0.]];
let mut b = vec![vec![1., 1., 1., 1.],
vec![1., 0., 0., 1.],
vec![1., 1., 1., 1.]];
let mut a_ref = &mut a;
let a_rref = &mut a_ref;
let mut b_ref = &mut b;
let b_rref = &mut b_ref;
let ab = kronecker_product(a_rref, b_rref);
println!("Kronecker product of\n");
for i in a {
println!("{:?}", i);
}
println!("\nand\n");
for i in b {
println!("{:?}", i);
}
println!("\nis\n");
for i in ab {
println!("{:?}", i);
}
println!("\n\n");
}
fn kronecker_product(a: &mut Vec<Vec<f64>>, b: &mut Vec<Vec<f64>>) -> Vec<Vec<f64>> {
let m = a.len();
let n = a[0].len();
let p = b.len();
let q = b[0].len();
let rtn = m * p;
let ctn = n * q;
let mut r = zero_matrix(rtn, ctn);
for i in 0..m {
for j in 0..n {
for k in 0..p {
for l in 0..q {
r[p * i + k][q * j + l] = a[i][j] * b[k][l];
}
}
}
}
r
}
fn zero_matrix(rows: usize, cols: usize) -> Vec<Vec<f64>> {
let mut matrix = Vec::with_capacity(cols);
for _ in 0..rows {
let mut col: Vec<f64> = Vec::with_capacity(rows);
for _ in 0..cols {
col.push(0.0);
}
matrix.push(col);
}
matrix
}
</syntaxhighlight>
{{out}}
<pre>
Kronecker product of
[1.0, 2.0]
[3.0, 4.0]
and
[0.0, 5.0]
[6.0, 7.0]
is
[0.0, 5.0, 0.0, 10.0]
[6.0, 7.0, 12.0, 14.0]
[0.0, 15.0, 0.0, 20.0]
[18.0, 21.0, 24.0, 28.0]
Kronecker product of
[0.0, 1.0, 0.0]
[1.0, 1.0, 1.0]
[0.0, 1.0, 0.0]
and
[1.0, 1.0, 1.0, 1.0]
[1.0, 0.0, 0.0, 1.0]
[1.0, 1.0, 1.0, 1.0]
is
[0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]
[0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0]
[0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
[1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0]
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
[0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]
[0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0]
[0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]
</pre>
=={{header|Scala}}==
<
{
/**Get the dimensions of the input matrix*/
Line 3,125 ⟶ 4,359:
}
</syntaxhighlight>
{{out}}
<pre>
Line 3,146 ⟶ 4,380:
=={{header|Sidef}}==
{{trans|Raku}}
<
a ~X b -> map { _[0] ~X* _[1] }
}
Line 3,155 ⟶ 4,389:
say ''
kronecker_product([[0,1,0], [1,1,1], [0,1,0]],
[[1,1,1,1],[1,0,0,1], [1,1,1,1]]).each { .say }</
{{out}}
<pre>
Line 3,175 ⟶ 4,409:
=={{header|Simula}}==
<
PROCEDURE OUTMATRIX(A, W); INTEGER ARRAY A; INTEGER W;
Line 3,325 ⟶ 4,559:
END EXAMPLE 2;
END</
{{out}}
<pre>
Line 3,363 ⟶ 4,597:
In Mata, the Kronecker product is the operator '''#'''.
<
------------------------------------------------- mata (type end to exit) ----------
: a=1,2\3,4
Line 3,395 ⟶ 4,629:
9 | 0 0 0 0 1 1 1 1 0 0 0 0 |
+-------------------------------------------------------------+
: end</
=={{header|SuperCollider}}==
<
(
f = { |a, b|
Line 3,437 ⟶ 4,671:
(a *.2 b).collect(_.reduce('+++')).reduce('++')
</syntaxhighlight>
<
(
x = f.(
Line 3,454 ⟶ 4,688:
)
)
</syntaxhighlight>
Results in:
Line 3,474 ⟶ 4,708:
And:
<syntaxhighlight lang="supercollider">(
x = f.(
[
Line 3,486 ⟶ 4,720:
)
)
</syntaxhighlight>
returns:
Line 3,500 ⟶ 4,734:
=={{header|Swift}}==
<
let m = m1.count
let n = m1[0].count
Line 3,577 ⟶ 4,811:
]
printProducts(a: a2, b: b2)</
{{out}}
Line 3,613 ⟶ 4,847:
=={{header|Tcl}}==
<
proc parse_matrix {s} {
split [string trim $s] \n
Line 3,677 ⟶ 4,911:
print_matrix [kroenecker $a $b]
puts ""
}</
{{out}}
Line 3,697 ⟶ 4,931:
=={{header|VBScript}}==
<
dim a(),b(),r()
Line 3,714 ⟶ 4,948:
end sub 'kroneckerproduct
wscript.stdout.writeline text
Select Case m
Case "r": myArr = r()
For i = LBound(myArr,
text =
For j = LBound(myArr, 2) To
Select Case
wscript.stdout.writeline text
Next
End Sub 'printmatrix
sub printall(w)
printmatrix "matrix a:", "a", w
Line 3,741 ⟶ 4,977:
sub main()
xa =
3, 4)
ReDim a(LBound(xa, 1) To LBound(xa, 1) + 1, LBound(xa, 1) To LBound(xa, 1) + 1)
k
For i =
a(i, j) = xa(k): k = k + 1
Next: Next
xb = Array(0,
k =
For i = LBound(b, 1) To UBound(b, 1): For j = LBound(b, 2) To UBound(b, 2)
b(i, j) = xb(k): k = k + 1
Next: Next
kroneckerproduct
printall 3
xa =
1, 1, 1, _
0, 1, 0)
ReDim a(LBound(xa, 1) To LBound(xa, 1) + 2, LBound(xa, 1) To LBound(xa, 1) + 2)
k
For i =
a(i, j) = xa(k): k = k + 1
Next: Next
xb =
1,
k =
For i = LBound(b, 1) To UBound(b, 1): For j = LBound(b, 2) To UBound(b, 2)
b(i, j) = xb(k): k = k + 1
Next: Next
kroneckerproduct
printall 2
end sub 'main
main</
{{out}}
<pre>
Line 3,788 ⟶ 5,029:
0 15 0 20
18 21 24 28
matrix a:
0 1 0
Line 3,793 ⟶ 5,035:
0 1 0
matrix b:
1 1 1 1
1
kronecker product:
0 0 0 0 1 1 1
0 0 0 0
0 0 0 0
1 1 1
0 0 0 0 1 1 1
0 0 0 0
0 0 0 0
</pre>
Line 3,812 ⟶ 5,054:
{{libheader|Wren-matrix}}
The above module already includes a method to calculate the Kronecker product.
<
import "./matrix" for Matrix
var a1 = [
Line 3,843 ⟶ 5,085:
var m3 = Matrix.new(a3)
var m4 = Matrix.new(a4)
Fmt.mprint(m3.kronecker(m4), 2, 0)</
{{out}}
Line 3,864 ⟶ 5,106:
=={{header|zkl}}==
<
fcn kronecker(A,B){
m,n, p,q := A.rows,A.cols, B.rows,B.cols;
Line 3,870 ⟶ 5,112:
foreach i,j,k,l in (m,n,p,q){ r[p*i + k, q*j + l]=A[i,j]*B[k,l] }
r
}</
<
B:=GSL.Matrix(2,2).set(0,5, 6,7);
kronecker(A,B).format(3,0).println(); // format(width,precision)
Line 3,881 ⟶ 5,123:
1,0,0,1,
1,1,1,1);
kronecker(A,B).format(2,0).println();</
{{out}}
<pre>
|