Realization of a tensor with for loops - 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: Realization of a tensor with for loops (/thread-33564.html) |
Realization of a tensor with for loops - Elda46 - May-06-2021 Hi all I am new of Python and I need some help in the development of a tensor through these lines: index_0=0#i index_1=0#j index_2=0#k index_3=0#w DD = np.zeros((100,100,100,100)) for i in Lunghezza_m: for j in Larghezza_m: for k in Parameter2: for w in Velocità_nodi: DD[index_0][index_1][index_2][index_3]=round(1.025*i*j*Immersa_m[index_1][index_2]*Cb[index_0][index_3],2) index_3+=1 index_3=0 index_2+=1 index_3=0 index_2=0 index_1+=1 index_3=0 index_2=0 index_1=0 index_0+=1Where Immersa_m and Cb are two rectangular matrices, instead Lunghezza_m, Larghezza_m, Parameter2 and Velocità_nodi are arrays. Obviously DD will be a tensor. How could I obtain it? I also tried something like that but with no success: the error is: IndexError Traceback (most recent call last) <ipython-input-39-71f7f1b76435> in <module> 60 for k in Parameter2: 61 for w in Velocità_nodi: ---> 62 DD[index_0][index_1][index_2][index_3]=round(1.025*i*j*Immersa_m[index_1][index_2]*Cb[index_0][index_3],2) 63 index_3+=1 64 index_3=0 IndexError: invalid index to scalar variable.Thank you for the help!! RE: Realization of a tensor with for loops - Gribouillis - May-07-2021 Why loop over k and w if they are not used in the formula?
RE: Realization of a tensor with for loops - Elda46 - May-07-2021 (May-07-2021, 12:19 PM)Gribouillis Wrote: Why loop over well you are actually rigth but i can not use, for example, Immersa_m[k][w] because k and w are not integer numbers but they are actually the values of, respectively, Parameter2 and Velocità_nodi. RE: Realization of a tensor with for loops - Gribouillis - May-07-2021 This would already be clearer. What does Python say ? If there is an IndexError, we must look for the faulty index DD = np.zeros((100,100,100,100)) for i0, i in enumerate(Lunghezza_m): for i1, j in enumerate(Larghezza_m): for i2 in range(len(Parameter2)): for i3 in range(len(Velocità_nodi)): DD[i0][i1][i2][i3] = round( 1.025 * i * j * Immersa_m[i1][i2] * Cb[i0][i3], 2) RE: Realization of a tensor with for loops - Elda46 - May-07-2021 (May-07-2021, 02:20 PM)Gribouillis Wrote: This would already be clearer. What does Python say ? If there is an IndexError, we must look for the faulty indexThank you!! Python gives me error of this type: IndexError Traceback (most recent call last) <ipython-input-3-2b4e026148f8> in <module> 55 for i3 in range(len(Velocità_nodi)): 56 DD[i0][i1][i2][i3] = round( ---> 57 1.025 * i * j * Immersa_m[i1][i2] * Cb[i0][i3], 2) 58 59 IndexError: invalid index to scalar variable. RE: Realization of a tensor with for loops - Gribouillis - May-07-2021 There are 8 indices in this line. Find the bad one. |