mindspore.numpy.piecewise
- mindspore.numpy.piecewise(x, condlist, funclist, *args, **kw)[source]
Evaluates 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 (Union[int, float, bool, list, tuple, Tensor]) – The input domain.
condlist (Union[bool, list of bool Tensor]) – Each boolean array corresponds to a function in funclist. Wherever condlist[i] is True, funclist[i](x) is used as the output value. Each boolean array in condlist selects a piece of x, and should therefore be of the same shape as x. 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 is the default value, used wherever all conditions are false.funclist (Union[list of callables, list of scalars]) – Each function is evaluated over x wherever its corresponding condition is True. It should take a 1d array as input and give an 1d array or a scalar value as output. If, instead of a callable, a scalar is provided then a constant function
(lambda x: scalar)
is assumed.args (any) – Any further arguments given to piecewise are passed to the functions upon execution, i.e., if called
piecewise(..., ..., 1, 'a')
, then each function is called asf(x, 1, 'a')
.kw (any) – Keyword arguments used in calling piecewise are passed to the functions upon execution, i.e., if called
piecewise(..., ..., alpha=1)
, then each function is called asf(x, alpha=1)
.
- Returns
Tensor, the output is the same shape and type as x and is found by calling the functions in funclist on the appropriate portions of x, as defined by the boolean arrays in condlist. Portions not covered by any condition have a default value of 0.
- Supported Platforms:
Ascend
GPU
CPU
- Raises
ValueError – if length of funclist is not in
(len(condlist), len(condlist) + 1)
Examples
>>> import mindspore.numpy as np >>> x = np.linspace(-2.5, 2.5, 6) >>> print(np.piecewise(x, [x < 0, x >= 0], [-1, 1])) [-1 -1 -1 1 1 1]