difficulty in understanding numpy array behavior
the following is a (very)shorterned vesrion of my program
the normvecs vector is not getting overwritten , you woud be able to see for yourself just once you run this code snippet , the parameters are correct geom function takes an n+1 X 2 array and n as input, i guess i'm doing something really stupid (which i think i am) or i don't understand this behavior
import numpy as np
#geometry calculations
def geom(pts,n):
r = np.zeros(n)
normvecs = np.zeros((n,2))
tgtvecs = np.zeros((n,2))
alphap = np.zeros(n)
cpts = np.zeros((n,2))
#collocation points
cpts[:,0] = (pts[0:n,0]+pts[1:n+1,0])/2
cpts[:,1] = (pts[0:n,1]+pts[1:n+1,1])/2
#length of panels
r[:] = np.sqrt((pts[0:n,0]-pts[1:n+1,0])**2 + (pts[0:n,1]-pts[1:n+1,1])**2)
#angle of each panel with the horizantal(chord)/refernce axis
alphap[:] = np.arctan2(pts[1:n+1,1]-pts[0:n,1],pts[1:n+1,0]-pts[0:n,0])
#normal vectors
normvecs[:,0] = -np.sin(alphap[:])
normvecs[:,1] = np.cos(alphap[:])
xx = np.cos(alphap[:])
print(normvecs[:,0],normvecs[:,1],np.cos(alphap[:]),xx)
return
geom(np.random.rand(31,2),30)
normvecs[:,1]开发者_如何转开发 is showing just negative of nomvecs[:,0] i wasn't able to overwrite like normvecs[:,1] = xx (no errors .. but just printed the same thing)
I maynot sound clear,once if you run you may see what i'm trying to tell.
what's the problem?(if it is so!)
I see no problem at all. Running a more concise version of your code:
import numpy as np
#geometry calculations
def geom(pts,n):
normvecs = np.zeros((n,2))
alphap = np.zeros(n)
alphap[:] = np.arctan2(pts[1:n+1,1]-pts[0:n,1],pts[1:n+1,0]-pts[0:n,0])
normvecs[:,0] = -np.sin(alphap[:])
normvecs[:,1] = np.cos(alphap[:])
print np.abs(normvecs[:,0])-np.abs(normvecs[:,1])
return
geom(np.random.rand(31,2),30)
produces something like this:
[ 0.47500019 -0.03182906 -0.46597523 0.7479451 0.12580804 -0.36311644
0.06406616 -0.29641905 -0.39982319 -0.98493049 -0.4431009 -0.29506693
-0.25931983 0.67831564 -0.80676608 -0.53007712 0.63448069 0.67457029
0.25457744 -0.82095266 -0.27461275 -0.91244341 -0.69530798 -0.69023852
0.18840807 0.49891863 0.52417747 0.06833423 0.83449866 0.47608894]
Which suggests to me that the elements of two rows of normvecs
you are saying are identical in magnitude are not.
精彩评论