Kronecker product based fractals: Difference between revisions

Added FreeBASIC
m (syntax highlighting fixup automation)
(Added FreeBASIC)
 
(12 intermediate revisions by 5 users not shown)
Line 335:
SDL.Finalise;
end Kronecker_Fractals;</syntaxhighlight>
 
=={{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 551 ⟶ 612:
 
{{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}}==
Line 1,371 ⟶ 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,408 ⟶ 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}}==
Line 2,747 ⟶ 2,913:
{{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}}==
Line 3,710 ⟶ 3,903:
{{trans|Kotlin}}
{{libheader|Wren-matrix}}
<syntaxhighlight lang="ecmascriptwren">import "./matrix" for Matrix
 
var kroneckerPower = Fn.new { |m, n|
2,122

edits