Kronecker product based fractals: Difference between revisions

Added FreeBASIC
(Added FreeBASIC)
 
(14 intermediate revisions by 7 users not shown)
Line 58:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">F kroneckerProduct(a, b)
V m = a.len
V n = a[0].len
Line 91:
V a2 = [[1, 1, 1], [1, 0, 1], [1, 1, 1]]
print(‘Sierpinski carpet fractal:’)
print(to_str(kroneckerPower(a2, 4)))</langsyntaxhighlight>
 
{{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 211 ⟶ 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 219 ⟶ 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 334 ⟶ 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 425 ⟶ 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 446 ⟶ 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 697 ⟶ 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 824 ⟶ 1,007:
 
end subroutine write2file
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,248 ⟶ 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,260 ⟶ 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,277 ⟶ 1,522:
ttl = "Sierpinski triangle fractal"; clr = '"dark-green"'; filename = "pkf3";
load "plotff.gp"
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 1,285 ⟶ 1,530:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Kronecker_product_based_fractals}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''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)
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Kronecker product based fractals 15.png|256px]]
In '''[https://formulae.org/?example=Kronecker_product_based_fractals this]''' page you can see the program(s) related to this task and their results.
 
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,351 ⟶ 1,640:
m2 := matrix{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}
m2.kroneckerPower(4).print("Sierpinski carpet")
}</langsyntaxhighlight>
 
{{out}}
Line 1,363 ⟶ 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,446 ⟶ 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,454 ⟶ 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,460 ⟶ 1,749:
ascii_art=: ' *'{~]
 
KPfractal=:dyad def 'x&KP^:y,.1'</langsyntaxhighlight>
 
Task examples (order 4, 25% font size):
Line 1,633 ⟶ 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,770 ⟶ 2,059:
 
}
</syntaxhighlight>
</lang>
 
{{Output}}
Line 2,043 ⟶ 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,092 ⟶ 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,107 ⟶ 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,121 ⟶ 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,135 ⟶ 2,424:
<canvas id="canvId" width="750" height="750" style="border: 1px outset;"></canvas>
</body></html>
</langsyntaxhighlight>
 
{{Output}}
Line 2,147 ⟶ 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,166 ⟶ 2,455:
 
M = [1 1 1; 1 0 1; 1 1 1]
matkronpow(M, 3) |> fracprint</langsyntaxhighlight>
 
{{out}}
Line 2,334 ⟶ 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,385 ⟶ 2,674:
)
printMatrix("Sierpinski carpet", kroneckerPower(a, 4))
}</langsyntaxhighlight>
 
{{out}}
Line 2,560 ⟶ 2,849:
=={{header|Lua}}==
Needs L&Ouml;VE 2D Engine
<langsyntaxhighlight lang="lua">
function prod( a, b )
local rt, l = {}, 1
Line 2,613 ⟶ 2,902:
love.graphics.draw( canvas )
end
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="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]]</langsyntaxhighlight>
{{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}}
<langsyntaxhighlight Nimlang="nim">import sequtils
 
type Matrix[T] = seq[seq[T]]
Line 2,661 ⟶ 2,977:
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)</langsyntaxhighlight>
 
{{out}}
Line 2,839 ⟶ 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,860 ⟶ 3,176:
pkronfractal(M,7,6);
}
</langsyntaxhighlight>
{{Output}}
<pre>
Line 2,875 ⟶ 3,191:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use Imager;
use Math::Cartesian::Product;
 
Line 2,913 ⟶ 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}}==
<!--<langsyntaxhighlight Phixlang="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>
Line 2,972 ⟶ 3,288:
<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>
<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>
<!--</langsyntaxhighlight>-->
Output same as Julia/Kotlin/Factor
 
Line 2,979 ⟶ 3,295:
 
'''Using only python lists'''
<langsyntaxhighlight lang="python">import os
from PIL import Image
 
Line 3,066 ⟶ 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 3,142 ⟶ 3,458:
fractal('test1', test1)
fractal('test2', test2)
fractal('test3', test3)</langsyntaxhighlight>
 
=={{header|R}}==
Line 3,152 ⟶ 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 3,195 ⟶ 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 3,226 ⟶ 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 3,253 ⟶ 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 3,259 ⟶ 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,296 ⟶ 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,315 ⟶ 3,631:
[[Bitmap/Write a PPM file| writing PPM files]].
 
<langsyntaxhighlight lang="rust">use std::{
fmt::{Debug, Display, Write},
ops::Mul,
Line 3,551 ⟶ 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,581 ⟶ 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]
 
Line 3,587 ⟶ 3,903:
{{trans|Kotlin}}
{{libheader|Wren-matrix}}
<langsyntaxhighlight ecmascriptlang="wren">import "./matrix" for Matrix
 
var kroneckerPower = Fn.new { |m, n|
Line 3,609 ⟶ 3,925:
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))</langsyntaxhighlight>
 
{{out}}
Line 3,619 ⟶ 3,935:
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,635 ⟶ 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