How to resolve the algorithm Kronecker product based fractals step by step in the Sidef programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Kronecker product based fractals step by step in the Sidef programming language

Table of Contents

Problem Statement

This task is based on   Kronecker product   of two matrices. If your language has no a built-in function for such product 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   nth   order. Actually, "self-product" is a Kronecker power of the matrix. In other words: for a matrix   M   and a power   n   create a function like   matkronpow(M, n), which returns   MxMxMx...   (n   times product). A formal recurrent algorithm of creating Kronecker power of a matrix is the following:

Even just looking at the resultant matrix you can see what will be plotted. There are virtually infinitely many fractals of this type. You are limited only by your creativity and the power of your computer.

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 approaches.

These 2 fractals (each order/power 4 at least) should be built using the following 2 simple matrices:

See implementations and results below in JavaScript, PARI/GP and R languages. They have additional samples of "H", "+" and checkerboard fractals.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Kronecker product based fractals step by step in the Sidef programming language

Source code in the sidef programming language

func kronecker_product (a, b) { a ~X b -> map { _[0] ~X* _[1] } }

func kronecker_fractal(pattern, order=4) {
    var kronecker = pattern
    { kronecker = kronecker_product(kronecker, pattern) } * order
    return kronecker
}

var vicsek = [[0,1,0], [1,1,1], [0,1,0]]
var carpet = [[1,1,1], [1,0,1], [1,1,1]]
var six    = [[0,1,1,1,0], [1,0,0,0,1], [1,0,0,0,0],
              [1,1,1,1,0], [1,0,0,0,1], [1,0,0,0,1], [0,1,1,1,0]]

require("Imager")

for name,shape,order in [
    [:vicsek, vicsek, 4],
    [:carpet, carpet, 4],
    [:six,    six,    3],
] {
    var pat = kronecker_fractal(shape, order)
    var img = %O.new(xsize => pat[0].len, ysize => pat.len)
    for x,y in (^pat[0].len ~X ^pat.len) {
        img.setpixel(x => x, y => y, color => (pat[y][x] ? <255 255 32> : <16 16 16>))
    }
    img.write(file => "kronecker-#{name}-sidef.png")
}


  

You may also check:How to resolve the algorithm Levenshtein distance step by step in the LiveCode programming language
You may also check:How to resolve the algorithm Literals/Integer step by step in the AArch64 Assembly programming language
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the jq programming language
You may also check:How to resolve the algorithm Fibonacci n-step number sequences step by step in the Julia programming language
You may also check:How to resolve the algorithm Esthetic numbers step by step in the Julia programming language