I have a simple function called get_gradient which takes a numpy array of [[x,y,Vx,Vy]] and returns (should return) an array of [[Vx,Vy,Ax,Ay]]. I want to update my k[0][0:2] to equal state[0][2:4] but when I print them out k never changes value. I created a copy of the initial array in the hope that it would sort it out but it still doesn’t work! any help would be much appreciated
Y = numpy.array([[0, 0, 0, 0]]) # [x,y,Vx,Vy]
k = numpy.array([[0, 0, 0, 0]]) # [x,y,Vx,Vy]
def get_gradient(Y):
state = Y.copy()
spring_force = spring.force(state[0], feet_pos[0])
brownian_force = mass.brownian(dt)
drag_force = mass.drag(state[0][2:4])
total_force = spring_force + brownian_force + drag_force #2d array [Fx,Fy]
k[0][0:2] = state[0][2:4] ##### DOESN'T WORK!!!
k[0][2:4] = total_force/mass.mass ##### but this works :s
print k[0][0:2]
print state[0][2:4]
return k
It’s not that k isn’t changing, it’s that it has an integer dtype, and so when you try to assign a float to it, you might not get what you expect:
or
but:
Notice the
.after the zeros.I would probably use
instead; it defaults to
floatdtype.