Skip to content

numpy.interp bug (Trac #1064) #1662

Closed
Closed

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

Activity

  1. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    Attachment added by trac user changimeno on 2009-03-23: _compiled_base.c

  2. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    @charris wrote on 2009-04-11

    Attach a diff, please.

    Note that the whole d*mn file could use a cleanup and contains duplicate functionality with some functions in core/src. I think it should eventually be merged with the rest of the c stuff.

  3. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    Attachment added by @charris on 2009-04-11: _compiled_base.patch

  4. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    @charris wrote on 2010-05-24

    Should be fixed in r8433.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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