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

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Kronecker product based fractals step by step in the Kotlin 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 Kotlin programming language

This code implements the Kronecker product of two matrices which is a way of combining two matrices into a larger matrix. It also implements the Kronecker power of a matrix, which is the Kronecker product of the matrix with itself n times. The code then prints out the fractal patterns generated by the Kronecker powers of two different matrices.

The kroneckerProduct function takes two matrices as input and returns a new matrix that is the Kronecker product of the two input matrices. The Kronecker product of two matrices is a block matrix with the same number of rows as the first matrix and the same number of columns as the second matrix. The elements of the Kronecker product matrix are the products of the corresponding elements of the two input matrices.

The kroneckerPower function takes a matrix and an integer as input and returns the Kronecker power of the matrix to the power of the integer. The Kronecker power of a matrix is the Kronecker product of the matrix with itself n times.

The printMatrix function takes a string and a matrix as input and prints out the matrix with the given string as a label. The matrix is printed out as a series of rows, with each row separated by a newline character. The elements of the matrix are printed out as either a "*" or a " ", depending on whether the element is equal to 1 or 0, respectively.

The main function creates two matrices, a and b, and then prints out the Vicsek fractal and the Sierpinski carpet fractal. The Vicsek fractal is generated by the Kronecker power of a to the power of 4, and the Sierpinski carpet fractal is generated by the Kronecker power of b to the power of 4.

Source code in the kotlin programming language

// version 1.2.31

typealias Matrix = Array<IntArray>

fun kroneckerProduct(a: Matrix, b: Matrix): Matrix {
    val m = a.size
    val n = a[0].size
    val p = b.size
    val q = b[0].size
    val rtn = m * p
    val ctn = n * q
    val r: Matrix = Array(rtn) { IntArray(ctn) } // all elements zero by default
    for (i in 0 until m)
        for (j in 0 until n)
            for (k in 0 until p)
                for (l in 0 until q)
                    r[p * i + k][q * j + l] = a[i][j] * b[k][l]
    return r
}

fun kroneckerPower(a: Matrix, n: Int): Matrix {
    var pow = a.copyOf()
    for (i in 1 until n) pow = kroneckerProduct(pow, a)
    return pow
}

fun printMatrix(text: String, m: Matrix) {
    println("$text fractal :\n")
    for (i in 0 until m.size) {
        for (j in 0 until m[0].size) {
            print(if (m[i][j] == 1) "*" else " ")
        }
        println()
    }
    println()
}

fun main(args: Array<String>) {
    var a = arrayOf(
        intArrayOf(0, 1, 0),
        intArrayOf(1, 1, 1),
        intArrayOf(0, 1, 0)
    )
    printMatrix("Vicsek", kroneckerPower(a, 4))

    a = arrayOf(
        intArrayOf(1, 1, 1),
        intArrayOf(1, 0, 1),
        intArrayOf(1, 1, 1)
    )
    printMatrix("Sierpinski carpet", kroneckerPower(a, 4))
}


  

You may also check:How to resolve the algorithm Vampire number step by step in the Raku programming language
You may also check:How to resolve the algorithm Four bit adder step by step in the OCaml programming language
You may also check:How to resolve the algorithm Circles of given radius through two points step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Find limit of recursion step by step in the jq programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Maxima programming language