Kronecker product based fractals: Difference between revisions

m
syntax highlighting fixup automation
(Added solution for Action!)
m (syntax highlighting fixup automation)
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}}
Line 99:
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}}
<langsyntaxhighlight Actionlang="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!
Line 215:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Kronecker_product_based_fractals.png Screenshot from Atari 8-bit computer]
Line 221:
=={{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 334:
Window.Finalize;
SDL.Finalise;
end Kronecker_Fractals;</langsyntaxhighlight>
 
=={{header|C}}==
Line 342:
 
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 457:
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 548:
kronecker_fractal("sierpinski_triangle.png", matrix3, 8);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 558:
=={{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 569:
{ { 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 820:
=={{header|Fortran}}==
A Fortran 90 implementation. Uses dense matrices and dynamic allocation for working arrays.
<langsyntaxhighlight Fortranlang="fortran">program Kron_frac
implicit none
 
Line 947:
 
end subroutine write2file
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,383:
[[File:pkf3.png|right|thumb|Output pkf3.png]]
 
<langsyntaxhighlight lang="gnuplot">
## KPF.gp 4/8/17 aev
## Plotting 3 KPF pictures.
Line 1,400:
ttl = "Sierpinski triangle fractal"; clr = '"dark-green"'; filename = "pkf3";
load "plotff.gp"
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 1,416:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,474:
m2 := matrix{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}
m2.kroneckerPower(4).print("Sierpinski carpet")
}</langsyntaxhighlight>
 
{{out}}
Line 1,486:
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,569:
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,577:
Implementation:
 
<langsyntaxhighlight Jlang="j">V=: -.0 2 6 8 e.~i.3 3
S=: 4 ~:i.3 3
KP=: 1 3 ,/"2@(,/)@|: */
Line 1,583:
ascii_art=: ' *'{~]
 
KPfractal=:dyad def 'x&KP^:y,.1'</langsyntaxhighlight>
 
Task examples (order 4, 25% font size):
Line 1,756:
This implementation does not use sparse matrices since the powers involved do not exceed 4.
 
<syntaxhighlight lang="java">
<lang Java>
package kronecker;
 
Line 1,893:
 
}
</syntaxhighlight>
</lang>
 
{{Output}}
Line 2,166:
[[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,215:
// 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,230:
<canvas id="canvId" width="750" height="750" style="border: 1px outset;"></canvas>
</body></html>
</langsyntaxhighlight>
 
<langsyntaxhighlight lang="html">
<!-- SierpCarpetFractal.html -->
<html>
Line 2,244:
<canvas id="canvId" width="750" height="750" style="border: 1px outset;"></canvas>
</body></html>
</syntaxhighlight>
</lang>
 
<langsyntaxhighlight lang="html">
<!-- Checkerboard.html -->
<html>
Line 2,258:
<canvas id="canvId" width="750" height="750" style="border: 1px outset;"></canvas>
</body></html>
</langsyntaxhighlight>
 
{{Output}}
Line 2,270:
{{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,289:
 
M = [1 1 1; 1 0 1; 1 1 1]
matkronpow(M, 3) |> fracprint</langsyntaxhighlight>
 
{{out}}
Line 2,457:
=={{header|Kotlin}}==
This reuses code from the [[Kronecker_product#Kotlin]] task.
<langsyntaxhighlight lang="scala">// version 1.2.31
 
typealias Matrix = Array<IntArray>
Line 2,508:
)
printMatrix("Sierpinski carpet", kroneckerPower(a, 4))
}</langsyntaxhighlight>
 
{{out}}
Line 2,683:
=={{header|Lua}}==
Needs L&Ouml;VE 2D Engine
<langsyntaxhighlight lang="lua">
function prod( a, b )
local rt, l = {}, 1
Line 2,736:
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.
Line 2,750:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import sequtils
 
type Matrix[T] = seq[seq[T]]
Line 2,784:
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,962:
[[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,983:
pkronfractal(M,7,6);
}
</langsyntaxhighlight>
{{Output}}
<pre>
Line 2,998:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use Imager;
use Math::Cartesian::Product;
 
Line 3,036:
} [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 3,095:
<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 3,102:
 
'''Using only python lists'''
<langsyntaxhighlight lang="python">import os
from PIL import Image
 
Line 3,189:
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,265:
fractal('test1', test1)
fractal('test2', test2)
fractal('test3', test3)</langsyntaxhighlight>
 
=={{header|R}}==
Line 3,275:
[[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,318:
# 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,349:
{{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,376:
}
$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,382:
=={{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,419:
$= 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,438:
[[Bitmap/Write a PPM file| writing PPM files]].
 
<langsyntaxhighlight lang="rust">use std::{
fmt::{Debug, Display, Write},
ops::Mul,
Line 3,674:
)
}
</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,704:
}
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,710:
{{trans|Kotlin}}
{{libheader|Wren-matrix}}
<langsyntaxhighlight lang="ecmascript">import "/matrix" for Matrix
 
var kroneckerPower = Fn.new { |m, n|
Line 3,732:
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,742:
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,758:
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>
10,343

edits