summaryrefslogtreecommitdiff
path: root/numpy/lib/function_base.py
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2017-10-22 16:45:05 -0700
committerEric Wieser <wieser.eric@gmail.com>2017-10-23 20:31:40 -0700
commitc875b1391286a982c958d349d58eb720eb081069 (patch)
treeda383b1c94b6219d76c20978c5ff53a8ed6b4561 /numpy/lib/function_base.py
parent303941c929ee2938dc55ad633c9fc063610941b9 (diff)
downloadnumpy-c875b1391286a982c958d349d58eb720eb081069.tar.gz
BUG: Throw an error if too many functions are given to piecewise
Especially necessary given the strange heuristics that decay the number of conditions to 1
Diffstat (limited to 'numpy/lib/function_base.py')
-rw-r--r--numpy/lib/function_base.py7
1 files changed, 6 insertions, 1 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index 0f221fe05..498853d32 100644
--- a/numpy/lib/function_base.py
+++ b/numpy/lib/function_base.py
@@ -1254,7 +1254,7 @@ def piecewise(x, condlist, funclist, *args, **kw):
The length of `condlist` must correspond to that of `funclist`.
If one extra function is given, i.e. if
- ``len(funclist) - len(condlist) == 1``, then that extra function
+ ``len(funclist) == len(condlist) + 1``, then that extra function
is the default value, used wherever all conditions are false.
funclist : list of callables, f(x,*args,**kw), or scalars
Each function is evaluated over `x` wherever its corresponding
@@ -1336,6 +1336,11 @@ def piecewise(x, condlist, funclist, *args, **kw):
condelse = ~np.any(condlist, axis=0, keepdims=True)
condlist = np.concatenate([condlist, condelse], axis=0)
n += 1
+ elif n != n2:
+ raise ValueError(
+ "with {} condition(s), either {} or {} functions are expected"
+ .format(n, n, n+1)
+ )
y = zeros(x.shape, x.dtype)
for k in range(n):