dask 2021.10.0

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

This docstring was copied from numpy.piecewise.

Some inconsistencies with the Dask version may exist.

Given a set of conditions and corresponding functions, evaluate each function on the input data wherever its condition is true.

Notes

This is similar to choose or select, except that functions are evaluated on elements of x that satisfy the corresponding condition from :None:None:`condlist`.

The result is:

|--
|funclist[0](x[condlist[0]])
|funclist[1](x[condlist[1]])
|...
|funclist[n2](x[condlist[n2]])
|--

Parameters

x : ndarray or scalar

The input domain.

condlist : list of bool arrays or bool scalars

Each boolean array corresponds to a function in :None:None:`funclist`. Wherever :None:None:`condlist[i]` is True, :None:None:`funclist[i](x)` is used as the output value.

Each boolean array in :None:None:`condlist` selects a piece of x, and should therefore be of the same shape as x.

The length of :None:None:`condlist` must correspond to that of :None:None:`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 : list of callables, f(x,*args,**kw), or 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 : tuple, optional

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 as f(x, 1, 'a') .

kw : dict, optional

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 as f(x, alpha=1) .

Returns

out : ndarray

The output is the same shape and type as x and is found by calling the functions in :None:None:`funclist` on the appropriate portions of x, as defined by the boolean arrays in :None:None:`condlist`. Portions not covered by any condition have a default value of 0.

Evaluate a piecewise-defined function.

See Also

choose
select
where

Examples

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

This example is valid syntax, but we were not able to check execution
>>> x = np.linspace(-2.5, 2.5, 6)  # doctest: +SKIP
... np.piecewise(x, [x < 0, x >= 0], [-1, 1]) # doctest: +SKIP array([-1., -1., -1., 1., 1., 1.])

Define the absolute value, which is -x for x <0 and x for x >= 0 .

This example is valid syntax, but we were not able to check execution
>>> np.piecewise(x, [x < 0, x >= 0], [lambda x: -x, lambda x: x])  # doctest: +SKIP
array([2.5,  1.5,  0.5,  0.5,  1.5,  2.5])

Apply the same function to a scalar value.

This example is valid syntax, but we were not able to check execution
>>> y = -2  # doctest: +SKIP
... np.piecewise(y, [y < 0, y >= 0], [lambda x: -x, lambda x: x]) # doctest: +SKIP array(2)
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

dask.array.routines.piecewise

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /dask/array/routines.py#2121
type: <class 'function'>
Commit: