Attributeerror: 'numpy.ndarray' Object has no attribute 'append'

Asked

Viewed 495 times

-2

The program below serves to calculate two matrices (w1 and w2) and, from them, calculate an f rmula (called transmission matrix) and after that take the results of this matrix and trace the multiplication between the transmission matrix and its transpose. Until then I was getting it right, until I put this x so he could loop the entire program, causing it to generate the problem mentioned above. When I take this x, the program gets normal. Because it gives this error and what can I do to fix it? I will be grateful for the help, because I am a beginner in language and I do not know many functionality. Follow what I can do until then:

import numpy as np
import math
import cmath

n1=n2=1
w1 = []
w2 = []
g = []
E = 0
i = 1j  
pi = math.pi
M = 200

#valores usados pra montar minha matriz
gama1 = 1
b1 = math.sqrt(gama1) / (1+math.sqrt(1-gama1)) 

gama2 = 0.8
b2 = math.sqrt(gama2) / (1+math.sqrt(1-gama2))

x = int(input('valor: ')) #valor usado pra fazer um loop 
 
#matriz 1
for k in range(1, x+1):
    for i in range(M):
        for j in range(n1):
            w1.append(b1*math.sqrt(1/(pi * (M+1)))*math.sin(((i+1)*(j+1)*pi) / (M+1)))
    w1 = np.array(w1) #transformando a lisa em um array numpy
    print(f'MATRIX W1 \n {w1}')

    for i in range(M):
        for j in range(n2):
            w2.append(b2*math.sqrt(1/(pi * (M+1)))*math.sin(((i+1)*(j+1)*pi) / (M+1)))
    w2 = np.array(w2)
    print(f'MATRIX W2 \n {w2}')

    np.savetxt('dados.dat', [w1,w2], delimiter=",") # salvo os valores das matrizes no arquvio "dados.dat"

    H = np.random.rand (M,M)  #matriz hamiltoniana
    formula1 = -2*pi*w1.T*i #parte do cálculo
    formula2 = E - H + pi*i #parte do cálculo
    multiplication = (w1+w1.T) * (w2+w2.T) # soma da multiplicação entre as matrizes e suas transpostas
    print(f'VALOR DA MULTIPLICAÇÃO IGUAL A : {multiplication}')

    t = formula1*((formula2*(multiplication))**(-1))*w2 #cálculo completo
    print(t)

    for k in range(x):
        g.append(np.trace(t*t.T)) #criação do vetor g pra armazenar o valor do traço da multiplicação entre t e a transposta de t
    g = np.array(g)#transformo a lista em array
    print(f' valores de g = [{g}]')

I hope that it was clear and any questions, if I know how to answer, I reply I thank anyone who can help me :)

  • 1

    The error occurs because Voce is converting the list to numpy.ndarray on line 28 and line 34. This makes the list (which is now ndarray) lose the method in the next iteration as it is no longer a list.

  • thank you oooooooooo

1 answer

1

The error is occurring because Voce is converting the lists to the ndarray type on line 28(w1), line 34(w2) and line 55(g). This makes the list (which is now ndarray) lose the method in the next iteration as it is no longer a list.

Declare the 3 lists w1, w2, g within the first for.

import numpy as np
import math
import cmath

n1 = n2 = 1
E = 0
i = 1j
pi = math.pi
M = 200

# valores usados pra montar minha matriz
gama1 = 1
b1 = math.sqrt(gama1) / (1+math.sqrt(1-gama1))

gama2 = 0.8
b2 = math.sqrt(gama2) / (1+math.sqrt(1-gama2))

x = int(input('valor: '))  # valor usado pra fazer um loop

# matriz 1
for k in range(1, x+1):
    w1 = []
    w2 = []
    g = []
    for i in range(M):
        for j in range(n1):
            w1.append(b1*math.sqrt(1/(pi * (M+1))) *
                    math.sin(((i+1)*(j+1)*pi) / (M+1)))
    w1 = np.array(w1)  # transformando a lisa em um array numpy
    print(f'MATRIX W1 \n {w1}')

    for i in range(M):
        for j in range(n2):
            w2.append(b2*math.sqrt(1/(pi * (M+1))) *
                    math.sin(((i+1)*(j+1)*pi) / (M+1)))
    w2 = np.array(w2)
    print(f'MATRIX W2 \n {w2}')

    # salvo os valores das matrizes no arquvio "dados.dat"
    np.savetxt('dados.dat', [w1, w2], delimiter=",")

    H = np.random.rand(M, M)  # matriz hamiltoniana
    formula1 = -2*pi*w1.T*i  # parte do cálculo
    formula2 = E - H + pi*i  # parte do cálculo
    # soma da multiplicação entre as matrizes e suas transpostas
    multiplication = (w1+w1.T) * (w2+w2.T)
    print(f'VALOR DA MULTIPLICAÇÃO IGUAL A : {multiplication}')

    t = formula1*((formula2*(multiplication))**(-1))*w2  # cálculo completo
    print(t)

    for k in range(x):
        # criação do vetor g pra armazenar o valor do traço da multiplicação entre t e a transposta de t
        g.append(np.trace(t*t.T))
    g = np.array(g)  # transformo a lista em array
    print(f' valores de g = [{g}]')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.