Pass args for solve_ivp (new SciPy ODE API)

sharkmas picture sharkmas · Jan 14, 2018 · Viewed 9.4k times · Source

For solving simple ODEs using SciPy, I used to use the odeint function, with form:

scipy.integrate.odeint(func, y0, t, args=(), Dfun=None, col_deriv=0, full_output=0, ml=None, mu=None, rtol=None, atol=None, tcrit=None, h0=0.0, hmax=0.0, hmin=0.0, ixpr=0, mxstep=0, mxhnil=0, mxordn=12, mxords=5, printmessg=0)[source]

where a simple function to be integrated could include additional arguments of the form:

def dy_dt(t, y, arg1, arg2):
    # processing code here

In SciPy 1.0, it seems the ode and odeint funcs have been replaced by a newer solve_ivp method.

scipy.integrate.solve_ivp(fun, t_span, y0, method='RK45', t_eval=None, dense_output=False, events=None, vectorized=False, **options)

However, this doesn't seem to offer an args parameter, nor any indication in the documentation as to implementing the passing of args.

Therefore, I wonder if arg passing is possible with the new API, or is this a feature that has yet to be added? (It would seem an oversight to me if this features has been intentionally removed?)

Reference: https://docs.scipy.org/doc/scipy/reference/integrate.html

Answer

Lev K. picture Lev K. · Feb 27, 2018

Relatively recently there appeared a similar question on scipy's github. Their solution is to use lambda:

solve_ivp(fun=lambda t, y: fun(t, y, *args), ...)

And they argue that there is already enough overhead for this not to matter.