A = A.astype('double') Attributeerror: 'list' Object has no attribute 'astype'

Asked

Viewed 466 times

0

I have this error but I can’t find the cause. Code:

from __future__ import division
import numpy as np
from numpy import linalg


def jacobi(A,b,x0,tol,N):

    #preliminares
    A = A.astype('double')
    b = b.astype('double')
    x0 = x0.astype('double')

    n=np.shape(A)[0]
    x = np.zeros(n)
    it = 0
    #iteracoes
    while (it < N):
        it = it+1
        #iteracao de Jacobi
        for i in np.arange(n):
            x[i] = b[i]
            for j in np.concatenate((np.arange(0,i),np.arange(i+1,n))):
                x[i] -= A[i,j]*x0[j]
            x[i] /= A[i,i]
        #tolerancia
        if (np.linalg.norm(x-x0,np.inf) < tol):
            return x
        #prepara nova iteracao
        x0 = np.copy(x)
    raise NameError('num. max. de iteracoes excedido.')

The data used to call the function are these:

b = [8, 1, 11]

A = [[-1, 4, 1], [5/3,2/3,2/3], [2, 1, 4]]

x0 = [1, 1, 1]

tol = 0.0001

N = 10
  • What is your mistake?

1 answer

0

The mistake says, Like list has no attribute astype. I think the variable A should be the numpy.ndarray, and not the type list:

A = np.array(A) # passa a lista para um numpy.ndarray
A = A.astype('double')

Browser other questions tagged

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