interpolation.hpp

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

interpolation.hpp

b.nonas
Hi,

I found a problem accessing the last (i.e. highest element) of a vector by
using 1D interpolation. The problem seems to be the xEnd-1 as the upper bound
for the std::upper_bound(xBegin,xEnd-1,value) function in locate (Real x).
upper_bound returns a pointer to [a.begin(),...,a.end) where the pointer to
the last field is only returned for  value>=last element. By passing
a.end()-1 to the function we are therefore effectively passing only the array
[a[0],...,a.end()-2].
Replacing the code for locate
          Size locate(Real x) const {
                if (x < *xBegin_)
                    return 0;
                else if (x > *(xEnd_-1))
                    return xEnd_-xBegin_-2;
                else
                    return std::upper_bound(xBegin_,xEnd_-1,x)-xBegin_-1;
            }

by

           Size locate(Real x) const {
                if (x < *xBegin_)
                    return 0;
                else if (x > *(xEnd_-1))
                    return xEnd_-xBegin_-1;
                else
                    return std::upper_bound(xBegin_,xEnd_,x)-xBegin_-1;
            }
worked fine for me and passed all the tests in the suite as well.
Maybe I am missing something simple?

Cheers,
Bereshad


Reply | Threaded
Open this post in threaded view
|

Re: interpolation.hpp

Luigi Ballabio
On 01/13/05 23:28:59, b.nonas wrote:

>
> I found a problem accessing the last (i.e. highest element) of a vector  
> by using 1D interpolation. The problem seems to be the xEnd-1 as the  
> upper bound for the std::upper_bound(xBegin,xEnd-1,value) function in  
> locate (Real x).
> upper_bound returns a pointer to [a.begin(),...,a.end) where the pointer
> to the last field is only returned for  value>=last element. By passing
> a.end()-1 to the function we are therefore effectively passing only the
> array
> [a[0],...,a.end()-2].

Bereshad,
        that is intentional. locate() returns an index j which is later  
used to interpolate between v[j] and v[j+1]. Therefore, j cannot be N-1 as  
in this case v[j+1] would be off the end of the vector.  When a value is  
asked close to the end of the range, locate() returns N-2 so that the data  
are interpolated between v[N-2] and v[N-1] and the correct value is  
returned.

Cheers,
        Luigi