Kronecker product based fractals: Difference between revisions
(another related task + JS, GP, R samples) |
m (upgrading a little bit) |
||
Line 59: | Line 59: | ||
} |
} |
||
// Create and plot Kronecker product based fractal from matrix m (filled with 0/1) |
// Create and plot Kronecker product based fractal from matrix m (filled with 0/1) |
||
function cpmat(m,ord |
function cpmat(m,ord,color) { |
||
var kpr; |
var kpr; |
||
kpr=ckpbfmat(m,ord); |
kpr=ckpbfmat(m,ord); |
||
pmat01(kpr |
pmat01(kpr,color); |
||
} |
} |
||
// Fractal matrix "pretty" printing to document. |
// Fractal matrix "pretty" printing to document. |
||
Line 90: | Line 90: | ||
<script src="KPF.js"></script> |
<script src="KPF.js"></script> |
||
</head> |
</head> |
||
<body onload="cpmat([[0,1,0],[1,1,1],[0,1,0]],6 |
<body onload="cpmat([[0,1,0],[1,1,1],[0,1,0]],6,'navy')"> |
||
<h3>Vicsek fractal</h3> |
<h3>Vicsek fractal</h3> |
||
<a href="SierpCarpetFractal.html"> Next: Sierpinski carpet fractal</a><br /> |
<a href="SierpCarpetFractal.html"> Next: Sierpinski carpet fractal</a><br /> |
||
Line 104: | Line 104: | ||
<script src="KPF.js"></script> |
<script src="KPF.js"></script> |
||
</head> |
</head> |
||
<body onload="cpmat([[1,1,1],[1,0,1],[1,1,1]],6 |
<body onload="cpmat([[1,1,1],[1,0,1],[1,1,1]],6,'brown')"> |
||
<h3>Sierpinski carpet fractal</h3> |
<h3>Sierpinski carpet fractal</h3> |
||
<a href="VicsekFractal.html"/> Next: Vicsek fractal </a><br /> |
<a href="VicsekFractal.html"/> Next: Vicsek fractal </a><br /> |
Revision as of 02:02, 5 April 2017
You are encouraged to solve this task according to the task description, using any language you may know.
This task is based on Kronecker product of two matrices. If your
language has no a built-in function then you need to implement it first.
The essence of fractals is self-replication (at least, self-similar replications).
So, using n times self-product of the matrix (filled with 0/1) we will have a fractal of the n-th order.
Even just looking at the resultant matrix you can see it.
There are virtually infinitely many fractals of this type. You are limited only by your creativity and
the power of your computer.
- Task
Using Kronecker product implement and show two popular and well-known fractals, i.e.:
The last one ( Sierpinski carpet) is already here on RC, but built using different approach.
- Test cases
These 2 fractals (order 4 at least) should be built using the following 2 simple matrices:
|0 1 0| and |1 1 1| |1 1 1| |1 0 1| |0 1 0| |1 1 1|
- Note
- Output could be a graphical or ASCII-art representation, but if an order is set > 4 then printing is not suitable.
- The orientation and distortion of the fractal could be your language/tool specific.
See implementations and results below in JavaScript, PARI/GP and R languages.
JavaScript
Using Version #1 of Kronecker product in JavaScript.
<lang javascript> // KPF.js 6/23/16 aev // HFJS: Plot any matrix mat (filled with 0,1) function pmat01(mat, color) {
// DCLs var cvs = document.getElementById('canvId'); var ctx = cvs.getContext("2d"); var w = cvs.width; var h = cvs.height; var m = mat[0].length; var n = mat.length; // Cleaning canvas and setting plotting color ctx.fillStyle="white"; ctx.fillRect(0,0,w,h); ctx.fillStyle=color; // MAIN LOOP for(var i=0; i<m; i++) { for(var j=0; j<n; j++) { if(mat[i][j]==1) { ctx.fillRect(i,j,1,1)}; }//fend j }//fend i
}//func end // Prime functions: // Create Kronecker product based fractal matrix rm from matrix m (order=ord) function ckpbfmat(m,ord) {
var rm=m; for(var i=1; i<ord; i++) {rm=mkp(rm,m)}; //matpp2doc('R 4 ordd',rm,'*'); // ASCII "plotting" - if you wish to try. return(rm);
} // Create and plot Kronecker product based fractal from matrix m (filled with 0/1) function cpmat(m,ord,color) {
var kpr; kpr=ckpbfmat(m,ord); pmat01(kpr,color);
} // Fractal matrix "pretty" printing to document. // mat should be filled with 0 and 1; chr is a char substituting 1. function matpp2doc(title,mat,chr) {
var i,j,re=,e; var m=mat.length; var n=mat[0].length;
document.write(' '+title+':
'); for(var i=0; i<m; i++) { for(var j=0; j<n; j++) { e=' '; if(mat[i][j]==1) {e=chr}; re+=e; }//fend j document.write(' '+re+'<br />'); re=''; }//fend i document.write('
');
} // mkp function (exotic arrow function): Return the Kronecker product // 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; </lang>
- Required tests
<lang html> <html> <head>
<title>Vicsek fractal</title> <script src="KPF.js"></script>
</head> <body onload="cpmat([[0,1,0],[1,1,1],[0,1,0]],6,'navy')">
Vicsek fractal
<a href="SierpCarpetFractal.html"> Next: Sierpinski carpet fractal</a>
<canvas id="canvId" width="750" height="750" style="border: 1px outset;"></canvas>
</body></html> </lang>
<lang html> <html> <head>
<title>Sierpinski carpet fractal</title> <script src="KPF.js"></script>
</head> <body onload="cpmat([[1,1,1],[1,0,1],[1,1,1]],6,'brown')">
Sierpinski carpet fractal
<a href="VicsekFractal.html"/> Next: Vicsek fractal </a>
<canvas id="canvId" width="750" height="750" style="border: 1px outset;"></canvas>
</body></html> </lang>
- Output:
Page VicsekFractal.html with VicsekFractaljs.png Page SierpCarpetFractal.html with SierpCarpetFractaljs.png
PARI/GP
- Note
- Find iPlotmat() here on Helper Functions page.
- Find matkronprod() here on Kronecker product page.
<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 pkronfractal(m,n=2,clr)={
my(r=m); for(i=1,n, r=matkronprod(r,m)); iPlotmat(r,clr);
} \\Requireq tests: {\\ Vicsek fractal: VicsekFractalgp.png
my(M=[0,1,0;1,1,1;0,1,0]); print(" *** Vicsek fractal:"); pkronfractal(M,4,6);
} {\\ Sierpinski carpet fractal: SierpCarpetFractalgp.png
my(M=[1,1,1;1,0,1;1,1,1]); print(" *** Sierpinski carpet fractal:"); pkronfractal(M,4,5);
} </lang>
- Output:
*** Vicsek fractal: *** matrix(243x243) 3125 DOTS *** Sierpinski carpet fractal: *** matrix(243x243) 32768 DOTS
R
Generate and plot Kronecker product based fractals Note: Find plotmat() and plotv2() here on Helper Functions page.
<lang r>
- Generate and plot Kronecker product based fractals. aev 8/12/16
- gpKronFractal(m, n, pf, clr, ttl, dflg=0, psz=600):
- Where: m - initial matrix (filled with 0/1); n - order of the fractal;
- pf - plot file name (without extension); clr - color; ttl - plot title;
- dflg - writing dump file flag (0/1); psz - picture size.
gpKronFractal <- function(m, n, pf, clr, ttl, dflg=0, psz=600) {
cat(" *** START:", date(), "n=", n, "clr=", clr, "psz=", psz, "\n"); cat(" *** Plot file -", pf, "\n"); r <- m; for(i in 1:n) {r = r%x%m}; plotmat(r, pf, clr, ttl, dflg, psz); cat(" *** END:", date(), "\n");
}
- Required tests:
- 1. Vicsek Fractal
M <- matrix(c(0,1,0,1,1,1,0,1,0), ncol=3, nrow=3, byrow=TRUE); gpKronFractal(M, 4, "VicsekFractalR","red", "Vicsek Fractal n=4")
- 2. Sierpinski carpet fractal
M <- matrix(c(1,1,1,1,0,1,1,1,1), ncol=3, nrow=3, byrow=TRUE); gpKronFractal(M, 4, "SierpinskiCarpetFR", "maroon", "Sierpinski carpet fractal n=4")
- Also, try these 2. I bet you never seen them before.
- 3. Wider Sierpinski carpet fractal (a.k.a. Sierpinski carpet mutant)
- Note: If your computer is not super fast it could take a lot of time.
- Use dump flag = 1, to save generated fractal.
- M <- matrix(c(1,1,1,1,1,1,0,0,0,1,1,0,0,0,1,1,0,0,0,1,1,1,1,1,1), ncol=5,
- +nrow=5, byrow=TRUE);
- gpKronFractal(M, 4, "SierpinskiCarpetFw", "brown", "Wider Sierpinski carpet fractal n=4", 1)
- 4. "H" fractal (Try all other letters in alphabet...)
- M <- matrix(c(1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,1,
- +0,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1), ncol=7, nrow=7, byrow=TRUE);
- gpKronFractal(M, 3, "HFR", "maroon", "'H' fractal n=3", 1)
</lang>
- Output:
> M <- matrix(c(0,1,0,1,1,1,0,1,0), ncol=3, nrow=3, byrow=TRUE); > gpKronFractal(M, 4, "VicsekFractalR", "red", "Vicsek Fractal n=4") *** START: Mon Aug 29 16:14:14 2016 n= 4 clr= red *** Plot file - VicsekFractalR *** Matrix( 243 x 243 ) 3125 DOTS *** END: Mon Aug 29 16:14:14 2016 > M <- matrix(c(1,1,1,1,0,1,1,1,1), ncol=3, nrow=3, byrow=TRUE); > gpKronFractal(M, 4, "SierpinskiCarpetFR", "maroon", "Sierpinski carpet fractal n=4") *** START: Mon Aug 29 16:16:14 2016 n= 4 clr= maroon *** Plot file - SierpinskiCarpetFR *** Matrix( 243 x 243 ) 32768 DOTS *** END: Mon Aug 29 16:16:32 2016