So I am giving a talk to my lab about doing some fast ODE solving using
python. Traditionally I have used f2py to define the callback function, but I
think Cython is a better fit for some of the newer students that don't know
fortran (though in this case it is easy to teach them).

Now using the cython/numpy tutorial I can get my code to run around 50% slower
than the f2py generated code (which is still a healthy order of magnitude
faster than the python callback . . .). If  there are any easy things I can do
to make the code faster (without sacrificing readability) I would be very
grateful!

The callback code is (if you want the full program just ask, I am asking more
for glaring errors, as opposed to subtle optimizations):

cdef class Model:

    cdef public double a1, a2, b1, b2, d1, d2

        def __call__(self, np.ndarray[np.float_t, ndim=1] y, int t):
            cdef np.ndarray[np.float_t, ndim=1] yprime = np.empty(3)

            yprime[0] = y[0]*(1.0 - y[0]) - self.a1*y[0]*y[1]/(1.0 + 
self.b1*y[0]) 
            yprime[1] = self.a1*y[0]*y[1]/(1.0 + self.b1*y[0]) - 
self.a2*y[1]*y[2]/(1.0 + self.b2*y[1]) - self.d1*y[1]
            yprime[2] = self.a2*y[1]*y[2]/(1.0 + self.b2*y[1]) - self.d2*y[2]

            return yprime

thanks,
Gabriel
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to