[SciPy-user] Polynomial interpolation

Eike Welk eike.welk@gmx....
Tue Apr 29 11:57:59 CDT 2008


On Tuesday 29 April 2008 00:04, Eike Welk wrote:
> On Monday 28 April 2008 23:24, Robert Kern wrote:
> > On Mon, Apr 28, 2008 at 4:21 PM, Eike Welk <eike.welk@gmx.net>
>
> wrote:
> > > On Monday 28 April 2008 22:36, Anne Archibald wrote:
> > >  > 2008/4/28 Robert Kern <robert.kern@gmail.com>:
> > >  > >  Well those certainly aren't useful. The only functions I
> > >  > > would consider adding would be "one-shot" functions, e.g.:
> > >  > >
> > >  > >   def krogh(xi, yi, x):
> > >  > >       return KroghInterpolator(xi,yi)(x)
> > >  >
> > >  > The problem here is that construction of the splines is an
> > >  > order degree**2 process, so I want an interface that
> > >  > encourages users to construct them once and for all. I think
> > >  > such an approach also discourages people from just
> > >  >
> > >  > y_interp = krogh(all_my_data_x, all_my_data_y, x_interp)
> > >  >
> > >  > with hundreds of points, the results of which will be
> > >  > meaningless and horrible.
> > >
> > >  You could store already constructed interpolation objects in a
> > >  dictionary. (I didn't test it.):
> >
> > Arrays are unhashable and cannot be used as dictionary keys.
>
> Ah, yes!
>
> One could however store a copy of the last x-data and y-data
> together with the interpolator. When x-data and y-data are the same
> at the next call, the interpolator could be reused.
The function could look like this:


krogh_interpolator_cache=[]

def evaluate_krogh_interpolation(all_my_data_x, all_my_data_y,
                                 x_interp):
    global krogh_interpolator_cache
    maxLen = 3

    #search for already existing interpolators
    for record in krogh_interpolator_cache:
        x, y, interp = record
        if (x==all_my_data_x).all() and (y == all_my_data_y).all():
            return interp(x_interp)

    #limit size of cache
    if len(krogh_interpolator_cache) >= maxLen:
        krogh_interpolator_cache = []
    #create new interpolator
    newInterpolator = KroghInterpolator(all_my_data_x, all_my_data_y)
    krogh_interpolator_cache.append((all_my_data_x.copy(), 
                                     all_my_data_y.copy(),
                                     newInterpolator))
    return newInterpolator(x_interp)


Kind regards,
Eike.


More information about the SciPy-user mailing list