Skip to content

numpy.interp bug (Trac #1064) #1662

@thouis

Description

@thouis

Original ticket http://projects.scipy.org/numpy/ticket/1064 on 2009-03-23 by trac user changimeno, assigned to @charris.

interp(x, xp, fp, left=None, right=None)

the "right"--parameter option does not work properly.

Example:

In [2]: x = arange(4)*2 + 2

In [3]: y=x**2
In [4]: xx = arange(11)

In [5]: x
Out[5]: array([2, 4, 6, 8])

In [6]: y
Out[6]: array([ 4, 16, 36, 64])

In [7]: xx
Out[7]: array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

In [14]: numpy.interp(xx,x,y)
Out[14]: array([  4.,   4.,   4.,  10.,  16.,  26.,  36.,  50.,  64.,  64.,  64.]) #---->>>> RIGHT!!!!

In [15]: numpy.interp(xx,x,y,left=0.)
Out[15]: array([  0.,   0.,   4.,  10.,  16.,  26.,  36.,  50.,  64.,  64.,  64.])  #---->>>> RIGHT!!!!

In [16]: numpy.interp(xx,x,y,left=0.,right=0.)
Out[16]: array([  0.,   0.,   4.,  10.,  16.,  26.,  36.,  50.,  0.,  0.,  0.])  #---->>>> WRONG!!!!

numpy.interp set the vales of the interpolated array (yy) for "xx[i] >= x[len(x)-1]" to the given RIGHT value. So, the last GOOD value (xx[i] == x[len(x)-1]) is wrongly set to the RIGHT value instead of the proper value yy[i] = y[len(x)-1].

I edited the file numpy/lib/src/_compiled_base.c and fixed this bug (see attachment). Now

In [19]: numpy.interp(xx,x,y,0.,0.)
Out[19]: array([  0.,   0.,   4.,  10.,  16.,  26.,  36.,  50.,  64.,   0.,   0.])  #---->>>> RIGHT!!!!

My only worry is that I modified "binary_search" together with "arr_interp" and I do not know if this function is called by other routines and whether this modification can alter the results.

diff 
>     /* sgg */
>     else if (dval > dlist [len-1])
>       result = len ;
>     /* sgg */
448c454,455
<         else if (indx >= lenxp - 1)

---
>       /*sgg        else if (indx >= lenxp - 1) */
>         else if (indx > lenxp - 1)

Cheers,
Chan

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions