Equations with condition


here is an example that define the differential equation with a condition on a variable:
consider ternary operator in python: min = a if a < b else b

dc_Ca/dt= 'some code as a function of membrain potentail' :1
tau_m = 76*ms-72*ms*c_Ca/5 if(c_Ca < 5) else 4*ms :second

where c_Ca is variable that update with membrane potential, tau_m is also update with c_Ca.
is it possible to define it in the Brian?

I look for a while in the documentation to find an example for this, but I did not find anything.
Thank you for any guide.

I think we can use this:

tau_m = (76*ms-72*ms*c_Ca/5) * (c_Ca < 5) + 4*ms * (c_Ca >= 5) :second

Yes, your solution is the way to go currently, even though we recommend to use e.g. int(c_Ca < 5) instead of (c_Ca < 5) to make the conversion of a boolean value into 0 or 1 explicit. C and Python will do this automatically, but in principle other code generation targets might not handle it (e.g. in Java this would not work, admittedly a rather theoretical concern for now). We mention this in the documentation, but probably not in the expected place: Functions — Brian 2 2.4.2 documentation

We actually had this discussion about adding support for ternary operators, I agree it would be a good idea: Better syntax for if/else equations · Issue #1033 · brian-team/brian2 · GitHub It’s non-trivial to implement on the code generation side, but certainly doable.

1 Like

Oh, I just realized: you probably want tau_m = (76*ms - 72*ms*(c_Ca/5)) * int(c_Ca < 5) + 4*ms * int(c_Ca >= 5), i.e. with parentheses around the first term? Otherwise the function would be discontinuous at c_Ca=5. Actually in this case there is an alternative solutions since the conditional assignment is only used to clamp the value. Instead of using the boolean operator trick you can use: tau_m = clip(76*ms - 72*ms*(c_Ca/5), 4*ms, inf*ms) : second – this is probably more readable.

1 Like

Oh yeah I forgot the parenthesis, Thank you.