Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a numpy array to iterator

I want to use an array as argument of a function which will be used to solve an ODE function.

def ode(x, t, read_tau, tau_arr):
  q_ib = x[0:4]
  omega = x[4:7]

  dq_ib = 0.5 * np.dot(gen_omega(omega), q_ib) + read_tau(tau_arr)

  return dq_ib

dq_ib = odeint(rhs, x0, t, args=(b_I, read_tau, tau_arr))

And tau_arr is an (1000, 3) array. The only solution I can think of is first make tau_arr as an iterator and in read_tau().

def read_tau(tau_arr):
  return next(tau_arr)

And the return value of read_tau function will be a 1x3 array which will be used to solved ODE.

My question is how to convert a 2-dimensional array into an iterator, and when calling the iterator with next(), it will return an array row by row.

a = np.array([[1,2,3], [4,5,6]])
convert_to_iter(a)
next(a)
[1,2,3]
next[a]
[4,5,6]
like image 958
Lion Lai Avatar asked Jan 27 '19 21:01

Lion Lai


1 Answers

Your desired convert_to_iter() is the Python built-in iter() function.

> a = iter(np.array([[1,2,3], [4,5,6]]))
> next(a)
[1,2,3]
> next[a]
[4,5,6]
like image 196
Stephen Rauch Avatar answered Sep 17 '22 10:09

Stephen Rauch