cond#
- class brainpy.math.cond(pred, true_fun, false_fun, operands=())[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 (
Union[Callable,Array,Array,Number]) – Function to be applied ifpredis True. This function must receive one arguement foroperands.false_fun (
Union[Callable,Array,Array,Number]) – Function to be applied ifpredis False. This function must receive one arguement foroperands.operands (
Any) – Operands (A) input to branching function depending onpred. The type can be a scalar, array, or any pytree (nested Python tuple/list/dict) thereof.dyn_vars (optional,
Variable,sequenceofVariable,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,sequenceofBrainPyObject,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