6

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'

3
  • usol = insert(usol, 0, 1) Commented Aug 4, 2015 at 9:32
  • 1
    possible duplicate of python, numpy; How to insert element at the start of an array Commented Aug 4, 2015 at 9:42
  • Use usol=np.concatenate(([1], usol)). That's all that np.insert does. Commented Aug 4, 2015 at 12:14

2 Answers 2

10

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

Sign up to request clarification or add additional context in comments.

Comments

6

insert is not an attribute of the array. You can use usol=insert(usol,0,1) to get the result you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.