while_loop#
- class brainpy.math.while_loop(body_fun, cond_fun, operands)[source]#
while-loopcontrol flow withVariable.Changed in version 2.3.0:
dyn_varshas been changed into a default argument. Please change your call fromwhile_loop(f1, f2, dyn_vars, operands)towhile_loop(f1, f2, operands, dyn_vars).Note the diference between
for_loopandwhile_loop:while_loopdoes not support accumulating history values.The returns on the body function of
for_looprepresent the values to stack at one moment. However, the functional returns of body function inwhile_looprepresent the operands’ values at the next moment, meaning that the body function ofwhile_loopdefines the updating rule of how the operands are updated.
>>> import brainpy.math as bm >>> >>> a = bm.Variable(bm.zeros(1)) >>> b = bm.Variable(bm.ones(1)) >>> >>> def cond(x, y): >>> return x < 6. >>> >>> def body(x, y): >>> a.value += x >>> b.value *= y >>> return x + b[0], y + 1. >>> >>> res = bm.while_loop(body, cond, operands=(1., 1.)) >>> res (10.0, 4.0)
Added in version 2.1.11.
- Parameters:
body_fun (
Callable) – A function which define the updating logic. It receives one argument foroperands, without returns.cond_fun (
Callable) – A function which define the stop condition. It receives one argument foroperands, with one boolean value return.operands (
Any) – The operands forbody_funandcond_funfunctions.dyn_vars (
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
child_objs. This function is capable of automatically collecting the children objects used in the targetfunc.