Kronecker product: Difference between revisions
Content added Content deleted
Alpha bravo (talk | contribs) (Added AutoHotkey) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 49: | Line 49: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">V a1 = [[1, 2], [3, 4]] |
||
V b1 = [[0, 5], [6, 7]] |
V b1 = [[0, 5], [6, 7]] |
||
Line 82: | Line 82: | ||
V result2 = kronecker(a2, b2) |
V result2 = kronecker(a2, b2) |
||
L(elem) result2 |
L(elem) result2 |
||
print(elem)</ |
print(elem)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 103: | Line 103: | ||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |
||
< |
<syntaxhighlight lang="360asm">* Kronecker product 06/04/2017 |
||
KRONECK CSECT |
KRONECK CSECT |
||
USING KRONECK,R13 base register |
USING KRONECK,R13 base register |
||
Line 244: | Line 244: | ||
R DS (NR*NR)F r(nr,nr) |
R DS (NR*NR)F r(nr,nr) |
||
YREGS |
YREGS |
||
END KRONECK</ |
END KRONECK</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 266: | Line 266: | ||
The user must type in the monitor the following command after compilation and before running the program!<pre>SET EndProg=*</pre> |
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}} |
{{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! |
INCLUDE "D2:ALLOCATE.ACT" ;from the Action! Tool Kit. You must type 'SET EndProg=*' from the monitor after compiling, but before running this program! |
||
Line 405: | Line 405: | ||
Test1() |
Test1() |
||
Test2() |
Test2() |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Kronecker_product.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Kronecker_product.png Screenshot from Atari 8-bit computer] |
||
Line 434: | Line 434: | ||
{{works with|Ada|Ada|83}} |
{{works with|Ada|Ada|83}} |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; |
||
with Ada.Integer_Text_IO; |
with Ada.Integer_Text_IO; |
||
Line 488: | Line 488: | ||
Ada.Text_IO.New_Line; |
Ada.Text_IO.New_Line; |
||
end loop; |
end loop; |
||
end Kronecker_Product;</ |
end Kronecker_Product;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 0 5 0 10 |
<pre> 0 5 0 10 |
||
Line 507: | Line 507: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight lang="algol68">BEGIN |
||
# multiplies in-place the elements of the matrix a by the scaler b # |
# multiplies in-place the elements of the matrix a by the scaler b # |
||
OP *:= = ( REF[,]INT a, INT b )REF[,]INT: |
OP *:= = ( REF[,]INT a, INT b )REF[,]INT: |
||
Line 569: | Line 569: | ||
) |
) |
||
END |
END |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 590: | Line 590: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
< |
<syntaxhighlight lang="apl">kprod ← ⊃ (,/ (⍪⌿ (⊂[3 4] ∘.×)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 618: | Line 618: | ||
└─────┴───────┴───────────────────────┘</pre> |
└─────┴───────┴───────────────────────┘</pre> |
||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
< |
<syntaxhighlight lang="applescript">------------ KRONECKER PRODUCT OF TWO MATRICES ------------- |
||
-- kprod :: [[Num]] -> [[Num]] -> [[Num]] |
-- kprod :: [[Num]] -> [[Num]] -> [[Num]] |
||
Line 805: | Line 805: | ||
on unlines(xs) |
on unlines(xs) |
||
intercalate(linefeed, xs) |
intercalate(linefeed, xs) |
||
end unlines</ |
end unlines</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>{0, 5, 0, 10} |
<pre>{0, 5, 0, 10} |
||
Line 824: | Line 824: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="rebol">define :matrix [X][ |
||
print: [ |
print: [ |
||
result: "" |
result: "" |
||
Line 875: | Line 875: | ||
print "Kronecker Product:" |
print "Kronecker Product:" |
||
print kroneckerProduct A2 B2</ |
print kroneckerProduct A2 B2</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 915: | Line 915: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">KroneckerProduct(a, b){ |
||
prod := [], r:= 1, c := 1 |
prod := [], r:= 1, c := 1 |
||
for i, aa in a |
for i, aa in a |
||
Line 926: | Line 926: | ||
} |
} |
||
return prod |
return prod |
||
}</ |
}</syntaxhighlight> |
||
Examples:< |
Examples:<syntaxhighlight lang="autohotkey">a := [[1, 2], [3, 4]] |
||
b := [[0, 5], [6, 7]] |
b := [[0, 5], [6, 7]] |
||
P := KroneckerProduct(a, b) |
P := KroneckerProduct(a, b) |
||
Line 951: | Line 951: | ||
MsgBox % result |
MsgBox % result |
||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>0 5 0 10 |
<pre>0 5 0 10 |
||
Line 969: | Line 969: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f KRONECKER_PRODUCT.AWK |
# syntax: GAWK -f KRONECKER_PRODUCT.AWK |
||
BEGIN { |
BEGIN { |
||
Line 1,035: | Line 1,035: | ||
return(n) |
return(n) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,078: | Line 1,078: | ||
=={{header|BQN}}== |
=={{header|BQN}}== |
||
<lang |
<syntaxhighlight lang="bqn">KProd ← ∾·<⎉2 ×⌜</syntaxhighlight> |
||
or |
or |
||
<lang |
<syntaxhighlight lang="bqn">KProd ← ∾ ×⟜<</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<syntaxhighlight lang="bqn">l ← >⟨1‿2, 3‿4⟩ |
||
r ← >⟨0‿5, 6‿7⟩ |
r ← >⟨0‿5, 6‿7⟩ |
||
l KProd r</ |
l KProd r</syntaxhighlight> |
||
<pre> |
<pre> |
||
Line 1,105: | Line 1,105: | ||
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. |
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"> |
|||
<lang C> |
|||
#include<stdlib.h> |
#include<stdlib.h> |
||
#include<stdio.h> |
#include<stdio.h> |
||
Line 1,181: | Line 1,181: | ||
printf("\n\n\nKronecker product of the two matrices written to %s.",output); |
printf("\n\n\nKronecker product of the two matrices written to %s.",output); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Input file : |
Input file : |
||
Line 1,220: | Line 1,220: | ||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections; |
using System.Collections; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
Line 1,273: | Line 1,273: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,292: | Line 1,292: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <cassert> |
||
#include <iomanip> |
#include <iomanip> |
||
#include <iostream> |
#include <iostream> |
||
Line 1,390: | Line 1,390: | ||
test2(); |
test2(); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,417: | Line 1,417: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{Trans|Go}} |
{{Trans|Go}} |
||
<syntaxhighlight lang="d"> |
|||
<lang D> |
|||
import std.stdio, std.outbuffer; |
import std.stdio, std.outbuffer; |
||
Line 1,471: | Line 1,471: | ||
sample(C,D); |
sample(C,D); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
Line 1,513: | Line 1,513: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2020-01-23}} |
{{works with|Factor|0.99 2020-01-23}} |
||
< |
<syntaxhighlight lang="factor">USING: kernel math.matrices.extras prettyprint ; |
||
{ { 1 2 } { 3 4 } } |
{ { 1 2 } { 3 4 } } |
||
Line 1,519: | Line 1,519: | ||
{ { 0 1 0 } { 1 1 1 } { 0 1 0 } } |
{ { 0 1 0 } { 1 1 1 } { 0 1 0 } } |
||
{ { 1 1 1 1 } { 1 0 0 1 } { 1 1 1 1 } } |
{ { 1 1 1 1 } { 1 0 0 1 } { 1 1 1 1 } } |
||
[ kronecker-product . ] 2bi@</ |
[ kronecker-product . ] 2bi@</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,539: | Line 1,539: | ||
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. |
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. < |
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. <syntaxhighlight lang="fortran"> MODULE ARRAYMUSH !A rather small collection. |
||
CONTAINS !For the specific problem only. |
CONTAINS !For the specific problem only. |
||
SUBROUTINE KPRODUCT(A,B,AB) !AB = Kronecker product of A and B, both two-dimensional arrays. |
SUBROUTINE KPRODUCT(A,B,AB) !AB = Kronecker product of A and B, both two-dimensional arrays. |
||
Line 1,595: | Line 1,595: | ||
CALL SHOW (6,AB) |
CALL SHOW (6,AB) |
||
END</ |
END</syntaxhighlight> |
||
Output: |
Output: |
||
Line 1,619: | Line 1,619: | ||
</pre> |
</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, < |
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, <syntaxhighlight lang="fortran">AB(i,j) = A((i - 1)/RB + 1,(j - 1)/CB + 1)*B(MOD(i - 1,RB) + 1,MOD(j - 1,CB) + 1))</syntaxhighlight> with the subtracting and adding of one necessary because array indexing starts with row one and column one. With F90, they could start at zero (or any desired value) but if so, you will have to be very careful with counting. For instance, <code>DO I = 1,RA</code> must become <code>DO I = 0,RA - 1</code> and so forth. |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' version 06-04-2017 |
||
' compile with: fbc -s console |
' compile with: fbc -s console |
||
Line 1,672: | Line 1,672: | ||
Print : Print "hit any key to end program" |
Print : Print "hit any key to end program" |
||
Sleep |
Sleep |
||
End</ |
End</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[ 0 5 0 10] |
<pre>[ 0 5 0 10] |
||
Line 1,691: | Line 1,691: | ||
=={{header|Frink}}== |
=={{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. |
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]] |
b = [[0,5],[6,7]] |
||
println[formatProd[a,b]] |
println[formatProd[a,b]] |
||
Line 1,713: | Line 1,713: | ||
return n |
return n |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,756: | Line 1,756: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
===Implementation=== |
===Implementation=== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,828: | Line 1,828: | ||
{1, 1, 1, 1}, |
{1, 1, 1, 1}, |
||
}) |
}) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,863: | Line 1,863: | ||
===Library go.matrix=== |
===Library go.matrix=== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,893: | Line 1,893: | ||
{1, 1, 1, 1}, |
{1, 1, 1, 1}, |
||
}))) |
}))) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,913: | Line 1,913: | ||
===Library gonum/matrix=== |
===Library gonum/matrix=== |
||
Gonum/matrix doesn't have the Kronecker product, but here's an implementation using available methods. |
Gonum/matrix doesn't have the Kronecker product, but here's an implementation using available methods. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,956: | Line 1,956: | ||
1, 1, 1, 1, |
1, 1, 1, 1, |
||
})))) |
})))) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,976: | Line 1,976: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.List (transpose) |
||
-------------------- KRONECKER PRODUCT ------------------- |
-------------------- KRONECKER PRODUCT ------------------- |
||
Line 2,003: | Line 2,003: | ||
[[0, 1, 0], [1, 1, 1], [0, 1, 0]] |
[[0, 1, 0], [1, 1, 1], [0, 1, 0]] |
||
[[1, 1, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]] |
[[1, 1, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]] |
||
)</ |
)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>[0,5,0,10] |
<pre>[0,5,0,10] |
||
Line 2,026: | Line 2,026: | ||
Explicit implementation: |
Explicit implementation: |
||
< |
<syntaxhighlight lang="j">KP=: dyad def ',/"2 ,/ 1 3 |: x */ y'</syntaxhighlight> |
||
Tacit: |
Tacit: |
||
< |
<syntaxhighlight lang="j">KP=: 1 3 ,/"2@(,/)@|: */</syntaxhighlight> |
||
these definitions are functionally equivalent. |
these definitions are functionally equivalent. |
||
Line 2,036: | Line 2,036: | ||
Task examples: |
Task examples: |
||
< |
<syntaxhighlight lang="j"> M=: 1+i.2 2 |
||
N=: (+4**)i.2 2 |
N=: (+4**)i.2 2 |
||
P=: -.0 2 6 8 e.~i.3 3 |
P=: -.0 2 6 8 e.~i.3 3 |
||
Line 2,054: | Line 2,054: | ||
0 0 0 0 1 1 1 1 0 0 0 0 |
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 0 0 1 0 0 0 0 |
||
0 0 0 0 1 1 1 1 0 0 0 0</ |
0 0 0 0 1 1 1 1 0 0 0 0</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
<syntaxhighlight lang="java"> |
|||
<lang Java> |
|||
package kronecker; |
package kronecker; |
||
Line 2,203: | Line 2,203: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
Line 2,252: | Line 2,252: | ||
====Version #1.==== |
====Version #1.==== |
||
{{Works with|Chrome}} |
{{Works with|Chrome}} |
||
< |
<syntaxhighlight lang="javascript"> |
||
// matkronprod.js |
// matkronprod.js |
||
// Prime function: |
// Prime function: |
||
Line 2,270: | Line 2,270: | ||
} |
} |
||
} |
} |
||
</ |
</syntaxhighlight> |
||
;Required tests: |
;Required tests: |
||
< |
<syntaxhighlight lang="html"> |
||
<!-- KronProdTest.html --> |
<!-- KronProdTest.html --> |
||
<html><head> |
<html><head> |
||
Line 2,292: | Line 2,292: | ||
</head><body></body> |
</head><body></body> |
||
</html> |
</html> |
||
</ |
</syntaxhighlight> |
||
{{Output}} '''Console and page results''' |
{{Output}} '''Console and page results''' |
||
<pre> |
<pre> |
||
Line 2,332: | Line 2,332: | ||
{{trans|PARI/GP}} |
{{trans|PARI/GP}} |
||
{{Works with|Chrome}} |
{{Works with|Chrome}} |
||
< |
<syntaxhighlight lang="javascript"> |
||
// matkronprod2.js |
// matkronprod2.js |
||
// Prime function: |
// Prime function: |
||
Line 2,363: | Line 2,363: | ||
} |
} |
||
} |
} |
||
</ |
</syntaxhighlight> |
||
;Required tests: |
;Required tests: |
||
< |
<syntaxhighlight lang="html"> |
||
<!-- KronProdTest2.html --> |
<!-- KronProdTest2.html --> |
||
<html><head> |
<html><head> |
||
Line 2,384: | Line 2,384: | ||
</head><body></body> |
</head><body></body> |
||
</html> |
</html> |
||
</ |
</syntaxhighlight> |
||
{{Output}} '''Console and page results''' |
{{Output}} '''Console and page results''' |
||
Line 2,395: | Line 2,395: | ||
{{Trans|Haskell}} |
{{Trans|Haskell}} |
||
(As JavaScript is now widely embedded in non-browser contexts, a non-HTML string value is returned here, rather than making calls to methods of the Document Object Model, which is not part of JavaScript and will not always be available to a JavaScript interpreter) |
(As JavaScript is now widely embedded in non-browser contexts, a non-HTML string value is returned here, rather than making calls to methods of the Document Object Model, which is not part of JavaScript and will not always be available to a JavaScript interpreter) |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 2,481: | Line 2,481: | ||
// MAIN --- |
// MAIN --- |
||
console.log(main()); |
console.log(main()); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>[0,5,0,10] |
<pre>[0,5,0,10] |
||
Line 2,500: | Line 2,500: | ||
=={{header|jq}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="jq">def kprod(a; b): |
||
# element-wise multiplication of a matrix by a number, "c" |
# element-wise multiplication of a matrix by a number, "c" |
||
Line 2,517: | Line 2,517: | ||
| reduce range(0; a|length) as $i ([]; |
| reduce range(0; a|length) as $i ([]; |
||
. + reduce range(0; $m) as $j ([]; |
. + reduce range(0; $m) as $j ([]; |
||
addblock( b | multiply(a[$i][$j]) ) ));</ |
addblock( b | multiply(a[$i][$j]) ) ));</syntaxhighlight> |
||
Examples: |
Examples: |
||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
def left: [[ 1, 2], [3, 4]]; |
def left: [[ 1, 2], [3, 4]]; |
||
def right: [[ 0, 5], [6, 7]]; |
def right: [[ 0, 5], [6, 7]]; |
||
kprod(left;right)</ |
kprod(left;right)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[[0,5,0,10],[6,7,12,14],[0,15,0,20],[18,21,24,28]]</pre> |
<pre>[[0,5,0,10],[6,7,12,14],[0,15,0,20],[18,21,24,28]]</pre> |
||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
def left: [[0, 1, 0], [1, 1, 1], [0, 1, 0]]; |
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]]; |
def right: [[1, 1, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]]; |
||
kprod(left;right)</ |
kprod(left;right)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[[0,0,0,0,1,1,1,1,0,0,0,0], |
<pre>[[0,0,0,0,1,1,1,1,0,0,0,0], |
||
Line 2,546: | Line 2,546: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia"># v0.6 |
||
# Julia has a builtin kronecker product function |
# Julia has a builtin kronecker product function |
||
Line 2,564: | Line 2,564: | ||
for row in 1:size(k)[1] |
for row in 1:size(k)[1] |
||
println(k[row,:]) |
println(k[row,:]) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,586: | Line 2,586: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|JavaScript (Imperative #2)}} |
{{trans|JavaScript (Imperative #2)}} |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 (JVM) |
||
typealias Matrix = Array<IntArray> |
typealias Matrix = Array<IntArray> |
||
Line 2,645: | Line 2,645: | ||
r = kroneckerProduct(a, b) |
r = kroneckerProduct(a, b) |
||
printAll(a, b, r) |
printAll(a, b, r) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,686: | Line 2,686: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua"> |
||
function prod( a, b ) |
function prod( a, b ) |
||
print( "\nPRODUCT:" ) |
print( "\nPRODUCT:" ) |
||
Line 2,706: | Line 2,706: | ||
b = { { 1, 1, 1, 1 }, { 1, 0, 0, 1 }, { 1, 1, 1, 1 } } |
b = { { 1, 1, 1, 1 }, { 1, 0, 0, 1 }, { 1, 1, 1, 1 } } |
||
prod( a, b ) |
prod( a, b ) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,728: | Line 2,728: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">KroneckerProduct[{{1, 2}, {3, 4}}, {{0, 5}, {6, 7}}]//MatrixForm |
||
KroneckerProduct[{{0, 1, 0}, {1, 1, 1}, {0, 1, 0}}, |
KroneckerProduct[{{0, 1, 0}, {1, 1, 1}, {0, 1, 0}}, |
||
{{1, 1, 1, 1}, {1, 0, 0, 1}, {1, 1, 1, 1}}]//MatrixForm</ |
{{1, 1, 1, 1}, {1, 0, 0, 1}, {1, 1, 1, 1}}]//MatrixForm</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 5 0 10 |
<pre>0 5 0 10 |
||
Line 2,750: | Line 2,750: | ||
Same as Kotlin but with a generic Matrix type. |
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]] |
type Matrix[M, N: static Positive; T: SomeNumber] = array[M, array[N, T]] |
||
Line 2,788: | Line 2,788: | ||
echo "Matrix A:\n", A2 |
echo "Matrix A:\n", A2 |
||
echo "Matrix B:\n", B2 |
echo "Matrix B:\n", B2 |
||
echo "Kronecker product:\n", kroneckerProduct(A2, B2)</ |
echo "Kronecker product:\n", kroneckerProduct(A2, B2)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,827: | Line 2,827: | ||
=={{header|Octave}}== |
=={{header|Octave}}== |
||
< |
<syntaxhighlight lang="octave">>> kron([1 2; 3 4], [0 5; 6 7]) |
||
ans = |
ans = |
||
Line 2,846: | Line 2,846: | ||
0 0 0 0 1 1 1 1 0 0 0 0 |
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 0 0 1 0 0 0 0 |
||
0 0 0 0 1 1 1 1 0 0 0 0</ |
0 0 0 0 1 1 1 1 0 0 0 0</syntaxhighlight> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
=== Version #1 === |
=== Version #1 === |
||
{{Works with|PARI/GP|2.9.1 and above}} |
{{Works with|PARI/GP|2.9.1 and above}} |
||
< |
<syntaxhighlight lang="parigp"> |
||
\\ Print title and matrix mat rows. 4/17/16 aev |
\\ Print title and matrix mat rows. 4/17/16 aev |
||
matprows(title,mat)={print(title); for(i=1,#mat[,1], print(mat[i,]))} |
matprows(title,mat)={print(title); for(i=1,#mat[,1], print(mat[i,]))} |
||
Line 2,879: | Line 2,879: | ||
matprows("Sample 2 result:",r); |
matprows("Sample 2 result:",r); |
||
} |
} |
||
</ |
</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
Line 2,902: | Line 2,902: | ||
This version is from B. Allombert. 12/12/17 |
This version is from B. Allombert. 12/12/17 |
||
{{Works with|PARI/GP|2.9.1 and above}} |
{{Works with|PARI/GP|2.9.1 and above}} |
||
< |
<syntaxhighlight lang="parigp"> |
||
\\ Print title and matrix mat rows. aev |
\\ Print title and matrix mat rows. aev |
||
matprows(title,mat)={print(title); for(i=1,#mat[,1], print(mat[i,]))} |
matprows(title,mat)={print(title); for(i=1,#mat[,1], print(mat[i,]))} |
||
Line 2,921: | Line 2,921: | ||
matprows("Sample 2 result:",r); |
matprows("Sample 2 result:",r); |
||
} |
} |
||
</ |
</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
Line 2,943: | Line 2,943: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; |
use strict; |
||
use warnings; |
use warnings; |
||
Line 2,968: | Line 2,968: | ||
print "B = $mat->[1]\n"; |
print "B = $mat->[1]\n"; |
||
print "kron(A,B) = " . kron($mat->[0], $mat->[1]) . "\n"; |
print "kron(A,B) = " . kron($mat->[0], $mat->[1]) . "\n"; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{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: #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: #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> |
||
Line 3,007: | Line 3,007: | ||
<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;">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> |
<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}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,028: | Line 3,028: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="pike">array kronecker(array matrix1, array matrix2) { |
||
array final_list = ({}); |
array final_list = ({}); |
||
array sub_list = ({}); |
array sub_list = ({}); |
||
Line 3,081: | Line 3,081: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 5 0 10 |
<pre>0 5 0 10 |
||
Line 3,099: | Line 3,099: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">EnableExplicit |
||
DataSection |
DataSection |
||
Matrix_A_B_Dimension_Bsp1: |
Matrix_A_B_Dimension_Bsp1: |
||
Line 3,206: | Line 3,206: | ||
Bsp2_Matrix_A_B: |
Bsp2_Matrix_A_B: |
||
Restore Matrix_A_B_Dimension_Bsp2 |
Restore Matrix_A_B_Dimension_Bsp2 |
||
Return</ |
Return</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Matrix A: |
<pre>Matrix A: |
||
Line 3,249: | Line 3,249: | ||
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. |
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. |
||
< |
<syntaxhighlight lang="python">#!/usr/bin/env python3 |
||
# Sample 1 |
# Sample 1 |
||
Line 3,289: | Line 3,289: | ||
result2 = kronecker(a2, b2) |
result2 = kronecker(a2, b2) |
||
for elem in result2: |
for elem in result2: |
||
print(elem)</ |
print(elem)</syntaxhighlight> |
||
Result: |
Result: |
||
Line 3,312: | Line 3,312: | ||
Code: |
Code: |
||
< |
<syntaxhighlight lang="python"># Sample 1 |
||
r = [[1, 2], [3, 4]] |
r = [[1, 2], [3, 4]] |
||
s = [[0, 5], [6, 7]] |
s = [[0, 5], [6, 7]] |
||
Line 3,330: | Line 3,330: | ||
# Result 2 |
# Result 2 |
||
for row in kronecker(t, u): |
for row in kronecker(t, u): |
||
print(row)</ |
print(row)</syntaxhighlight> |
||
===Version 3=== |
===Version 3=== |
||
Line 3,342: | Line 3,342: | ||
(Versions 2 and 3 produce the same output from the same test) |
(Versions 2 and 3 produce the same output from the same test) |
||
< |
<syntaxhighlight lang="python">from itertools import (chain) |
||
Line 3,387: | Line 3,387: | ||
# Result 2 |
# Result 2 |
||
for row in kronecker(t, u): |
for row in kronecker(t, u): |
||
print(row)</ |
print(row)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>[0, 5, 0, 10] |
<pre>[0, 5, 0, 10] |
||
Line 3,406: | Line 3,406: | ||
=={{header|R}}== |
=={{header|R}}== |
||
R has built-in Kronecker product operator for a and b matrices: '''a %x% b'''. |
R has built-in Kronecker product operator for a and b matrices: '''a %x% b'''. |
||
<syntaxhighlight lang="r"> |
|||
<lang r> |
|||
## Sample using: |
## Sample using: |
||
a <- matrix(c(1,1,1,1), ncol=2, nrow=2, byrow=TRUE); |
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); |
b <- matrix(c(0,1,1,0), ncol=2, nrow=2, byrow=TRUE); |
||
a %x% b |
a %x% b |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 3,427: | Line 3,427: | ||
Uses typed racket, since the 'math/...' libraries are much more performant in that language. |
Uses typed racket, since the 'math/...' libraries are much more performant in that language. |
||
< |
<syntaxhighlight lang="racket">#lang typed/racket/base |
||
(require math/array |
(require math/array |
||
Line 3,459: | Line 3,459: | ||
(matrix [[1 1 1 1] |
(matrix [[1 1 1 1] |
||
[1 0 0 1] |
[1 0 0 1] |
||
[1 1 1 1]])))</ |
[1 1 1 1]])))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,477: | Line 3,477: | ||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{works with|rakudo|2017.01-34-g700a077}} |
{{works with|rakudo|2017.01-34-g700a077}} |
||
<lang |
<syntaxhighlight lang="raku" line>sub kronecker_product ( @a, @b ) { |
||
return (@a X @b).map: { .[0].list X* .[1].list }; |
return (@a X @b).map: { .[0].list X* .[1].list }; |
||
} |
} |
||
Line 3,486: | Line 3,486: | ||
.say for kronecker_product([ <0 1 0>, <1 1 1>, <0 1 0> ], |
.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>]); |
[ <1 1 1 1>, <1 0 0 1>, <1 1 1 1>]); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>(0 5 0 10) |
<pre>(0 5 0 10) |
||
Line 3,505: | Line 3,505: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
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. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program calculates the Kronecker product of two arbitrary size matrices. */ |
||
w= 0 /*W: max width of any matrix element. */ |
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.*/ |
||
Line 3,549: | Line 3,549: | ||
say $ $ _ '│' /*append a long vertical bar. */ |
say $ $ _ '│' /*append a long vertical bar. */ |
||
end /*r*/ |
end /*r*/ |
||
say $ $ translate(z, '└┘', "┌┐"); return /*show the bot part of matrix.*/</ |
say $ $ translate(z, '└┘', "┌┐"); return /*show the bot part of matrix.*/</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 3,604: | Line 3,604: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
# Project : Kronecker product |
# Project : Kronecker product |
||
Line 3,650: | Line 3,650: | ||
next |
next |
||
next |
next |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 3,671: | Line 3,671: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">fn main() { |
||
let mut a = vec![vec![1., 2.], vec![3., 4.]]; |
let mut a = vec![vec![1., 2.], vec![3., 4.]]; |
||
Line 3,764: | Line 3,764: | ||
matrix |
matrix |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,815: | Line 3,815: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala"> object KroneckerProduct |
||
{ |
{ |
||
/**Get the dimensions of the input matrix*/ |
/**Get the dimensions of the input matrix*/ |
||
Line 3,856: | Line 3,856: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,877: | Line 3,877: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="ruby">func kronecker_product(a, b) { |
||
a ~X b -> map { _[0] ~X* _[1] } |
a ~X b -> map { _[0] ~X* _[1] } |
||
} |
} |
||
Line 3,886: | Line 3,886: | ||
say '' |
say '' |
||
kronecker_product([[0,1,0], [1,1,1], [0,1,0]], |
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 }</ |
[[1,1,1,1],[1,0,0,1], [1,1,1,1]]).each { .say }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,906: | Line 3,906: | ||
=={{header|Simula}}== |
=={{header|Simula}}== |
||
< |
<syntaxhighlight lang="simula">BEGIN |
||
PROCEDURE OUTMATRIX(A, W); INTEGER ARRAY A; INTEGER W; |
PROCEDURE OUTMATRIX(A, W); INTEGER ARRAY A; INTEGER W; |
||
Line 4,056: | Line 4,056: | ||
END EXAMPLE 2; |
END EXAMPLE 2; |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,094: | Line 4,094: | ||
In Mata, the Kronecker product is the operator '''#'''. |
In Mata, the Kronecker product is the operator '''#'''. |
||
< |
<syntaxhighlight lang="stata">. mata |
||
------------------------------------------------- mata (type end to exit) ---------- |
------------------------------------------------- mata (type end to exit) ---------- |
||
: a=1,2\3,4 |
: a=1,2\3,4 |
||
Line 4,126: | Line 4,126: | ||
9 | 0 0 0 0 1 1 1 1 0 0 0 0 | |
9 | 0 0 0 0 1 1 1 1 0 0 0 0 | |
||
+-------------------------------------------------------------+ |
+-------------------------------------------------------------+ |
||
: end</ |
: end</syntaxhighlight> |
||
=={{header|SuperCollider}}== |
=={{header|SuperCollider}}== |
||
< |
<syntaxhighlight lang="supercollider">// the iterative version is derived from the javascript one here: |
||
( |
( |
||
f = { |a, b| |
f = { |a, b| |
||
Line 4,168: | Line 4,168: | ||
(a *.2 b).collect(_.reduce('+++')).reduce('++') |
(a *.2 b).collect(_.reduce('+++')).reduce('++') |
||
</syntaxhighlight> |
|||
</lang> |
|||
< |
<syntaxhighlight lang="supercollider">// to apply either of the two functions: |
||
( |
( |
||
x = f.( |
x = f.( |
||
Line 4,185: | Line 4,185: | ||
) |
) |
||
) |
) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Results in: |
Results in: |
||
Line 4,205: | Line 4,205: | ||
And: |
And: |
||
<syntaxhighlight lang="supercollider">( |
|||
<lang SuperCollider>( |
|||
x = f.( |
x = f.( |
||
[ |
[ |
||
Line 4,217: | Line 4,217: | ||
) |
) |
||
) |
) |
||
</syntaxhighlight> |
|||
</lang> |
|||
returns: |
returns: |
||
Line 4,231: | Line 4,231: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">func kronecker(m1: [[Int]], m2: [[Int]]) -> [[Int]] { |
||
let m = m1.count |
let m = m1.count |
||
let n = m1[0].count |
let n = m1[0].count |
||
Line 4,308: | Line 4,308: | ||
] |
] |
||
printProducts(a: a2, b: b2)</ |
printProducts(a: a2, b: b2)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,344: | Line 4,344: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl"># some helpers for matrices in nice string form: |
||
proc parse_matrix {s} { |
proc parse_matrix {s} { |
||
split [string trim $s] \n |
split [string trim $s] \n |
||
Line 4,408: | Line 4,408: | ||
print_matrix [kroenecker $a $b] |
print_matrix [kroenecker $a $b] |
||
puts "" |
puts "" |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,428: | Line 4,428: | ||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
< |
<syntaxhighlight lang="vb">' Kronecker product - 05/04/2017 |
||
dim a(),b(),r() |
dim a(),b(),r() |
||
Line 4,505: | Line 4,505: | ||
end sub 'main |
end sub 'main |
||
main</ |
main</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,543: | Line 4,543: | ||
{{libheader|Wren-matrix}} |
{{libheader|Wren-matrix}} |
||
The above module already includes a method to calculate the Kronecker product. |
The above module already includes a method to calculate the Kronecker product. |
||
< |
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt |
||
import "/matrix" for Matrix |
import "/matrix" for Matrix |
||
Line 4,574: | Line 4,574: | ||
var m3 = Matrix.new(a3) |
var m3 = Matrix.new(a3) |
||
var m4 = Matrix.new(a4) |
var m4 = Matrix.new(a4) |
||
Fmt.mprint(m3.kronecker(m4), 2, 0)</ |
Fmt.mprint(m3.kronecker(m4), 2, 0)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,595: | Line 4,595: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">var [const] GSL=Import.lib("zklGSL"); // libGSL (GNU Scientific Library) |
||
fcn kronecker(A,B){ |
fcn kronecker(A,B){ |
||
m,n, p,q := A.rows,A.cols, B.rows,B.cols; |
m,n, p,q := A.rows,A.cols, B.rows,B.cols; |
||
Line 4,601: | Line 4,601: | ||
foreach i,j,k,l in (m,n,p,q){ r[p*i + k, q*j + l]=A[i,j]*B[k,l] } |
foreach i,j,k,l in (m,n,p,q){ r[p*i + k, q*j + l]=A[i,j]*B[k,l] } |
||
r |
r |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">A:=GSL.Matrix(2,2).set(1,2, 3,4); |
||
B:=GSL.Matrix(2,2).set(0,5, 6,7); |
B:=GSL.Matrix(2,2).set(0,5, 6,7); |
||
kronecker(A,B).format(3,0).println(); // format(width,precision) |
kronecker(A,B).format(3,0).println(); // format(width,precision) |
||
Line 4,612: | Line 4,612: | ||
1,0,0,1, |
1,0,0,1, |
||
1,1,1,1); |
1,1,1,1); |
||
kronecker(A,B).format(2,0).println();</ |
kronecker(A,B).format(2,0).println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |