def thresh_Naka_Rushton_fndef(N=2, half_on=120, max_val=100, with_if=True, sys_pars=None): """Can specify strings or numbers for half_on and max_val arguments, in case of including parameters or other variables in the definitions. (Don't forget to declare those parameters, in that case.) Use the with_if=False case to ensure Jacobians can be calculated. Use sys_pars list to provide names of any model parameters or functions that will be declared elsewhere. """ assert N == int(N), "Provide integer N" extra_pars = [] if sys_pars is None: sys_pars = [] if not isinstance(half_on, str): half_on = str(half_on) else: Q = QuantSpec('h', half_on) # don't add model system parameters to function parameter list extra_pars.extend(remain(Q.freeSymbols, sys_pars)) if not isinstance(max_val, str): max_val = str(max_val) else: Q = QuantSpec('m', max_val) extra_pars.extend(remain(Q.freeSymbols, sys_pars)) if with_if: return (['x']+extra_pars, 'if(x>0,%s*pow(x,%i)/(pow(%s,%i) + pow(x,%i)),0)' % (max_val, N, half_on, N, N)) else: return (['x']+extra_pars, '%s*pow(x,%i)/(pow(%s,%i) + pow(x,%i))' % (max_val, N, half_on, N, N))
def test_nested_functions_diff(): """Examples of differentiation using nested functions. This functionality is built in to Symbolic.prepJacobian """ func_ma_spec = (["p", "v"], "0.32*(v+54)/(1-exp(-p*(v+54)/4))") ma = Fun(func_ma_spec[1], func_ma_spec[0], "ma") ma_1 = Fun(Diff(ma, "v"), ["v"], "ma_1") func_mb_spec = (["v"], "0.28*(v+27)/(exp((v+27)/5)-1)") mb = Fun(func_mb_spec[1], func_ma_spec[0], "mb") mb_0 = Fun(Diff(mb, "v"), ["v"], "mb_0") func_ma_1_spec = (["p", "v"], str(ma_1.spec)) func_mb_0_spec = (["v"], str(mb_0.spec)) # artificial example to introduce time dependence rhs_m = "exp(-2*t)*(100-v) + ma(1, v)*(1-m)-mb(v)*m" jac_part = Diff(rhs_m, ["v", "m"]) f = expr2fun(jac_part, ma_1=func_ma_1_spec, mb_0=func_mb_0_spec, ma=func_ma_spec, mb=func_mb_spec) assert remain(f._args, ["t", "v", "m"]) == [] assert abs(norm(f(0, -50, 0.2)) - 8.565615) < 1e-6 # alternative usage syntax assert abs(norm(f(**{"t": 0, "v": -50, "m": 0.2})) - 8.565615) < 1e-6
def test_nested_functions_diff(): """Examples of differentiation using nested functions. This functionality is built in to Symbolic.prepJacobian """ func_ma_spec = (['p', 'v'], '0.32*(v+54)/(1-exp(-p*(v+54)/4))') ma = Fun(func_ma_spec[1], func_ma_spec[0], 'ma') ma_1 = Fun(Diff(ma, 'v'), ['v'], 'ma_1') func_mb_spec = (['v'], '0.28*(v+27)/(exp((v+27)/5)-1)') mb = Fun(func_mb_spec[1], func_ma_spec[0], 'mb') mb_0 = Fun(Diff(mb, 'v'), ['v'], 'mb_0') func_ma_1_spec = (['p', 'v'], str(ma_1.spec)) func_mb_0_spec = (['v'], str(mb_0.spec)) # artificial example to introduce time dependence rhs_m = 'exp(-2*t)*(100-v) + ma(1, v)*(1-m)-mb(v)*m' jac_part = Diff(rhs_m, ['v', 'm']) f = expr2fun(jac_part, ma_1=func_ma_1_spec, mb_0=func_mb_0_spec, ma=func_ma_spec, mb=func_mb_spec) assert remain(f._args, ['t', 'v', 'm']) == [] assert abs(norm(f(0, -50, 0.2)) - 8.565615) < 1e-6 # alternative usage syntax assert abs(norm(f(**{'t': 0, 'v': -50, 'm': 0.2})) - 8.565615) < 1e-6
def make_Jac(DS, varnames=None): if varnames is None: varnames = DS.funcspec.vars subdomain = {} fixedvars = remain(DS.funcspec.vars, varnames) for k in fixedvars: subdomain[k] = DS.initialconditions[k] jac, new_fnspecs = prepJacobian(DS.funcspec._initargs['varspecs'], varnames, DS.funcspec._initargs['fnspecs']) scope = copy.copy(DS.pars) scope.update(subdomain) scope.update(new_fnspecs) return expr2fun(jac, ensure_args=['t'], **scope)