Object 'numpy.ndarray' has no attribute 'insert'

I want to add one item to a vector which receives:

import time
from numpy import *
from scipy.sparse.linalg import bicgstab,splu
from scipy.sparse import lil_matrix,identity
from scipy.linalg import lu_factor,lu_solve,cho_factor,cho_solve
from matplotlib.pyplot import *

 #N,alfa and beta are introduced

    M = lil_matrix((2*N,2*N), dtype='float64')
    b=zeros(2*N)
    M.setdiag(alfa*ones(2*N),0)
    M.setdiag(beta*ones(2*N-1),1)
    M.setdiag(beta*ones(2*N-1),-1)
    M.setdiag(ones(2*N-2),2)
    M.setdiag(ones(2*N-2),-2)
    M=M.tocsc()

    for i in range(0,2*N):
        b[i]=2*dx*feval(fuente,x[i])/6.0

    A=1.0/(3.0*dx)*M
    u=bicgstab(A,b)
    usol=u[0]

      

And now I want to usol.insert(0,1) usol=[1,usol[0],usol[1],..]

, but I have an error. Object 'numpy.ndarray' has no attribute 'insert'

+3


source to share


2 answers


insert

is not an attribute of an array. You can use usol=insert(usol,0,1)

to get the desired result.



+3


source


In numpy, it insert

is a function, not a method. Therefore, you will need to use the following:

import numpy as np
#rest of your code
usol=np.insert(usol,0,1)

      



This will create a copy usol

with the values ​​pasted in. Note that the insert does not occur in place. You can see the docs here

+1


source







All Articles