cond#
- class brainpy.math.cond(pred, true_fun, false_fun, operands=(), dyn_vars=None, child_objs=None)[source]#
Simple conditional statement (if-else) with instance of
Variable
.>>> import brainpy.math as bm >>> a = bm.Variable(bm.zeros(2)) >>> b = bm.Variable(bm.ones(2)) >>> def true_f(): a.value += 1 >>> def false_f(): b.value -= 1 >>> >>> bm.cond(True, true_f, false_f) >>> a, b Variable([1., 1.], dtype=float32), Variable([1., 1.], dtype=float32) >>> >>> bm.cond(False, true_f, false_f) >>> a, b Variable([1., 1.], dtype=float32), Variable([0., 0.], dtype=float32)
- Parameters:
pred (bool) – Boolean scalar type, indicating which branch function to apply.
true_fun (callable, ArrayType, float, int, bool) – Function to be applied if
pred
is True. This function must receive one arguement foroperands
.false_fun (callable, ArrayType, float, int, bool) – Function to be applied if
pred
is False. This function must receive one arguement foroperands
.operands (Any) – Operands (A) input to branching function depending on
pred
. The type can be a scalar, array, or any pytree (nested Python tuple/list/dict) thereof.dyn_vars (optional, Variable, sequence of Variable, dict) –
The dynamically changed variables.
Deprecated since version 2.4.0: No longer need to provide
dyn_vars
. This function is capable of automatically collecting the dynamical variables used in the targetfunc
.child_objs (optional, dict, sequence of BrainPyObject, BrainPyObject) –
The children objects used in the target function.
Deprecated since version 2.4.0: No longer need to provide
dyn_vars
. This function is capable of automatically collecting the dynamical variables used in the targetfunc
.
- Returns:
res – The conditional results.
- Return type:
Any