diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2014-02-11 18:16:02 -0700 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2014-02-11 18:16:02 -0700 |
commit | 58ffcdc62b1db9d6c762a82ca483484a5cd7c92b (patch) | |
tree | cda9017ae4d767a97d6e1e3e307b1e6c3af5c1aa /numpy/lib/src | |
parent | e2addbd77fbed5aa64a07f9b08e217c63e15467f (diff) | |
parent | c8975ff262292d760b533bb433de50b686b15f9e (diff) | |
download | numpy-58ffcdc62b1db9d6c762a82ca483484a5cd7c92b.tar.gz |
Merge pull request #4247 from jaimefrio/digitize-monotonic
BUG: check for monotonic bin arrays in digitize
Diffstat (limited to 'numpy/lib/src')
-rw-r--r-- | numpy/lib/src/_compiled_base.c | 37 |
1 files changed, 25 insertions, 12 deletions
diff --git a/numpy/lib/src/_compiled_base.c b/numpy/lib/src/_compiled_base.c index 328fc2d14..c91855e81 100644 --- a/numpy/lib/src/_compiled_base.c +++ b/numpy/lib/src/_compiled_base.c @@ -60,29 +60,44 @@ decr_slot_right_(double x, double * bins, npy_intp lbins) return 0; } -/** +/* * Returns -1 if the array is monotonic decreasing, * +1 if the array is monotonic increasing, * and 0 if the array is not monotonic. */ static int -check_array_monotonic(double * a, int lena) +check_array_monotonic(const double *a, npy_int lena) { - int i; + npy_intp i; + double next; + double last = a[0]; - if (a [0] <= a [1]) { - /* possibly monotonic increasing */ - for (i = 1; i < lena - 1; i ++) { - if (a [i] > a [i + 1]) { + /* Skip repeated values at the beginning of the array */ + for (i = 1; (i < lena) && (a[i] == last); i++); + + if (i == lena) { + /* all bin edges hold the same value */ + return 1; + } + + next = a[i]; + if (last < next) { + /* Possibly monotonic increasing */ + for (i += 1; i < lena; i++) { + last = next; + next = a[i]; + if (last > next) { return 0; } } return 1; } else { - /* possibly monotonic decreasing */ - for (i = 1; i < lena - 1; i ++) { - if (a [i] < a [i + 1]) { + /* last > next, possibly monotonic decreasing */ + for (i += 1; i < lena; i++) { + last = next; + next = a[i]; + if (last < next) { return 0; } } @@ -90,8 +105,6 @@ check_array_monotonic(double * a, int lena) } } - - /* find the index of the maximum element of an integer array */ static npy_intp mxx (npy_intp *i , npy_intp len) |