Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a whirlpool type pattern using numpy?

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.

like image 343
Coding99 Avatar asked Oct 26 '25 05:10

Coding99


2 Answers

Very short and concise:

def whirlpool(n):
    center = np.abs(np.arange(-n, n + 1))
    return np.maximum.outer(center, center)
like image 181
Chrysophylaxs Avatar answered Oct 28 '25 20:10

Chrysophylaxs


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.

like image 36
TimH Avatar answered Oct 28 '25 20:10

TimH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!