make_cond

Contents

make_cond#

class brainpy.math.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
(Array([1., 1.], dtype=float32),
 Array([1., 1.], dtype=float32))
>>> cond(False)
>>> a, b
(Array([1., 1.], dtype=float32),
 Array([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 ArrayType, sequence of ArrayType) – The dynamically changed variables.

Returns:

cond_func – The condictional function receives two arguments: pred for true/false judgement and x for external input.

Return type:

ControlObject