1

I'm currently trying to use my already written python code with Cython in the hope of a better performance. However, I encounter problems at a point I use scipy.interpolate.ode:

TypeError: f() takes exactly 3 positional arguments (2 given)

This can be reproduced with the following code:

import scipy.integrate as inte
import scipy.interpolate
import numpy as np


class model(object):
   def __init__(self):
    self.r = np.arange(10) #a variable
    self.val = np.arange(10,20) #some values already calculated
    self.interpol = scipy.interpolate.interp1d(self.r,self.val) #interpolation

   #works with python but produces an error with cython
   def do_integration(self):

       abbrev = lambda i: self.interpol(i) #this is more complex in reality

       def f(x,y,i):
           return x+abbrev(i)

       ode = inte.ode(f,None)
       ode.set_integrator('dopri5')
       ode.set_f_params(5)
       ode.set_initial_value(0,1)

       for i in np.arange(0,1,0.1):
           ode.integrate(i)

If f is defined outside of do_integration, it works also with cython. However, in the real code I define 4-5 lambda functions which are then used in f (mainly to get some derivatives from interpolated values, probably this is a bad style?). Thus, it would mean a lot of work to define f outside of do_integration.

I think my question is similar to this one, however the proposed solution does not work out for me:

Just defining

cpdef double f(double x,double y,double i):

gives

C function definition not allowed here

As I'm new to Cython, I'm not really sure what is the reason for that error messages. Is there anyone who can help me out?

Thanks a lot!

El Hors
  • 11
  • 2
  • 1
    I think you need to learn more Cython before trying something advanced like this. It isn't a drop in replacement for Python. Put the `f` function definition(s) in a separate `.pdx` file, and learn from experience what can be sped up and what can't. Code that depends on imported packages like `scipy` is not a good Cython candidate. – hpaulj Jul 08 '16 at 17:15
  • 1
    @ElHors I can reproduce this. I don't think this is the sort of code that will hugely benefit from Cython, but they generally aim for good Python compatibility so it _should_ work. I would report it as a bug (but it isn't 100% obvious how to do this) – DavidW Jul 09 '16 at 08:38

0 Answers0