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!