Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass args for solve_ivp (new SciPy ODE API)

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

like image 498
sharkmas Avatar asked Jan 14 '18 00:01

sharkmas


3 Answers

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.

like image 147
Lev K. Avatar answered Nov 16 '22 13:11

Lev K.


It doesn't seem like the new function has an args parameter. As a workaround you can create a wrapper like

def wrapper(t, y):
    orig_func(t,y,hardcoded_args)

and pass that in.

like image 39
rlee827 Avatar answered Nov 16 '22 13:11

rlee827


Recently the 'args' option was added to solve_ivp, see here: https://github.com/scipy/scipy/issues/8352#issuecomment-535689344

like image 6
Javier-Acuna Avatar answered Nov 16 '22 12:11

Javier-Acuna