python - Issue in numpy array loop for central difference -


input array reference,

u = array([[ 0.,  0.,  0.,  0.,  0.],            [ 0.,  1.,  1.,  1.,  0.],            [ 0.,  1.,  1.,  1.,  0.],            [ 0.,  1.,  1.,  1.,  0.],            [ 0.,  0.,  0.,  0.,  0.]]) 

python function using loop

import numpy np u = np.zeros((5,5)) u[1:-1,1:-1]=1 def cds(n):     in range(1,4):         j in range(1,4):             u[i,j] = u[i,j+1] + u[i,j-1] + u[i+1,j] + u[i-1,j]     return u 

above function cds(5) provide following result using for loop,

u=array([[  0.,   0.,   0.,   0.,   0.],          [  0.,   2.,   4.,   5.,   0.],          [  0.,   4.,  10.,  16.,   0.],          [  0.,   5.,  16.,  32.,   0.],          [  0.,   0.,   0.,   0.,   0.]]) 

same function using numpy

def cds(n):     u[1:-1,1:-1] =  u[1:-1,2:] +  u[1:-1,:-2] +  u[2:,1:-1] +  u[:-2,1:-1]     return u 

but same input array(u), function cds(5) using numpy provide different result.,

u=array([[ 0.,  0.,  0.,  0.,  0.],          [ 0.,  2.,  3.,  2.,  0.],          [ 0.,  3.,  4.,  3.,  0.],          [ 0.,  2.,  3.,  2.,  0.],          [ 0.,  0.,  0.,  0.,  0.]]) 

the reason problem is, python "for loop" updates every u[i,j] value exsisting u array while looping "numpy" didn't.....

i want same result numpy loop.

is there way solve issue in numpy? please me, in advance...


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -