Calculate next rows based on previous values of array - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Calculate next rows based on previous values of array (/thread-35616.html) |
Calculate next rows based on previous values of array - divon - Nov-23-2021 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: 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: In this depth, it has 1 group and 16 rows. 3. For the next depth (depth = 1), it is constructed as: 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: 4. At depth = 2, it become: Same as before, the value of this depth is calculated from the previous depth (depth = 1) using: 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. |