title: C/C++ speed ODE integration from Python
body: <p>I am numerically integrating some ODE's, e.g.</p>
  
  <pre><code>y'(t) = f(y(t), t)
  </code></pre>
  
  <p>This is easily done using for instance scipy's <a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.ode.html" rel="nofollow noreferrer">integrate.ode</a>. The function <code>f</code> is defined using standard Python, e.g.:</p>
  
  <pre><code>def f(y, t, k):
      return -k*y**3
  </code></pre>
  
  <p>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. <b>My question is whether there is a way to avoid this?</b></p>
  
  <p>Preferably I am looking for a package that lets me inline in my Python code a C-snippet, e.g.:</p>
  
  <pre><code>double f(double y, double t, double k) {
      return -k*pow(y,3);
  }
  </code></pre>
  
  <p>Is there any ODE integrator library for Python that allows this?</p>
  
  <p>I know there are packages like <a href="https://docs.scipy.org/doc/scipy-0.18.1/reference/tutorial/weave.html" rel="nofollow noreferrer">scipy.weave</a> 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.</p>
  
  <p>Inline C-code like this exist in other libraries such as <a href="https://fenicsproject.org/olddocs/dolfin/1.5.0/python/programmers-reference/functions/expression/Expression.html" rel="nofollow noreferrer">fenics</a>, where <code>Expression</code> allow jit compiled C code.</p>