I have been playing around with a package that uses a linear scipy.interpolate.interp1d to create a history function for the ode solver in scipy, described here.
The relevant bit of code goes something like
def update(self, ti, Y):
""" Add one new (ti, yi) to the interpolator """
self.itpr.x = np.hstack([self.itpr.x, [ti]])
yi = np.array([Y]).T
self.itpr.y = np.hstack([self.itpr.y, yi])
#self.itpr._y = np.hstack([self.itpr.y, yi])
self.itpr.fill_value = Y
Where "self.itpr" is initialized in __init__:
def __init__(self, g, tc=0):
""" g(t) = expression of Y(t) for t<tc """
self.g = g
self.tc = tc
# We must fill the interpolator with 2 points minimum
self.itpr = scipy.interpolate.interp1d(
np.array([tc-1, tc]), # X
np.array([self.g(tc), self.g(tc)]).T, # Y
kind='linear', bounds_error=False,
fill_value = self.g(tc))
Where g
is some function that returns an array of values that are solutions to a set of differential equations and tc
is the current time.
This seems nice to me because a new interpolator object doesn't have to be re-created every time I want to update the ranges of values (which happens at each explicit time step during a simulation). This method of updating the interpolator works well under scipy v 0.11.0. However, after updating to v 0.12.0 I ran into issues. I see that the new interpolator now includes an array _y
that seems to just be another copy of the original. Is it safe and/or sane to just update _y
as outlined above as well? Is there a simpler, more pythonic way to address this that would hopefully be more robust to future updates in scipy? Again, in v 0.11 everything works well and expected results are produced, and in v 0.12 I get an IndexError
when _y
is referenced as it isn't updated in my function while y itself is.
Any help/pointers would be appreciated!