I want to add one element to a vector which is geting:
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 usol.insert(0,1) usol=[1,usol[0],usol[1],..]
but I have an error 'numpy.ndarray' object has no attribute 'insert'
In numpy, insert
is a function, not a method. So you will have to use the following:
import numpy as np
#rest of your code
usol=np.insert(usol,0,1)
This will create a copy of usol
with values inserted. Note that insert does not occur in-place.
You can see the docs here
insert
is not an attribute of the array. You can use usol=insert(usol,0,1)
to get the result you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With