Python Forum
Realization of a tensor with for loops
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Realization of a tensor with for loops
#1
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+=1   
Where 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!!
Reply
#2
Why loop over k and w if they are not used in the formula?
Reply
#3
(May-07-2021, 12:19 PM)Gribouillis Wrote: Why loop over k and w if they are not used in the formula?

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.
Reply
#4
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)
Elda46 likes this post
Reply
#5
(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 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)
Thank 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.
Reply
#6
There are 8 indices in this line. Find the bad one.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tensorflow, How get value from Tensor Oleksnadr 1 1,185 Mar-26-2022, 07:11 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020