Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
def VerletNextV(v_t,a_t,a_t_plus_h):

  v_t_plus_h = [0.0, 0.0, 0.0]
  #<-- find v_t_plus_h[0], v_t_plus_h[1], v_t_plus_h[2], here -->
  for i in range(0,len(v_t_plus_h)):
    v_t_plus_h[i] = v_t + .5*(a_t[i] + a_t_plus_h[i])
  return v_t_plus_h

Next I have this line:

nV[i] = VerletNextV(V[i],A[i], a_t_h)

which is giving me the error that I specified in my title:

ValueError: setting an array element with a sequence

This is driving me insane - what is the problem here? nV[i] is a declared using:

nV = numpy.zeros((N,3))
share|improve this question
1  
What are the input variables V[i],A[i], a_t_h? –  Marcin Feb 16 at 23:46
2  
You'll want to check what VerletNextV is actually returning. If it is returning a list of 3 numbers then this should work, but it seems likely that is is probably not. –  Iguananaut Feb 16 at 23:48
1  
Unrelatedly, it's not necessary in Python to create a list of zeros if you're just going to replace all the values in the list. You can just as easily declare v_t_plus_h = [] and use v_t_plus_h.append in the loop. Or use a list comprehension. Though since it seems like you're doing vector arithmetic it might make more sense if you velocity and acceleration vectors were also Numpy arrays; then you would just array arithmetic and no explicit loops in Python. –  Iguananaut Feb 16 at 23:52
    
Initializing a numpy array, and then filling it row by row is a valid approach. Building an array from a comprehension is also good. That is, if you can't do it with vector operations. –  hpaulj Feb 17 at 3:09
    
Try this: nV[i,:] = VerletNextV(V[i],A[i], a_t_h). You could also print the shape of both sides of that equation before hand. The error looks like nV[i] is a scalar, and VerletNextV() is a list. –  hpaulj Feb 17 at 3:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.