Skip to content

Commit 9ab9a29

Browse files
authored
[fix] bugs of control flows (#404)
[fix] bugs of control flows
2 parents 12a9c9f + 41e7040 commit 9ab9a29

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

brainpy/_src/math/object_transform/controls.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,9 +905,11 @@ def _body_fun(op):
905905
v._value = dyn_vals[k]
906906
new_vals = body_fun(*old_vals)
907907
if new_vals is None:
908-
new_vals = tuple()
908+
new_vals = old_vals
909909
if not isinstance(new_vals, tuple):
910910
new_vals = (new_vals,)
911+
if isinstance(new_vals, list):
912+
new_vals = tuple(new_vals)
911913
return dyn_vars.dict_data(), new_vals
912914

913915
def _cond_fun(op):

brainpy/_src/math/object_transform/tests/test_controls.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,26 @@ def body(x, y):
205205
print()
206206
print(res)
207207

208+
def test3(self):
209+
a = bm.Variable(bm.zeros(1))
210+
b = bm.Variable(bm.ones(1))
211+
212+
def cond(x, y):
213+
return bm.all(a.value < 6.)
214+
215+
def body(x, y):
216+
a.value += x
217+
b.value *= y
218+
219+
res = bm.while_loop(body, cond, operands=(1., 1.))
220+
self.assertTrue(bm.allclose(a, 6.))
221+
self.assertTrue(bm.allclose(b, 1.))
222+
print()
223+
print(res)
224+
print(a)
225+
print(b)
226+
227+
208228
def test2(self):
209229
a = bm.Variable(bm.zeros(1))
210230
b = bm.Variable(bm.ones(1))

brainpy/_src/neurons/input_groups.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def reset_state(self, batch_size=None):
147147

148148
def update(self):
149149
self.spike.value = bm.zeros_like(self.spike)
150-
bm.while_loop(self._cond_fun, self._body_fun, share.load('t'))
150+
bm.while_loop(self._body_fun, self._cond_fun, share.load('t'))
151151
return self.spike.value
152152

153153
# functions

0 commit comments

Comments
 (0)