brainpy / BrainPy

Brain Dynamics Programming in Python
https://brainpy.readthedocs.io/
GNU General Public License v3.0
531 stars 94 forks source link

Example error in function brain.math.ifelse() #455

Closed jnjnnjzch closed 1 year ago

jnjnnjzch commented 1 year ago

The example code of function brain.math.ifelse() seems not work. The function is located at brainpy/_src/math/object_transform/controls.py The example of doc doesn't work either, which is located at https://brainpy.readthedocs.io/en/latest/apis/auto/generated/brainpy.math.ifelse.html

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)

To let it work, one way is to delete "_".

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)

Another way is to directly not use lambda expression

import brainpy.math as bm
def f(a):
return bm.ifelse(conditions=[a > 10, a > 5, a > 2, a > 0],
                     branches=[1, 2, 3, 4, 5])
 f(1)
chaoming0625 commented 1 year ago

So many thanks for your report. That is indeed a bug. We should fix the bug in the doc.