Skip to content Skip to sidebar Skip to footer

Design Heuristics For Writing Python Classes That Interact With `scipy.integrate.odeint`?

Introduction scipy.integrate.odeint requires as its first argument, a function that computes the derivatives of the variables we want to integrate over (which I'll refer to as d_fu

Solution 1:

I can comment on the Numba portion of this question.

As other users have mentioned, attribute access in Numba leads to some overhead. For example, you might be tempted to write code like this:

classFoo(object):
    def__init__(self, x):
        self.x = x

    @numba.jitdefdosomething(self, y):
        for i inrange(len(self.x)):
            self.x[i] += y[i]

This will be slow, because Numba has to call into the Python layer for attribute access each time it encounters self.x.

A better way to do the same thing would be this:

classFoo(object):
    def__init__(self, x):
        self.x = x

    defdosomething(self, y):
        _dosomething(self.x, y)

@numba.jit(nopython=True)def_dosomething(x, y):
    for i inrange(len(x)):
        x[i] += y[i]

Here there is no attribute access within the loop, and additionally we're able to add the nopython=True argument, which will cause Numba to raise an error if the function has to fall back on any (slow) Python code. This nopython argument is a great way to make sure that your Numba functions are as efficient as possible.

Post a Comment for "Design Heuristics For Writing Python Classes That Interact With `scipy.integrate.odeint`?"