1

I am numerically integrating some ODE's, e.g.

y'(t) = f(y(t), t) 

This is easily done using for instance scipy's integrate.ode. The function f is defined using standard Python, e.g.:

def f(y, t, k): return -k*y**3 

My understand is that this means that the fortran/C implementation used by integrate.ode must do call-backs to Python all the time and this can be quite slow. My question is whether there is a way to avoid this?

Preferably I am looking for a package that lets me inline in my Python code a C-snippet, e.g.:

double f(double y, double t, double k) { return -k*pow(y,3); } 

Is there any ODE integrator library for Python that allows this?

I know there are packages like scipy.weave that could be used to inline C code in Python, but I can't see an easy way to interface with integrate.ode. In all cases I think interfacing will have to go through a Python function call.

Inline C-code like this exist in other libraries such as fenics, where Expression allow jit compiled C code.

2
  • 1
    I think you're in luck - recent changes in scipy 0.19 let you pass a compiled Cython function to scipy.ode. I've not actually used it myself though so can't easily provide a useful working example... Commented Aug 23, 2017 at 17:05
  • Thank you @DavidW. This is very usefull! I'm excited to see that coming to scipy. However, it seems to not be implemented for odeint yet, but only for quad (see descriptions of func in the documentation). Commented Aug 29, 2017 at 15:09

1 Answer 1

2

There is a scikit devoted to this that extends the capabilities of scipy.integrate.

It is available here: https://github.com/bmcage/odes

The documentation contains an example of ODE integration sped up by implementing the right hand side in Cython: https://github.com/bmcage/odes/blob/master/docs/ipython/Cython%20cvode%20speedup.ipynb

As DavidW mentions, there is a new feature in SciPy to implement compiled style callbacks but only the quadrature routines can make use of it at this time.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.