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]
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With