Home > Blockchain >  How to generate a whirlpool type pattern using numpy?
How to generate a whirlpool type pattern using numpy?

Time:10-05

I want to create a whirlpool pattern using numpy, but not very sure about the approach.

The Whirpool starts from 0 at the center of an array. Every layer of whirlpool is incremented by 1. The last layer of whirlpool can have any number but only between 1 to 10. Below image might help for understanding:-

enter image description here

I want to create a function that generates such whirpool patterns given the digits to be used in last layer. The last layer of whirpool should only allow numbers between 1 to 10 (inclusive). This should not be harcoding.

enter image description here

CodePudding user response:

I'll take the liberty of calling this an onion instead of a whirlpool since this matrix has concentric layers instead of a spiral structure.

import numpy as np

def makeOnion(final_layer_num):
    dim = 2 * final_layer_num   1
    matrix = []
    for row_num in range(dim):
        row = []
        for col_num in range(dim):
            row_centrality = abs(row_num - final_layer_num)
            col_centrality = abs(col_num - final_layer_num)
            row.append(max(row_centrality, col_centrality))
        matrix.append(row)
    return np.array(matrix)

If you calculate the "distance" of a row (or column) from the center, it will help with this problem, hence row_centrality and col_centrality. I just made those terms up though, maybe there are better ones. Whatever the case, the max between row & column centrality for a given entry in the matrix is equal to the layer that it is in.

CodePudding user response:

I came up with this function, where n is the digit in the outer layer:

import numpy as np

def whirlpool(n):
    m = n*2 1
    arr = np.full((m, m), n)
    for l in range(1, n 1):
        arr[l:m-l, l:m-l] = np.full((m-l*2, m-l*2), n-l)
    return arr
        
whirlpool(3)

Out:

array([[3, 3, 3, 3, 3, 3, 3],
       [3, 2, 2, 2, 2, 2, 3],
       [3, 2, 1, 1, 1, 2, 3],
       [3, 2, 1, 0, 1, 2, 3],
       [3, 2, 1, 1, 1, 2, 3],
       [3, 2, 2, 2, 2, 2, 3],
       [3, 3, 3, 3, 3, 3, 3]])
  • Related