dpnp.piecewise

dpnp.piecewise(x, condlist, funclist)[source]

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.

For full documentation refer to numpy.piecewise.

Parameters:
  • x ({dpnp.ndarray, usm_ndarray}) -- The input domain.

  • condlist ({list of array-like boolean, bool scalars}) --

    Each boolean array/scalar 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 ({array-like of scalars}) -- A constant value is returned wherever corresponding condition of x is True.

Returns:

out -- 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.

Return type:

dpnp.ndarray

Limitations

Parameters args and kw are not supported and funclist cannot include a callable functions.

See also

dpnp.choose

Construct an array from an index array and a set of arrays to choose from.

dpnp.select

Return an array drawn from elements in choicelist, depending on conditions.

dpnp.where

Return elements from one of two arrays depending on condition.

Examples

>>> import dpnp as np

Define the signum function, which is -1 for x < 0 and +1 for x >= 0.

>>> x = np.linspace(-2.5, 2.5, 6)
>>> np.piecewise(x, [x < 0, x >= 0], [-1, 1])
array([-1., -1., -1.,  1.,  1.,  1.])