brainpy.math.controls.ifelse
brainpy.math.controls.ifelse#
- brainpy.math.controls.ifelse(conditions, branches, operands=None, dyn_vars=None, show_code=False)[source]#
If-else
control flows looks like native Pythonic programming.Examples
>>> import brainpy.math as bm >>> def f(a): >>> return bm.ifelse(conditions=[a > 10, a > 5, a > 2, a > 0], >>> branches=[lambda _: 1, >>> lambda _: 2, >>> lambda _: 3, >>> lambda _: 4, >>> lambda _: 5]) >>> f(1) 4 >>> # or, it can be expressed as: >>> def f(a): >>> return bm.ifelse(conditions=[a > 10, a > 5, a > 2, a > 0], >>> branches=[1, 2, 3, 4, 5]) >>> f(3) 3
- Parameters
conditions (bool, sequence of bool) – The boolean conditions.
branches (Sequence) – The branches, at least has two elements. Elements can be functions, arrays, or numbers. The number of
branches
andconditions
has the relationship of len(branches) == len(conditions) + 1. Each branch should receive one arguement foroperands
.operands (optional, Any) – The operands for each branch.
dyn_vars (Variable, sequence of Variable, dict) – The dynamically changed variables.
show_code (bool) – Whether show the formatted code.
- Returns
res – The results of the control flow.
- Return type
Any