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)
| 695 | |
| 696 | @array_function_dispatch(_piecewise_dispatcher) |
| 697 | def 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:: |
searching dependent graphs…