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)
| 641 | |
| 642 | @array_function_dispatch(_piecewise_dispatcher) |
| 643 | def piecewise(x, condlist, funclist, *args, **kw): |
| 644 | """ |
| 645 | Evaluate a piecewise-defined function. |
| 646 | |
| 647 | Given a set of conditions and corresponding functions, evaluate each |
| 648 | function on the input data wherever its condition is true. |
| 649 | |
| 650 | Parameters |
| 651 | ---------- |
| 652 | x : ndarray or scalar |
| 653 | The input domain. |
| 654 | condlist : list of bool arrays or bool scalars |
| 655 | Each boolean array corresponds to a function in `funclist`. Wherever |
| 656 | `condlist[i]` is True, `funclist[i](x)` is used as the output value. |
| 657 | |
| 658 | Each boolean array in `condlist` selects a piece of `x`, |
| 659 | and should therefore be of the same shape as `x`. |
| 660 | |
| 661 | The length of `condlist` must correspond to that of `funclist`. |
| 662 | If one extra function is given, i.e. if |
| 663 | ``len(funclist) == len(condlist) + 1``, then that extra function |
| 664 | is the default value, used wherever all conditions are false. |
| 665 | funclist : list of callables, f(x,*args,**kw), or scalars |
| 666 | Each function is evaluated over `x` wherever its corresponding |
| 667 | condition is True. It should take a 1d array as input and give an 1d |
| 668 | array or a scalar value as output. If, instead of a callable, |
| 669 | a scalar is provided then a constant function (``lambda x: scalar``) is |
| 670 | assumed. |
| 671 | args : tuple, optional |
| 672 | Any further arguments given to `piecewise` are passed to the functions |
| 673 | upon execution, i.e., if called ``piecewise(..., ..., 1, 'a')``, then |
| 674 | each function is called as ``f(x, 1, 'a')``. |
| 675 | kw : dict, optional |
| 676 | Keyword arguments used in calling `piecewise` are passed to the |
| 677 | functions upon execution, i.e., if called |
| 678 | ``piecewise(..., ..., alpha=1)``, then each function is called as |
| 679 | ``f(x, alpha=1)``. |
| 680 | |
| 681 | Returns |
| 682 | ------- |
| 683 | out : ndarray |
| 684 | The output is the same shape and type as x and is found by |
| 685 | calling the functions in `funclist` on the appropriate portions of `x`, |
| 686 | as defined by the boolean arrays in `condlist`. Portions not covered |
| 687 | by any condition have a default value of 0. |
| 688 | |
| 689 | |
| 690 | See Also |
| 691 | -------- |
| 692 | choose, select, where |
| 693 | |
| 694 | Notes |
| 695 | ----- |
| 696 | This is similar to choose or select, except that functions are |
| 697 | evaluated on elements of `x` that satisfy the corresponding condition from |
| 698 | `condlist`. |
| 699 | |
| 700 | The result is:: |