Kronecker product based fractals: Difference between revisions

m
 
(12 intermediate revisions by 6 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 554 ⟶ 615:
[[Media:Kronecker fractals sierpinski triangle.png]]<br>
[[Media:Kronecker fractals vicsek.png]]
 
=={{header|EasyLang}}==
[https://easylang.online/show/#cod=bVBBbsMwDLv7FTwMw1ZgWTxgx7zE8KE1HKxYYrtKura/HyQnq5314igkRUnsz8EZayy+KRH2Uh7kbRSAPhJGdNCYIwYfsoKZhUwleShIACQ+zx0MCpS7Qm05Vm2L5lQ7p61GBjwtE8QjWOyy8lRLG/WvlK885OczhbysalRfBBIvSyJhkcoP32OgYfNVvOuR9wyqPLsrE6U1mEcTE0WH6StekHLwWeUGvycuOCzOIK0mk+No2hbveSZvQOUGDLgSAHBFhxeHN+hX7DC5O3Njhh4xY/zxuOJ2R449kiFrHB+oq5DJu5lXKw02aTdKzszJ5hC1BMlV+1dlzOJTTYP3CR/btlbktjJYMW5Tvw== Run it]
<syntaxhighlight>
func[][] krpr a[][] b[][] .
for m = 1 to len a[][]
for p = 1 to len b[][]
r[][] &= [ ]
for n = 1 to len a[m][]
for q = 1 to len b[p][]
r[$][] &= a[m][n] * b[p][q]
.
.
.
.
return r[][]
.
func[][] krpow a[][] n .
r[][] = [ [ 1 ] ]
for i to n
r[][] = krpr a[][] r[][]
.
return r[][]
.
proc show p[][] . .
clear
n = len p[][]
sc = 100 / n
for r to n
for c to n
x = (c - 1) * sc
y = (r - 1) * sc
move x y
if p[r][c] = 1
rect sc sc
.
.
.
.
show krpow [ [ 1 1 1 ] [ 1 0 1 ] [ 1 1 1 ] ] 5
sleep 2
show krpow [ [ 0 1 0 ] [ 1 1 1 ] [ 0 1 0 ] ] 5
</syntaxhighlight>
 
=={{header|Factor}}==
Line 1,370 ⟶ 1,474:
 
</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,574:
 
{{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}}==
Line 2,742 ⟶ 2,956:
{{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,705 ⟶ 3,946:
{{trans|Kotlin}}
{{libheader|Wren-matrix}}
<syntaxhighlight lang="ecmascriptwren">import "./matrix" for Matrix
 
var kroneckerPower = Fn.new { |m, n|
2,083

edits