Mercurial > hg > octave-shane
changeset 11671:1507a9d6df40 release-3-0-x
__lin_interpn__.cc: handle decreasing coordinate values
author | Alexander Barth |
---|---|
date | Thu, 06 Mar 2008 01:58:38 -0500 |
parents | b1368dc9e719 |
children | a5a86cc9ef38 |
files | src/ChangeLog src/DLD-FUNCTIONS/__lin_interpn__.cc |
diffstat | 2 files changed, 64 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,8 @@ +2008-03-06 Alexander Barth <barth.alexander@gmail.com> + + * DLD-FUNCTIONS/__lin_interpn__.cc (lookup): + Handle decreasing coordinate values. + 2008-02-27 John W. Eaton <jwe@octave.org> * oct-stream.cc (do_read): Stop reading if seek fails.
--- a/src/DLD-FUNCTIONS/__lin_interpn__.cc +++ b/src/DLD-FUNCTIONS/__lin_interpn__.cc @@ -43,38 +43,77 @@ octave_idx_type lookup (const double *x, octave_idx_type n, double y) { - octave_idx_type j, j0, j1; + octave_idx_type j; - if (y > x[n-1] || y < x[0]) - return -1; + if (x[0] < x[n-1]) + { + // increasing x + + if (y > x[n-1] || y < x[0]) + return -1; #ifdef EXHAUSTIF - for (j = 0; j < n - 1; j++) - { - if (x[j] <= y && y <= x[j+1]) - return j; - } + for (j = 0; j < n - 1; j++) + { + if (x[j] <= y && y <= x[j+1]) + return j; + } #else - j0 = 0; - j1 = n - 1; + octave_idx_type j0 = 0; + octave_idx_type j1 = n - 1; + + while (true) + { + j = (j0+j1)/2; + + if (y <= x[j+1]) + { + if (x[j] <= y) + return j; - while (true) + j1 = j; + } + + if (x[j] <= y) + j0 = j; + } +#endif + } + else { - j = (j0+j1)/2; + // decreasing x + // previous code with x -> -x and y -> -y - if (y <= x[j+1]) + if (y > x[0] || y < x[n-1]) + return -1; + +#ifdef EXHAUSTIF + for (j = 0; j < n - 1; j++) { - if (x[j] <= y) + if (x[j+1] <= y && y <= x[j]) return j; - - j1 = j; } +#else + octave_idx_type j0 = 0; + octave_idx_type j1 = n - 1; - if (x[j] <= y) - j0 = j; + while (true) + { + j = (j0+j1)/2; + + if (y >= x[j+1]) + { + if (x[j] >= y) + return j; + + j1 = j; + } + + if (x[j] >= y) + j0 = j; + } +#endif } - -#endif } // n-dimensional linear interpolation