interpolation.hpp
Posted by b.nonas on
URL: http://quantlib.414.s1.nabble.com/interpolation-hpp-tp3515.html
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