Python Forum

Full Version: Calculate next rows based on previous values of array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone,
I am trying to calculate the value of the next array from the previous array. The calculation is done like this:
1. I have two arrays like this:
[Image: screenshot-01.png]

2. I called each of the rows in this array as depth, for example in the first row (row = 0) it will be called depth = 0 and it will be written as:
[Image: screenshot-02.png]

In this depth, it has 1 group and 16 rows.

3. For the next depth (depth = 1), it is constructed as:
[Image: screenshot-03.png]

And in this depth has 2 groups and 8 rows. The value of this depth is calculated from the previous depth (depth = 0) with the equation:
[Image: screenshot-04.png]

4. At depth = 2, it become:
[Image: screenshot-05.png]

Same as before, the value of this depth is calculated from the previous depth (depth = 1) using:
[Image: screenshot-06.png]

And it goes until depth = 4, but I don't show how to calculate the next depth because there are too many figures.
(If the other process is needed, I will update my post with calculation until depth = 4.)

Right now I am stuck at how to call the value of the previous row in this calculation and the code that I have worked out so far:

Method 1: L and B is divided by calculate the range based on node group
import numpy as np
N = 16
n = int(np.log2(N))
L0 = np.arange(N)
B0 = np.arange(N)[::-1]
L = np.zeros((n + 1, N))
B = L.copy()

L[0, :] = L0 #insert the value for L0
B[0, :] = B0 #insert the value for B0

for depth in range(n + 1):
    node = 2 ** (n - depth)
    node_group = 2 ** depth
    for i in range(node_group):
        Ln = L[depth, (node * i) : (node * i) + (node)] #divide the array of each rows into groups
        l1, l2 = Ln[::2], Ln [1::2]
        Bn = B[depth, (node * i) : (node * i) + (node - 1)]
Method 2: L and B is reshaped based on the depth, node group and node
import numpy as np
N = 16
n = int(np.log2(N))
L0 = np.arange(N)
B0 = np.arange(N)[::-1]
L = np.zeros((n + 1, N))
B = L.copy()

L[0, :] = L0 #insert the value for L0
B[0, :] = B0 #insert the value for B0

for depth in range(n + 1):
    node = 2 ** (n - depth)
    node_group = 2 ** depth
    Ln = np.reshape(L[depth], (node_group, node))
    Bn = np.reshape(B[depth], (node_group, node))
    print(Ln)
(I don't know which method is the best and I am showing this to get any opinions about it)

Please give me some advice on it, and forgive me if I am not posting it in the correct category. Because even though it uses NumPy, but I think this is only array manipulation, so I am not posting it in Data Science.