Kronecker product based fractals: Difference between revisions

Added FreeBASIC
(Add source for Rust)
(Added FreeBASIC)
 
(21 intermediate revisions by 11 users not shown)
Line 54:
See implementations and results below in JavaScript, PARI/GP and R languages. They have additional samples of "H", "+" and checkerboard fractals.
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">F kroneckerProduct(a, b)
V m = a.len
V n = a[0].len
V p = b.len
V q = b[0].len
V result = [[0] * (n * q)] * (m * p)
L(i) 0 .< m
L(j) 0 .< n
L(k) 0 .< p
L(l) 0 .< q
result[i * p + k][j * q + l] = a[i][j] * b[k][l]
R result
 
F kroneckerPower(m, n)
V result = m
L 2..n
result = kroneckerProduct(result, m)
R result
 
F to_str(m)
V result = ‘’
L(row) m
L(val) row
result ‘’= I val == 0 {‘ ’} E ‘ *’
result ‘’= "\n"
R result
 
V a1 = [[0, 1, 0], [1, 1, 1], [0, 1, 0]]
print(‘Vicsek fractal:’)
print(to_str(kroneckerPower(a1, 4)))
print()
V a2 = [[1, 1, 1], [1, 0, 1], [1, 1, 1]]
print(‘Sierpinski carpet fractal:’)
print(to_str(kroneckerPower(a2, 4)))</syntaxhighlight>
 
{{out}}
The same as in Nim solution.
 
=={{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(w*h)
RETURN (m)
 
PTR FUNC Create(BYTE w,h BYTE ARRAY a)
Matrix POINTER m
 
m=CreateEmpty(w,h)
MoveBlock(m.data,a,w*h)
RETURN (m)
 
PROC Destroy(Matrix POINTER m)
Free(m.data,m.width*m.height)
Free(m,MATRIX_SIZE)
RETURN
 
PTR FUNC Product(Matrix POINTER m1,m2)
Matrix POINTER m
BYTE x1,x2,y1,y2
INT i1,i2,i
BYTE 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 DrawMatrix(Matrix POINTER m INT x,y)
INT i,j
BYTE ARRAY d
 
d=m.data
FOR j=0 TO m.height-1
DO
FOR i=0 TO m.width-1
DO
IF d(j*m.width+i) THEN
Plot(x+i,y+j)
FI
OD
OD
RETURN
 
PROC DrawFractal(BYTE ARRAY a BYTE w,h INT x,y BYTE n)
Matrix POINTER m1,m2,m3
BYTE i
m1=Create(w,h,a)
m2=Create(w,h,a)
FOR i=1 TO n
DO
m3=Product(m1,m2)
IF i<n THEN
Destroy(m1)
m1=m3 m3=0
FI
OD
 
DrawMatrix(m3,x,y)
 
Destroy(m1)
Destroy(m2)
Destroy(m3)
RETURN
 
PROC Main()
BYTE CH=$02FC,COLOR1=$02C5,COLOR2=$02C6
BYTE ARRAY a=[0 1 0 1 1 1 0 1 0],
b=[1 1 1 1 0 1 1 1 1],
c=[1 0 1 0 1 0 1 0 1]
 
Graphics(8+16)
AllocInit(0)
Color=1
COLOR1=$0C
COLOR2=$02
 
DrawFractal(a,3,3,12,55,3)
DrawFractal(b,3,3,120,55,3)
DrawFractal(c,3,3,226,55,3)
 
DO UNTIL CH#$FF OD
CH=$FF
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Kronecker_product_based_fractals.png Screenshot from Atari 8-bit computer]
 
=={{header|Ada}}==
{{libheader|SDLAda}} Using multiplication function from Kronecker product.
<langsyntaxhighlight Adalang="ada">with SDL.Video.Windows.Makers;
with SDL.Video.Renderers.Makers;
with SDL.Events.Events;
Line 170 ⟶ 334:
Window.Finalize;
SDL.Finalise;
end Kronecker_Fractals;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">
BEGIN # Kronecker product based fractals - translated from the Kotlin sample #
 
MODE MATRIX = FLEX[ 1 : 0, 1 : 0 ]INT;
 
PROC kronecker product = ( MATRIX a in, b in )MATRIX:
BEGIN
MATRIX a = a in[ AT 0, AT 0 ], b = b in[ AT 0, AT 0 ];
INT m = 1 UPB a + 1, n = 2 UPB a + 1;
INT p = 1 UPB b + 1, q = 2 UPB b + 1;
INT rtn = m * p, ctn = n * q;
[ 0 : rtn - 1, 0 : ctn - 1 ]INT r;
FOR i FROM 0 TO rtn - 1 DO FOR j FROM 0 TO ctn - 1 DO r[ i, j ] := 0 OD OD;
FOR i FROM 0 TO m - 1 DO
FOR j FROM 0 TO n - 1 DO
FOR k FROM 0 TO p - 1 DO
FOR l FROM 0 TO q - 1 DO
r[ p * i + k, q * j + l ] := a[ i, j ] * b[ k, l ]
OD
OD
OD
OD;
r
END # kronecker product # ;
 
PROC kronecker power = ( MATRIX a, INT n )MATRIX:
BEGIN
MATRIX pow := a;
FOR i TO n - 1 DO pow := kronecker product( pow, a ) OD;
pow
END # kronecker power # ;
 
PROC print matrix = ( STRING text, MATRIX m )VOID:
BEGIN
print( ( text, " fractal :", newline ) );
FOR i FROM 1 LWB m TO 1 UPB m DO
FOR j FROM 2 LWB m TO 2 UPB m DO
print( ( IF m[ i, j ] = 1 THEN "*" ELSE " " FI ) )
OD;
print( ( newline ) )
OD;
print( ( newline ) )
END # print matrix # ;
 
MATRIX a := MATRIX( ( 0, 1, 0 )
, ( 1, 1, 1 )
, ( 0, 1, 0 )
);
print matrix( "Vicsek", kronecker power( a, 4 ) );
 
a := MATRIX( ( 1, 1, 1 )
, ( 1, 0, 1 )
, ( 1, 1, 1 )
);
print matrix( "Sierpinski carpet", kronecker power( a, 4 ) )
END
</syntaxhighlight>
{{out}}
Same as the Kotlin sample.
 
=={{header|C}}==
Line 178 ⟶ 403:
 
Thus this implementation treats the initial matrix as a [https://en.wikipedia.org/wiki/Sparse_matrix Sparse matrix]. Doing so cuts down drastically on the required storage and number of operations. The graphical part needs the [http://www.cs.colorado.edu/~main/bgi/cs1300/ WinBGIm] library.
<syntaxhighlight lang="c">
<lang C>
#include<graphics.h>
#include<stdlib.h>
Line 293 ⟶ 518:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
{{libheader|Qt}}
This program produces image files in PNG format. The C++ code from [[Kronecker product| Kronecker product]] is reused here.
<langsyntaxhighlight lang="cpp">#include <cassert>
#include <vector>
 
Line 384 ⟶ 609:
kronecker_fractal("sierpinski_triangle.png", matrix3, 8);
return 0;
}</langsyntaxhighlight>
 
{{out}}
[[Media:Kronecker fractals sierpinski carpet.png]]<br>
See (offsite PNG images):
[[Media:Kronecker fractals sierpinski triangle.png]]<br>
[https://slack-files.com/T0CNUL56D-F016R5XAUKD-6ba59ceec5 sierpinski_carpet.png]
[[Media:Kronecker fractals vicsek.png]]
[https://slack-files.com/T0CNUL56D-F016QV726R0-4eaf54c9dc sierpinski_triangle.png]
[https://slack-files.com/T0CNUL56D-F016X53JYE8-925bb34642 vicsek.png]
 
=={{header|Factor}}==
{{works with|Factor|0.99 2020-01-23}}
<langsyntaxhighlight lang="factor">USING: io kernel math math.matrices.extras sequences ;
 
: mat-kron-pow ( m n -- m' )
Line 405 ⟶ 629:
{ { 1 1 1 } { 1 0 1 } { 1 1 1 } }
{ { 0 1 1 } { 0 1 0 } { 1 1 0 } }
[ 3 mat-kron-pow print-fractal ] tri@</langsyntaxhighlight>
Output shown at order 4 and 25% font size.
{{out}}
Line 656 ⟶ 880:
=={{header|Fortran}}==
A Fortran 90 implementation. Uses dense matrices and dynamic allocation for working arrays.
<langsyntaxhighlight Fortranlang="fortran">program Kron_frac
implicit none
 
Line 783 ⟶ 1,007:
 
end subroutine write2file
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,207 ⟶ 1,431:
 
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">Type Matrix
As Integer x
As Integer y
As Integer Ptr Dato
End Type
 
Function kroneckerProduct(a As Matrix, b As Matrix) As Matrix
Dim As Integer m = a.x, n = a.y
Dim As Integer p = b.x, q = b.y
Dim As Matrix r
r.x = m * p
r.y = n * q
r.dato = Callocate(r.x * r.y, Sizeof(Integer))
Dim As Integer i, j, k, l
For i = 0 To m - 1
For j = 0 To n - 1
For k = 0 To p - 1
For l = 0 To q - 1
r.dato[(p * i + k) * r.y + (q * j + l)] = a.dato[i * a.y + j] * b.dato[k * b.y + l]
Next
Next
Next
Next
Return r
End Function
 
Function kroneckerPower(a As Matrix, n As Integer) As Matrix
Dim As Matrix pow = a
For i As Integer = 1 To n - 1
pow = kroneckerProduct(pow, a)
Next
Return pow
End Function
 
Sub printMatrix(text As String, m As Matrix)
Dim As Integer i, j
Print text & " fractal:"
For i = 0 To m.x - 1
For j = 0 To m.y - 1
Print Iif(m.dato[i * m.y + j] = 1, "*", " ");
Next
Print
Next
Print
End Sub
 
Dim As Matrix a = Type(3, 3, Callocate(9, Sizeof(Integer)))
a.dato[0] = 0: a.dato[1] = 1: a.dato[2] = 0
a.dato[3] = 1: a.dato[4] = 1: a.dato[5] = 1
a.dato[6] = 0: a.dato[7] = 1: a.dato[8] = 0
printMatrix("Vicsek", kroneckerPower(a, 4))
 
a.dato[0] = 1: a.dato[1] = 1: a.dato[2] = 1
a.dato[3] = 1: a.dato[4] = 0: a.dato[5] = 1
a.dato[6] = 1: a.dato[7] = 1: a.dato[8] = 1
printMatrix("Sierpinski carpet", kroneckerPower(a, 4))
 
Sleep</syntaxhighlight>
{{out}}
<pre>Same as Kotlin entry.</pre>
 
=={{header|gnuplot}}==
Line 1,219 ⟶ 1,505:
[[File:pkf3.png|right|thumb|Output pkf3.png]]
 
<langsyntaxhighlight lang="gnuplot">
## KPF.gp 4/8/17 aev
## Plotting 3 KPF pictures.
Line 1,236 ⟶ 1,522:
ttl = "Sierpinski triangle fractal"; clr = '"dark-green"'; filename = "pkf3";
load "plotff.gp"
</syntaxhighlight>
</lang>
{{Output}}
<pre>
3 plotted files: pkf1.png, pkf2.png and pkf3.png.
</pre>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Kronecker_product_based_fractals}}
 
'''Solution'''
 
[[File:Fōrmulæ - Kronecker product based fractals 01.png]]
 
'''Test case 1. Vicsek fractal'''
 
Cross form
 
[[File:Fōrmulæ - Kronecker product based fractals 02.png]]
 
[[File:Fōrmulæ - Kronecker product based fractals 03.png]]
 
Saltire form
 
[[File:Fōrmulæ - Kronecker product based fractals 04.png]]
 
[[File:Fōrmulæ - Kronecker product based fractals 05.png]]
 
'''Test case 2. Sierpiński carpet fractal'''
 
[[File:Fōrmulæ - Kronecker product based fractals 06.png]]
 
[[File:Fōrmulæ - Kronecker product based fractals 07.png]]
 
'''Test case 3. Sierpiński triangle fractal'''
 
[[File:Fōrmulæ - Kronecker product based fractals 08.png]]
 
[[File:Fōrmulæ - Kronecker product based fractals 09.png]]
 
'''Test case 3. Other cases'''
 
[[File:Fōrmulæ - Kronecker product based fractals 10.png]]
 
[[File:Fōrmulæ - Kronecker product based fractals 11.png]]
 
[[File:Fōrmulæ - Kronecker product based fractals 12.png]]
 
[[File:Fōrmulæ - Kronecker product based fractals 13.png]]
 
'''Test case 4. Numbers between 0 and 1 can be used, to produce greyscale shades'''
 
[[File:Fōrmulæ - Kronecker product based fractals 14.png]]
 
(click or tap to see in real size)
 
[[File:Fōrmulæ - Kronecker product based fractals 15.png|256px]]
 
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,302 ⟶ 1,640:
m2 := matrix{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}
m2.kroneckerPower(4).print("Sierpinski carpet")
}</langsyntaxhighlight>
 
{{out}}
Line 1,314 ⟶ 1,652:
This implementation compiles to javascript that runs in the browser using the [https://github.com/ghcjs/ghcjs ghcjs compiler ] . The [https://github.com/reflex-frp/reflex-dom reflex-dom ] library is used to help with svg rendering.
 
<langsyntaxhighlight lang="haskell">{-# LANGUAGE OverloadedStrings #-}
import Reflex
import Reflex.Dom
Line 1,397 ⟶ 1,735:
elSvgns t m ma = do
(el, val) <- elDynAttrNS' (Just "http://www.w3.org/2000/svg") t m ma
return val</langsyntaxhighlight>
 
Link to live demo: https://dc25.github.io/rosettaCode__Kronecker_product_based_fractals/ ( a little slow to load ).
Line 1,405 ⟶ 1,743:
Implementation:
 
<langsyntaxhighlight Jlang="j">V=: -.0 2 6 8 e.~i.3 3
S=: 4 ~:i.3 3
KP=: 1 3 ,/"2@(,/)@|: */
Line 1,411 ⟶ 1,749:
ascii_art=: ' *'{~]
 
KPfractal=:dyad def 'x&KP^:y,.1'</langsyntaxhighlight>
 
Task examples (order 4, 25% font size):
Line 1,584 ⟶ 1,922:
This implementation does not use sparse matrices since the powers involved do not exceed 4.
 
<syntaxhighlight lang="java">
<lang Java>
package kronecker;
 
Line 1,721 ⟶ 2,059:
 
}
</syntaxhighlight>
</lang>
 
{{Output}}
Line 1,994 ⟶ 2,332:
[[File:SierpCarpetFractaljs.png|200px|right|thumb|Output SierpCarpetFractaljs.png]]
[[File:CheckbrdFractaljs.png|200px|right|thumb|Output CheckbrdFractaljs.png]]
<langsyntaxhighlight lang="javascript">
// KPF.js 6/23/16 aev
// HFJS: Plot any matrix mat (filled with 0,1)
Line 2,043 ⟶ 2,381:
// of the a and b matrices
mkp=(a,b)=>a.map(a=>b.map(b=>a.map(y=>b.map(x=>r.push(y*x)),t.push(r=[]))),t=[])&&t;
</langsyntaxhighlight>
 
;Required tests:
<langsyntaxhighlight lang="html">
<!-- VicsekFractal.html -->
<html>
Line 2,058 ⟶ 2,396:
<canvas id="canvId" width="750" height="750" style="border: 1px outset;"></canvas>
</body></html>
</langsyntaxhighlight>
 
<langsyntaxhighlight lang="html">
<!-- SierpCarpetFractal.html -->
<html>
Line 2,072 ⟶ 2,410:
<canvas id="canvId" width="750" height="750" style="border: 1px outset;"></canvas>
</body></html>
</syntaxhighlight>
</lang>
 
<langsyntaxhighlight lang="html">
<!-- Checkerboard.html -->
<html>
Line 2,086 ⟶ 2,424:
<canvas id="canvId" width="750" height="750" style="border: 1px outset;"></canvas>
</body></html>
</langsyntaxhighlight>
 
{{Output}}
Line 2,098 ⟶ 2,436:
{{works with|Julia|0.6}}
Julia has a builtin function `kron`:
<langsyntaxhighlight lang="julia">function matkronpow(M::Matrix, n::Int)
P = copy(M)
for i in 1:n P = kron(P, M) end
Line 2,117 ⟶ 2,455:
 
M = [1 1 1; 1 0 1; 1 1 1]
matkronpow(M, 3) |> fracprint</langsyntaxhighlight>
 
{{out}}
Line 2,285 ⟶ 2,623:
=={{header|Kotlin}}==
This reuses code from the [[Kronecker_product#Kotlin]] task.
<langsyntaxhighlight lang="scala">// version 1.2.31
 
typealias Matrix = Array<IntArray>
Line 2,336 ⟶ 2,674:
)
printMatrix("Sierpinski carpet", kroneckerPower(a, 4))
}</langsyntaxhighlight>
 
{{out}}
Line 2,511 ⟶ 2,849:
=={{header|Lua}}==
Needs L&Ouml;VE 2D Engine
<langsyntaxhighlight lang="lua">
function prod( a, b )
local rt, l = {}, 1
Line 2,564 ⟶ 2,902:
love.graphics.draw( canvas )
end
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">m = {{0, 1, 0}, {1, 1, 1}, {0, 1, 0}};
ArrayPlot[KroneckerProduct[m, m, m, m]]
m = {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}};
ArrayPlot[KroneckerProduct[m, m, m, m]]
m = {{0, 1, 1}, {1, 0, 1}, {1, 1, 0}};
ArrayPlot[KroneckerProduct[m, m, m, m]]</syntaxhighlight>
{{out}}
Outputs three graphical visualisations of the three 4th order products.
 
=={{header|Maxima}}==
Using function defined in Kronecker product task page. [[https://rosettacode.org/wiki/Kronecker_product#Maxima Kronecker Product]]
 
<syntaxhighlight lang="maxima">
pow_kron(matr,n):=block(MATR:copymatrix(matr),
for i from 1 thru n do MATR:altern_kronecker(matr,MATR),
MATR);
 
/* Examples (images are shown in format png)*/
/* A to generate Vicsek fractal */
/* B to generate Sierpinski carpet fractal */
A:matrix([0,1,0],[1,1,1],[0,1,0])$
B:matrix([1,1,1],[1,0,1],[1,1,1])$
 
/* Vicsek */
pow_kron(A,3)$
at(%,[0="",1="x"]);
 
/* Sierpinski carpet */
pow_kron(B,3)$
at(%,[0="",1="x"]);
</syntaxhighlight>
 
[[File:Vicsek.png|thumb|center]]
 
[[File:SierpinskiCarpet.png|thumb|center]]
 
=={{header|Nim}}==
{{trans|Kotlin}}
<syntaxhighlight lang="nim">import sequtils
 
type Matrix[T] = seq[seq[T]]
 
func kroneckerProduct[T](a, b: Matrix[T]): Matrix[T] =
result = newSeqWith(a.len * b.len, newSeq[T](a[0].len * b[0].len))
let m = a.len
let n = a[0].len
let p = b.len
let q = b[0].len
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]
 
func kroneckerPower(m: Matrix; n: int): Matrix =
result = m
for i in 2..n:
result = kroneckerProduct(result, m)
 
func `$`(m: Matrix): string =
for row in m:
for val in row:
result.add if val == 0: " " else: " *"
result.add '\n'
 
 
type B = range[0..1]
 
const A1: Matrix[B] = @[@[B 0, 1, 0], @[B 1, 1, 1], @[B 0, 1, 0]]
echo "Vicsek fractal:\n", A1.kroneckerPower(4)
echo ""
const A2: Matrix[B] = @[@[B 1, 1, 1], @[B 1, 0, 1], @[B 1, 1, 1]]
echo "Sierpinski carpet fractal:\n", A2.kroneckerPower(4)</syntaxhighlight>
 
{{out}}
<pre>Vicsek fractal:
*
* * *
*
* * *
* * * * * * * * *
* * *
*
* * *
*
* * *
* * * * * * * * *
* * *
* * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * *
* * *
* * * * * * * * *
* * *
*
* * *
*
* * *
* * * * * * * * *
* * *
*
* * *
*
* * *
* * * * * * * * *
* * *
* * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * *
* * *
* * * * * * * * *
* * *
* * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * *
* * *
* * * * * * * * *
* * *
* * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * *
* * *
* * * * * * * * *
* * *
*
* * *
*
* * *
* * * * * * * * *
* * *
*
* * *
*
* * *
* * * * * * * * *
* * *
* * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * *
* * *
* * * * * * * * *
* * *
*
* * *
*
* * *
* * * * * * * * *
* * *
*
* * *
*
 
 
Sierpinski carpet fractal:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *</pre>
 
=={{header|PARI/GP}}==
Line 2,574 ⟶ 3,155:
[[File:SierpCarpetFractalgp.png|200px|right|thumb|Output SierpCarpetFractalgp.png]]
[[File:SierpTriFractalgp.png|200px|right|thumb|Output SierpTriFractalgp.png]]
<langsyntaxhighlight lang="parigp">
\\ Build block matrix applying Kronecker product to the special matrix m
\\ (n times to itself). Then plot Kronecker fractal. 4/25/2016 aev
Line 2,595 ⟶ 3,176:
pkronfractal(M,7,6);
}
</langsyntaxhighlight>
{{Output}}
<pre>
Line 2,610 ⟶ 3,191:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use Imager;
use Math::Cartesian::Product;
 
Line 2,648 ⟶ 3,229:
} [0..@{$img[0]}-1], [0..$#img];
$png->write(file => "run/kronecker-$name-perl6.png");
}</langsyntaxhighlight>
See [https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/kronecker-vicsek-perl6.png Kronecker-Vicsek], [https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/kronecker-carpet-perl6.png Kronecker-Carpet] and [https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/kronecker-six-perl6.png Kronecker-Six] images.
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function kronecker(sequence a, b)
<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>
integer ar = length(a),
<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>
ac = length(a[1]),
<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>
br = length(b),
<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>
bc = length(b[1])
<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>
sequence res = repeat(repeat(0,ac*bc),ar*br)
<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>
for ia=1 to ar do
<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>
integer i0 = (ia-1)*br
<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>
for ja=1 to ac do
<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>
integer j0 = (ja-1)*bc
<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>
for ib=1 to br do
<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>
integer i = i0+ib
<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>
for jb=1 to bc do
<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>
integer j = j0+jb
<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>
res[i,j] = a[ia,ja]*b[ib,jb]
<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>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
return res
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
function kroneckern(sequence m, integer n)
<span style="color: #008080;">function</span> <span style="color: #000000;">kroneckern</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
sequence res = m
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">m</span>
for i=2 to n do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
res = kronecker(res,m)
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">kronecker</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
return res
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
procedure show(sequence m)
<span style="color: #008080;">procedure</span> <span style="color: #000000;">show</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">)</span>
for i=1 to length(m) do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
string s = repeat(' ',length(m[i]))
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]))</span>
for j=1 to length(s) do
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
if m[i][j] then s[j] = '#' end if
<span style="color: #008080;">if</span> <span style="color: #000000;">m</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: #008080;">then</span> <span style="color: #000000;">s</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: #008000;">'#'</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
puts(1,s&"\n")
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
puts(1,"\n")
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
 
constant vicsek = {{0,1,0},
<span style="color: #008080;">constant</span> <span style="color: #000000;">vicsek</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>
{1,1,1},
<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>
{0,1,0}},
<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>
siercp = {{1,1,1},
<span style="color: #000000;">siercp</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>
{1,0,1},
<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;">1</span><span style="color: #0000FF;">},</span>
{1,1,1}},
<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>
xxxxxx = {{0,1,1},
<span style="color: #000000;">xxxxxx</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;">1</span><span style="color: #0000FF;">},</span>
{0,1,0},
<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>
{1,1,0}}
<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;">0</span><span style="color: #0000FF;">}}</span>
 
show(kroneckern(vicsek,4))
<span style="color: #000000;">show</span><span style="color: #0000FF;">(</span><span style="color: #000000;">kroneckern</span><span style="color: #0000FF;">(</span><span style="color: #000000;">vicsek</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">))</span>
show(kroneckern(siercp,4))
<span style="color: #000000;">show</span><span style="color: #0000FF;">(</span><span style="color: #000000;">kroneckern</span><span style="color: #0000FF;">(</span><span style="color: #000000;">siercp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">))</span>
show(kroneckern(xxxxxx,4))</lang>
<span style="color: #000000;">show</span><span style="color: #0000FF;">(</span><span style="color: #000000;">kroneckern</span><span style="color: #0000FF;">(</span><span style="color: #000000;">xxxxxx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">))</span>
<!--</syntaxhighlight>-->
Output same as Julia/Kotlin/Factor
 
Line 2,712 ⟶ 3,295:
 
'''Using only python lists'''
<langsyntaxhighlight lang="python">import os
from PIL import Image
 
Line 2,799 ⟶ 3,382:
fractal('test2', test2)
fractal('test3', test3)
</syntaxhighlight>
</lang>
 
Because this is not very efficent/fast you should use scipy sparse matrices instead
<langsyntaxhighlight lang="python">import os
import numpy as np
from scipy.sparse import csc_matrix, kron
Line 2,875 ⟶ 3,458:
fractal('test1', test1)
fractal('test2', test2)
fractal('test3', test3)</langsyntaxhighlight>
 
=={{header|R}}==
Line 2,885 ⟶ 3,468:
[[File:PlusSignFR.png|200px|right|thumb|Output PlusSignFR.png]]
 
<syntaxhighlight lang="r">
<lang r>
## Generate and plot Kronecker product based fractals. aev 8/12/16
## gpKronFractal(m, n, pf, clr, ttl, dflg=0, psz=600):
Line 2,928 ⟶ 3,511:
# 0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1), ncol=8, nrow=8, byrow=TRUE);
#gpKronFractal(M, 2, "ChessBrdFractalR","black", "Chessboard Fractal, n=2")
</langsyntaxhighlight>
 
{{Output}}
Line 2,959 ⟶ 3,542:
{{works with|Rakudo|2018.09}}
 
<syntaxhighlight lang="raku" perl6line>sub kronecker-product ( @a, @b ) { (@a X @b).map: { (.[0].list X* .[1].list).Array } }
 
sub kronecker-fractal ( @pattern, $order = 4 ) {
Line 2,986 ⟶ 3,569:
}
$png.write: "kronecker-{$name}-perl6.png";
}</langsyntaxhighlight>
 
See [https://github.com/thundergnat/rc/blob/master/img/kronecker-vicsek-perl6.png Kronecker-Vicsek], [https://github.com/thundergnat/rc/blob/master/img/kronecker-carpet-perl6.png Kronecker-Carpet] and [https://github.com/thundergnat/rc/blob/master/img/kronecker-six-perl6.png Kronecker-Six] images.
Line 2,992 ⟶ 3,575:
=={{header|REXX}}==
This is a work-in-progress, this version shows the 1st order.
<langsyntaxhighlight lang="rexx">/*REXX program calculates the Kronecker product of two arbitrary size matrices. */
parse arg pGlyph . /*obtain optional argument from the CL.*/
if pGlyph=='' | pGlyph=="," then pGlyph= '█' /*Not specified? Then use the default.*/
Line 3,029 ⟶ 3,612:
$= translate($, pGlyph, 10) /*change──►plot glyph*/
say strip($, 'T') /*display line──►term*/
end /*r*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 3,048 ⟶ 3,631:
[[Bitmap/Write a PPM file| writing PPM files]].
 
<langsyntaxhighlight lang="rust">use std::{
fmt::{Debug, Display, Write},
ops::Mul,
Line 3,284 ⟶ 3,867:
)
}
</syntaxhighlight>
</lang>
 
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">func kronecker_product (a, b) { a ~X b -> map { _[0] ~X* _[1] } }
 
func kronecker_fractal(pattern, order=4) {
Line 3,314 ⟶ 3,897:
}
img.write(file => "kronecker-#{name}-sidef.png")
}</langsyntaxhighlight>
Output images: [https://github.com/trizen/rc/blob/master/img/kronecker-carpet-sidef.png Kronecker Carpet], [https://github.com/trizen/rc/blob/master/img/kronecker-vicsek-sidef.png Kronecker Vicsek] and [https://github.com/trizen/rc/blob/master/img/kronecker-six-sidef.png Kronecker Six]
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-matrix}}
<syntaxhighlight lang="wren">import "./matrix" for Matrix
 
var kroneckerPower = Fn.new { |m, n|
var pow = m.copy()
for (i in 1...n) pow = pow.kronecker(m)
return pow
}
 
var printMatrix = Fn.new { |text, m|
System.print("%(text) fractal :\n")
for (i in 0...m.numRows) {
for (j in 0...m.numCols) {
System.write((m[i][j] == 1) ? "*" : " ")
}
System.print()
}
System.print()
}
 
var m = Matrix.new([ [0, 1, 0], [1, 1, 1], [0, 1, 0] ])
printMatrix.call("Vicsek", kroneckerPower.call(m, 4))
m = Matrix.new([ [1, 1, 1], [1, 0, 1], [1, 1, 1] ])
printMatrix.call("Sierpinski carpet", kroneckerPower.call(m, 4))</syntaxhighlight>
 
{{out}}
<pre>
Same as Kotlin entry.
</pre>
 
=={{header|zkl}}==
Uses Image Magick and
the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
<langsyntaxhighlight lang="zkl">var [const] GSL=Import.lib("zklGSL"); // libGSL (GNU Scientific Library)
fcn kronecker(A,B){ //--> new Matrix
m,n, p,q := A.rows,A.cols, B.rows,B.cols;
Line 3,336 ⟶ 3,951:
R.pump(0,Ref(0).inc,Void.Filter).value)); // count 1s in fractal matrix
img.writeJPGFile(fname);
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">var [const] A=GSL.Matrix(3,3).set(0,1,0, 1,1,1, 0,1,0),
B=GSL.Matrix(3,3).set(1,1,1, 1,0,1, 1,1,1);
kfractal(A,4,"vicsek_k.jpg");
kfractal(B,4,"sierpinskiCarpet_k.jpg");</langsyntaxhighlight>
{{out}}
<pre>
2,122

edits