MCPcopy Index your code
hub / github.com/numpy/numpy / piecewise

Function piecewise

numpy/lib/_function_base_impl.py:697–815  ·  view source on GitHub ↗

Evaluate a piecewise-defined function. Given a set of conditions and corresponding functions, evaluate each function on the input data wherever its condition is true. Parameters ---------- x : ndarray or scalar The input domain. condlist : list of bool arrays o

(x, condlist, funclist, *args, **kw)

Source from the content-addressed store, hash-verified

695
696@array_function_dispatch(_piecewise_dispatcher)
697def piecewise(x, condlist, funclist, *args, **kw):
698 """
699 Evaluate a piecewise-defined function.
700
701 Given a set of conditions and corresponding functions, evaluate each
702 function on the input data wherever its condition is true.
703
704 Parameters
705 ----------
706 x : ndarray or scalar
707 The input domain.
708 condlist : list of bool arrays or bool scalars
709 Each boolean array corresponds to a function in `funclist`. Wherever
710 `condlist[i]` is True, `funclist[i](x)` is used as the output value.
711
712 Each boolean array in `condlist` selects a piece of `x`,
713 and should therefore be of the same shape as `x`.
714
715 The length of `condlist` must correspond to that of `funclist`.
716 If one extra function is given, i.e. if
717 ``len(funclist) == len(condlist) + 1``, then that extra function
718 is the default value, used wherever all conditions are false.
719 funclist : list of callables, f(x,*args,**kw), or scalars
720 Each function is evaluated over `x` wherever its corresponding
721 condition is True. It should take a 1d array as input and give a 1d
722 array or a scalar value as output. If, instead of a callable,
723 a scalar is provided then a constant function (``lambda x: scalar``) is
724 assumed.
725 args : tuple, optional
726 Any further arguments given to `piecewise` are passed to the functions
727 upon execution, i.e., if called ``piecewise(..., ..., 1, 'a')``, then
728 each function is called as ``f(x, 1, 'a')``.
729 kw : dict, optional
730 Keyword arguments used in calling `piecewise` are passed to the
731 functions upon execution, i.e., if called
732 ``piecewise(..., ..., alpha=1)``, then each function is called as
733 ``f(x, alpha=1)``.
734
735 Returns
736 -------
737 out : ndarray
738 The output is the same shape and type as x and is found by
739 calling the functions in `funclist` on the appropriate portions of `x`,
740 as defined by the boolean arrays in `condlist`. Portions not covered
741 by any condition have a default value of 0.
742
743
744 See Also
745 --------
746 choose, select, where
747
748 Notes
749 -----
750 This is similar to choose or select, except that functions are
751 evaluated on elements of `x` that satisfy the corresponding condition from
752 `condlist`.
753
754 The result is::

Callers 10

test_simpleMethod · 0.90
test_two_conditionsMethod · 0.90
test_defaultMethod · 0.90
test_0dMethod · 0.90
test_0d_comparisonMethod · 0.90
test_0d_0d_conditionMethod · 0.90
test_subclassesMethod · 0.90
i0Function · 0.85

Calls 6

isscalarFunction · 0.90
zeros_likeFunction · 0.90
asanyarrayFunction · 0.85
funcFunction · 0.70
asarrayFunction · 0.50
anyMethod · 0.45

Tested by 9

test_simpleMethod · 0.72
test_two_conditionsMethod · 0.72
test_defaultMethod · 0.72
test_0dMethod · 0.72
test_0d_comparisonMethod · 0.72
test_0d_0d_conditionMethod · 0.72
test_subclassesMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…