I am working with a large system of ODEs that I automatically construct. I have a state
array of length n
, and an equal number of differential equations also stored in an array. Here there are 2, but in the real system, there would be over 100. The differential equations are constructed as a class and called via __call__
, alternatively, ODEs are created and called with functools partial
. I need to send the exact same state
and time t
to each differential function in the array and fill the derivative state vector dstate
. I am wondering what the cleanest, most pythonic and, most importantly, fastest way of doing that is.
import numpy as np
from scipy import integrate
import pandas as pd
class F1:
def __init__(self):
pass
def __call__(self, state):
return 2 - state[0]**2 * state[1]
class F2:
def __init__(self):
pass
def __call__(self, state):
return 3 - state[1]*state[0]
fs = np.array([F1(), F2()])
state0 = np.ones(2)*4
def change(t, state):
"""
This is the function I would like to change for something better!
Ideally there would be something like: dstate = map(input_to_apply, list_of_functions)
"""
dstate = np.zeros(2)
for i,f in enumerate(fs):
dstate[i] = f(state = state)
return dstate
sol = integrate.solve_ivp(fun = change, t_span = (0, 15), y0 = state0)
res = pd.DataFrame(sol.y.T, columns = ['A', 'B'])
res.index = sol.t
ax = res.plot()