brainpy.math.controls.make_cond
brainpy.math.controls.make_cond#
- brainpy.math.controls.make_cond(true_fun, false_fun, dyn_vars=None)[source]#
Make a condition (if-else) function.
Examples
>>> import brainpy.math as bm >>> a = bm.zeros(2) >>> b = bm.ones(2) >>> >>> def true_f(x): a.value += 1 >>> def false_f(x): b.value -= 1 >>> >>> cond = bm.make_cond(true_f, false_f, dyn_vars=[a, b]) >>> cond(True) >>> a, b (JaxArray([1., 1.], dtype=float32), JaxArray([1., 1.], dtype=float32)) >>> cond(False) >>> a, b (JaxArray([1., 1.], dtype=float32), JaxArray([0., 0.], dtype=float32))
- Parameters
true_fun (callable, function) – A function receives one argument, without any returns.
false_fun (callable, function) – A function receives one argument, without any returns.
dyn_vars (dict of JaxArray, sequence of JaxArray) – The dynamically changed variables.
- Returns
cond_func – The condictional function receives two arguments:
pred
for true/false judgement andx
for external input.- Return type
callable, function