bundles / scipy 1.17.1 / scipy / _lib / array_api_extra / _lib / _funcs / apply_where
function
scipy._lib.array_api_extra._lib._funcs:apply_where
Signature
def apply_where ( cond : Array , args : Array | tuple[Array, ...] , f1 : Callable[..., Array] , f2 : Callable[..., Array] | None = None , / , fill_value : Array | complex | None = None , xp : ModuleType | None = None ) → Array Summary
Run one of two elementwise functions depending on a condition.
Extended Summary
Equivalent to f1(*args) if cond else fill_value performed elementwise when fill_value is defined, otherwise to f1(*args) if cond else f2(*args).
Parameters
cond: arrayThe condition, expressed as a boolean array.
args: Array or tuple of ArraysArgument(s) to
f1(andf2). Must be broadcastable withcond.f1: callableElementwise function of
args, returning a single array. Wherecondis True, output will bef1(arg0[cond], arg1[cond], ...).f2: callable, optionalElementwise function of
args, returning a single array. Wherecondis False, output will bef2(arg0[cond], arg1[cond], ...). Mutually exclusive withfill_value.fill_value: Array or scalar, optionalIf provided, value with which to fill output array where
condis False. It does not need to be scalar; it needs however to be broadcastable withcondandargs. Mutually exclusive withf2. You must provide one or the other.xp: array_namespace, optionalThe standard-compatible namespace for
condandargs. Default: infer.
Returns
: ArrayAn array with elements from the output of
f1wherecondis True and either the output off2orfill_valuewherecondis False. The returned array has data type determined by type promotion rules between the output off1and eitherfill_valueor the output off2.
Notes
xp.where(cond, f1(*args), f2(*args)) requires explicitly evaluating f1 even when cond is False, and f2 when cond is True. This function evaluates each function only for their matching condition, if the backend allows for it.
On Dask, f1 and f2 are applied to the individual chunks and should use functions from the namespace of the chunks.
Examples
import array_api_strict as xp import array_api_extra as xpx a = xp.asarray([5, 4, 3]) b = xp.asarray([0, 2, 2])⚠
def f(a, b): return a // b✓
xpx.apply_where(b != 0, (a, b), f, fill_value=xp.nan)
⚠Aliases
-
scipy.differentiate.xpx.apply_where